但是在第9、10、11和12个月中,使用了两个齿轮,所以我希望在这几个月中使用两个齿轮.我正在使用以下代码: ggplot(subset(cdata,year%in%c("2012")& site code%in%c("678490")),aes(x =因子(月),y =总价值))+geom_bar(stat ="identity")+实验室(x =月",y =总值") 在此方面的任何帮助将不胜感激.解决方案如果您要为每个 gear 使用单独的条形,则应在 geom_bar 中的code> aes : ggplot(cdata [cdata $ year == 2012& cdata $ sitecode == 678490,],aes(x =因子(月),y =总值,填充=齿轮))+geom_bar(stat ="identity",position ="dodge")+实验室(x =月",y =总值") 这给出了: 当您想每年在每个站点上绘制一个图,以条形图显示每个齿轮,每个月的总值"时,可以使用 facet_grid .例如: ggplot(cdata,aes(x = factor(month),y = totalvalue,fill = gear))+geom_bar(stat ="identity",position ="dodge")+实验室(x ="Month",y ="Total value")+facet_grid(网站代码〜年) 这给出了: 一些其他评论:最好不要在列名中使用空格(在上面的代码中,我删除了空格)在您的问题中添加一个示例,以说明您所面临的问题.在这种情况下,最好给出一个包含多个站点代码和几年时间的示例数据集.因此,我整理了一些数据: df1<-read.table(text ="sitecode年月齿轮总值678490 2012 3 GL 13882678490 2012 4 GL 50942678490 2012 5 GL 54973678490 2012 6 GL 63938678490 2012 7 GL 23825678490 2012 8 GL 8195678490 2012 9 GL 14859678490 2012 9室温3225678490 2012 10 GL 981678490 2012 10 RT 19074678490 2012 11 SD 106384678490 2012 11 RT 2828678490 2012 12 GL 107167678490 2012 12 RT 4514,标题为TRUE)df2<-df1df2 $ sitecode<-7849df2 $ year<-2013df3<-df1df3 $ sitecode<-7849df4<-df1df4 $ year<-2013cdata<-rbind(df1,df2,df3,df4) I would like to make a grouped bar plot. An example of my data is as follows:site code year month gear total value678490 2012 3 GL 13882678490 2012 4 GL 50942678490 2012 5 GL 54973678490 2012 6 GL 63938678490 2012 7 GL 23825678490 2012 8 GL 8195678490 2012 9 GL 14859678490 2012 9 RT 3225678490 2012 10 GL 981678490 2012 10 RT 19074678490 2012 11 SD 106384678490 2012 11 RT 2828678490 2012 12 GL 107167678490 2012 12 RT 4514There 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. 解决方案 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 totalvalue678490 2012 3 GL 13882678490 2012 4 GL 50942678490 2012 5 GL 54973678490 2012 6 GL 63938678490 2012 7 GL 23825678490 2012 8 GL 8195678490 2012 9 GL 14859678490 2012 9 RT 3225678490 2012 10 GL 981678490 2012 10 RT 19074678490 2012 11 SD 106384678490 2012 11 RT 2828678490 2012 12 GL 107167678490 2012 12 RT 4514", header= TRUE)df2 <- df1df2$sitecode <- 7849df2$year <- 2013df3 <- df1df3$sitecode <- 7849df4 <- df1df4$year <- 2013cdata <- rbind(df1,df2,df3,df4) 这篇关于R中ggplot2中的分组条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 06-05 21:38