将组均值线添加到geom_bar图并包括在图例中 [英] Adding group mean lines to geom_bar plot and including in legend

查看:118
本文介绍了将组均值线添加到geom_bar图并包括在图例中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够创建一个条形图,该条形图还显示每个组中条形的平均值. AND显示图例中的平均值条.

I want to be able to create a bar graph which shows also shows the mean value for bars in each group. AND shows the mean bar in the legend.

我已经可以使用下面的代码来获得带有条形图的条形图,这很好,但是我希望能够看到图例中的均线.

I have been able to get this graph Bar chart with means using the code below, which is fine, but I would like to be able to see the mean lines in the legend.

##The data to be graphed is the proportion of persons receiving a treatment
## (num=numerator) in each population (denom=demoninator). The population is 
##grouped by two age groups and (Age) and further divided by a categorical 
##variable V1

###SET UP DATAFRAME###
require(ggplot2)    
df <- data.frame(V1 = c(rep(c("S1","S2","S3","S4","S5"),2)), 
               Age= c(rep(70,5),rep(80,5)), 
               num=c(5280,6570,5307,4894,4119,3377,4244,2999,2971,2322),
               denom=c(9984,12600,9425,8206,7227,7290,8808,6386,6206,5227))

df$prop<-df$num/df$denom*100

PopMean<-sum(df$num)/sum(df$denom)*100

df70<-df[df$Age==70,]
group70mean<-sum(df70$num)/sum(df70$denom)*100

df80<-df[df$Age==80,]
group80mean<-sum(df80$num)/sum(df80$denom)*100

df$PopMean<-c(rep(PopMean,10))
df$groupmeans<-c(rep(group70mean,5),rep(group80mean,5))

我希望情节看起来像这样,但也希望图例中的线条被标记为组均值"或类似的内容.

I want the plot to look like this, but want the lines in the legend too, to be labelled as 'mean of group' or similar.

 #basic plot
 P<-ggplot(df, aes(x=factor(Age), y=prop, fill=factor(V1))) +
   geom_bar(position=position_dodge(), colour='black',stat="identity")    

 P
####add mean lines    
P+geom_errorbar(aes(y=df$groupmeans, ymax=df$groupmeans, 
ymin=df$groupmeans), col="red", lwd=2)

添加show.legend = TRUE会将误差线覆盖在因子图例上,而不是单独覆盖.如果可以在图例中单独显示geom_errorbar,则这可能是最简单的解决方案.

Adding show.legend=TRUE overlays the error bars onto the factor legend, rather than separately. If there is a way of showing geom_errorbar separately in the legend this is probably the simplest solution.

我也尝试过用geom_line做各种事情 下面的语法为总体平均值生成一条线,但从每个点的中心开始,而不是覆盖条的宽度 这样就产生了一条用于总体平均值的线,并且确实产生了一个图例,但是却显示了一条颜色条而不是一条线.

I have also tried various things with geom_line The syntax below produces a line for the population mean value, but running from the centre of each point rather than covering the width of the bars This produces a line for the population mean and it does produce a legend but one showing a bar of colour rather than a line.

P+geom_line(aes(y=df$PopMean, group=df$PopMean, color=df$PopMean),lwd=1)

如果我尝试为组做线,则意味着线不可见(因为它们只是单点).

If i try to do lines for group means the lines are not visible (because they are only single points).

P+geom_line(aes(y=df$groupmeans, group=df$groupmeans, color=df$groupmeans))

我也试图用刻面图来解决这个问题,尽管这要求我假装我的分类变量是数字的才能起作用.

I also tried to get round this with facet plot, although this requires me to pretend my categorical variable is numeric to get it to work.

###set up new df
df2<-df
df2$V1<-c(rep(c(1,2,3,4,5),2))

P<-ggplot(df2, aes(x=factor(V1), y=prop, fill=factor(V1))) +
  geom_bar(position=position_dodge(),     
  colour='black',stat="identity",width=1)

P+facet_grid(.~factor(df2$Age))

P+facet_grid(.~factor(df2$Age))+geom_line(aes(y=df$groupmeans, 
group=df$groupmeans, color=df$groupmeans))

Facetplot

Facetplot

这使我可以使用geom_line显示平均线,因此确实会出现图例(尽管看起来不正确,显示的是颜色渐变而不是彩色的线条!).但是,线条仍未达到条形图的整个宽度.另外,我的x轴现在需要重新标记以显示S1,S2等,而不是数字1,2,3

This allows me to show the mean lines, using geom_line, so a legend does appear (although it doesn't look right, showing a colour gradient rather than coloured lines!). However, the lines still do not go the full width of the bars. Also my x-axis now needs relabelling to show S1, S2 etc rather than numeric 1,2,3

总而言之-是否可以在图例中单独显示错误栏行?

To sum up - is there a way of showing error bar lines separately in the legend?

如果没有,那么,如果我使用刻面,如何使用图例变量校正图例外观并重新标记轴,并且有可能使线段达到图的整个宽度?

If not, then, if i use facetting, how do I correct the legend appearance and relabel axes with my categorical variables and is is possible to get the line to go the full width of the plot?

还是我找不到其他解决方案!?

Or is there an alternate solution that I am missing!?

谢谢

推荐答案

要获取geom_error的图例,您需要在aes中传递colour参数. 由于您只需要一个类别(此处为红色),因此我首先创建了一个虚拟变量

To get the legend for the geom_error you need to pass the colour argument in the aes. As you want only one category (here red), I've create a dummy variable first

df$mean <- "Mean"
ggplot(df, aes(x=factor(Age), y=prop, fill=factor(V1))) +
  geom_bar(position=position_dodge(), colour='black',stat="identity") +
  geom_errorbar(aes (ymax=groupmeans, 
                ymin=groupmeans, colour=mean), lwd=2) +
  scale_colour_manual(name="",values = "#ff0000")

这篇关于将组均值线添加到geom_bar图并包括在图例中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆