Grouped barplot in ggplot2 in R

你说的曾经没有我的故事 提交于 2019-12-12 13:04:31

问题


I would like to make a grouped bar plot. An example of my data is as follows:

site code  year month gear total value
678490     2012 3     GL   13882
678490     2012 4     GL   50942
678490     2012 5     GL   54973
678490     2012 6     GL   63938
678490     2012 7     GL   23825
678490     2012 8     GL   8195
678490     2012 9     GL   14859
678490     2012 9     RT   3225
678490     2012 10    GL   981
678490     2012 10    RT   19074
678490     2012 11    SD   106384
678490     2012 11    RT   2828
678490     2012 12    GL   107167
678490     2012 12    RT   4514

There are 17 site code options, four year options, twelve month options, and four gear options.

What I would to produce is a plot per site, per year, showing the 'total value' for each gear, for each month, as a bar.

So far I have managed to produce a plot, specific to site and year, but with the total values displayed in one bar per month, not separated into separate bars per month (can not include image in first post!)

But for months 9, 10, 11 and 12 there were two gears used so I want there to be two bars for these months.

I am using the following piece of code:

ggplot(subset(cdata, year %in% c("2012") & site code %in% c("678490")), 
        aes(x = factor(month), y = total value)) + 
        geom_bar(stat = "identity") +
        labs(x = "Month", y = "Total value")

Any help on this would be greatly appreciated.


回答1:


If you want separate bars for each gear, then you should add fill=gear to the aes in geom_bar:

ggplot(cdata[cdata$year==2012 & cdata$sitecode==678490,],
       aes(x = factor(month), y = totalvalue, fill=gear)) + 
  geom_bar(stat = "identity", position="dodge") +
  labs(x = "Month", y = "Total value")

this gives:

When you want to make a plot per site, per year, showing the 'total value' for each gear, for each month, as a bar, you can use facet_grid. For example:

ggplot(cdata, aes(x = factor(month), y = totalvalue, fill=gear)) + 
  geom_bar(stat = "identity", position="dodge") +
  labs(x = "Month", y = "Total value") +
  facet_grid(sitecode ~ year)

this gives:

Some additional comments:

  • It's probably better not to use spaces in your column names (in the code above I removed the spaces)
  • Add an example to your question which illustrative for the problem you are facing. In this case, it's better to give an example dataset that includes several sitecodes and several years.

I therefore made up some data:

df1 <- read.table(text="sitecode  year month gear totalvalue
678490     2012 3     GL   13882
678490     2012 4     GL   50942
678490     2012 5     GL   54973
678490     2012 6     GL   63938
678490     2012 7     GL   23825
678490     2012 8     GL   8195
678490     2012 9     GL   14859
678490     2012 9     RT   3225
678490     2012 10    GL   981
678490     2012 10    RT   19074
678490     2012 11    SD   106384
678490     2012 11    RT   2828
678490     2012 12    GL   107167
678490     2012 12    RT   4514", header= TRUE)

df2 <- df1
df2$sitecode <- 7849
df2$year <- 2013
df3 <- df1
df3$sitecode <- 7849
df4 <- df1
df4$year <- 2013

cdata <- rbind(df1,df2,df3,df4)


来源:https://stackoverflow.com/questions/30996960/grouped-barplot-in-ggplot2-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!