Home > database >  How to get barplot using three columns in r
How to get barplot using three columns in r

Time:10-23

This is my data. ggplot2 bar char

CodePudding user response:

Using @br00t's data table: you could also do facets rather than a side-by-side bar chart

plot <- ggplot(data = sample_df, mapping = aes(x=degree, y=income)) 
  geom_bar(stat = "identity") 
  facet_wrap(~province)
plot

CodePudding user response:

So, that is not really a typical use of a barchart, the Y column should be numerical a frequency (count), a sum of some kind or an aggregate (percent) and the X should be categories (unless you make a horizontal bar chart. Adding that third vector is not a problem at all you can do it by adding to your aes() another dimension and you will get a 'stacked' chart where each region has a sub-bar within the particular bar for each degree Degree

aes(x = Degree, y=income, fill = region))

but my guess is you will need to do some manipulation first such as

new_df<-df%>%
group_by(region, degree)%>%
summarize(total_income=sum(income)

This will get you a total by region for each degree so that the chart knows what to do with the data. You can also us mean or median instead of sum, whatever works for your needs

  • Related