R中ggplot2中的分组条形图

编程入门 行业动态 更新时间:2024-10-26 07:33:42
本文介绍了R中ggplot2中的分组条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想进行分组的条形图.我的数据示例如下:

站点代码年份月份齿轮总值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

有17种站点代码选项,4年选项,12个月选项和4种齿轮选项.

我要生产的是每年每个站点的图,以条形图显示每个齿轮,每个月的总值".

到目前为止,我已经设法生成了一个针对地点和年份的图,但是总值显示在每月一个栏中,没有分成每月单独的栏(不能在第一篇文章中包含图片!)

但是在第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 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.

解决方案

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)

更多推荐

R中ggplot2中的分组条形图

本文发布于:2023-07-18 00:48:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1139254.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:条形图

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!