在geom_hline中为从geom_vline创建的元素创建图例 [英] Creating legend in geom_histogram for elements created from geom_vline

查看:222
本文介绍了在geom_hline中为从geom_vline创建的元素创建图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是一个示例数据集:

  structure(list(Age = c(6L,7L,5L,6L, 7L,9L,6L,7L,5L,6L,7L,9L,6L,7L,5L,6L,7L,9L),Year = c(2011,
2011,2011,2011,2011,2011,2011 ,2011,2011,2011,2011,2011,2011,2011,2011,2011,2011)),.Names = c(Age,Year),row.names = c(NA,6L), class =data.frame)

我试着创建一个图例来显示三个组件我在下面的geom_vline命令中列出。我已经阅读了几个关于s.overflow的例子,但似乎没有任何工作..



这是我到目前为止:

 #创建标准误差并在直方图上绘制平均线
se < - function(x)sqrt(var(x)/ length(x) )
se_11 < - se(Age_2011 $ Age)
mean_11 < - mean(Age_2011 $ Age)
se_11_plus< - mean_11 + se_11
se_11_minus< - mean_11 - se_11

#plot
p11_age < - ggplot(Age_2011,aes(x = Age))+
geom_histogram(aes(y =(.. count ..)/ sum (..count ..)),binwidth = 1,origin = - 。5,fill =white,color =black,show_guide = TRUE)+
scale_y_continuous(labels = percent_format(),name = 频率(%))+ ##绘制百分比频率
xlab(年龄(岁))+
scale_x_continuous(限制= c(1,45),休息= seq(1,45 ,1))+
scale_colour_discrete(name =Units,guide =legend)+#尝试创建图例

#用于平均值和标准误差的垂直线
geom_vline (AES(xintercept =平均值(A ge_2011 $ Age),na.rm = T),color =red,linetype =dashed,size = 1,show_guide = TRUE)+
geom_vline(aes(xintercept = se_11_plus),color =blue ,show_guide = TRUE)+
geom_vline(aes(xintercept = se_11_minus),color =blue,show_guide = TRUE)+

#使用指南创建自定义图例
scale_linetype_manual (name =test,labels = c(median,test,test2),values = c(median= 1,test= 2,test3= 3))+
主题(legend.key = element_rect(fill =white,color =white))+
theme(legend.background = element_blank())+
guides(color = guide_legend(override .aes = list(linetype = 0)),fill = guide_legend(override.aes = list(linetype = 0)),
shape = guide_legend(override.aes = list(linetype = 0)),
linetype = guide_legend())+

#title和background
ggtitle(2011年Catch的年龄频率直方图)+
主题(panel.grid.major = element_blank ),panel.grid.minor = element_blank(),panel.background = element_rect(color =black,fill =white))

所有的geom_vlines显示,但是我不知道如何得到一个图例,当真的只有一个直方图系列,我想要在图例中是垂直线。



任何帮助表示赞赏。谢谢

解决方案

这或多或少是您要求的?

  library(scales)
p11_age< - ggplot(Age_2011,aes(x = Age))+
geom_histogram(aes(y = .. count .. /sum(..count ..)),binwidth = 1,origin = -0.5,fill = NA,color =black)+
scale_y_continuous(name =Frequency(%),labels = percent_format( ))+
scale_x_continuous(name =Age(年),limits = c(1,45),breaks = seq(1,45,1))+
#均值和标准的垂直线错误
geom_vline(aes(xintercept = mean_11,color =Mean,linetype =Mean),size = 1,show_guide = TRUE)+
geom_vline(xescept = se_11_plus,color = (std.Err),linetype =Std.Err),show_guide = TRUE)+
geom_vline(aes(xintercept = se_11_minus,color =Std.Err,linetype =Std.Err),show_guide = TRUE)+
scale_colour_manual(name =Units,values = c(Std.Err =blue,Mean =red ))+
scale_linetype_manual(name =Units,values = c(Mean =dashed,Std.Err =solid),guide = FALSE)+
ggtitle(Age Frequency Histogram 2011年Catch)+
主题(legend.background = element_blank(),
panel.grid.major = element_blank(),panel.grid.minor = element_blank(),
面板。 background = element_rect(color =black,fill =white))
p11_age

所以在这里我们添加3 geom_vline 图层,使用颜色和线型美学 aes(...) 。然后,我们使用 scale_color_manual(...)将Mean和Std.Err颜色映射为red和blue,并且我们将Mean和Std.Err线型在调用 scale_linetype_manual(...)时显示为虚线和实线。请注意,在 values = ... 参数中使用了命名向量。我们还通过在 scale_linetype_manual(...)调用中使用 guide = FALSE 关闭了线型指南的显示。 。其原因是,否则传说中的行将是坚实的和虚线的(我想这是你正在试图做的事情 override_aes )。


Here is an example data set:

structure(list(Age = c(6L, 7L, 5L, 6L, 7L, 9L,6L, 7L, 5L, 6L, 7L, 9L,6L, 7L, 5L, 6L, 7L, 9L), Year = c(2011, 
2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011,     2011, 2011 )), .Names = c("Age", "Year"), row.names = c(NA, 6L), class = "data.frame")

I am trying to create a legend that will show the three components that I list in my geom_vline command below. I've read several examples on s.overflow but nothing seems to be working..

This is what I have so far:

# to create standard errors and mean lines to plot on histogram
se <- function(x) sqrt(var(x)/length(x))
se_11 <- se(Age_2011$Age)
mean_11 <- mean(Age_2011$Age)
se_11_plus <- mean_11 + se_11
se_11_minus <- mean_11 - se_11

#plot
p11_age <- ggplot(Age_2011, aes(x=Age))+ 
geom_histogram(aes(y=(..count..)/sum(..count..)), binwidth=1, origin=-.5, fill="white",    color="black", show_guide=TRUE)+             
scale_y_continuous(labels=percent_format(), name="Frequency (%)")+  ## plotting in     percent frequency
xlab("Age (years)")+
scale_x_continuous(limits=c(1,45), breaks=seq(1,45,1))+
scale_colour_discrete(name="Units", guide="legend")+ #attempting to create legend

# vertical lines for mean and standard errors
geom_vline(aes(xintercept=mean(Age_2011$Age), na.rm=T), color="red", linetype="dashed",     size=1, show_guide=TRUE)+
geom_vline(aes(xintercept=se_11_plus), color ="blue", show_guide=TRUE)+
geom_vline(aes(xintercept=se_11_minus), color="blue", show_guide=TRUE)+

# creating custom legends using guides
scale_linetype_manual(name="test", labels =c("median", "test", "test2"), values =     c("median"=1, "test"=2, "test3"=3))+
theme(legend.key=element_rect(fill="white", color ="white"))+
theme(legend.background=element_blank())+
guides(colour=guide_legend(override.aes=list(linetype=0)),   fill=guide_legend(override.aes=list(linetype=0)), 
    shape=guide_legend(override.aes=list(linetype=0)),
    linetype=guide_legend())+

#title and background
ggtitle("Age Frequency Histogram of 2011 Catch")+
theme(panel.grid.major=element_blank(), panel.grid.minor=element_blank(),     panel.background=element_rect(colour="black", fill="white"))

All of the geom_vlines show however I can't figure out how to get a legend when there is really only one histogram "series" and all I want in the legend are the vertical lines.

Any help is appreciated. Thanks

解决方案

Is this more or less what you're asking for?

library(ggplot2)
library(scales)
p11_age <- ggplot(Age_2011, aes(x=Age))+ 
  geom_histogram(aes(y=..count../sum(..count..)), binwidth=1, origin=-0.5, fill=NA, color="black")+             
  scale_y_continuous(name="Frequency (%)", labels=percent_format())+
  scale_x_continuous(name="Age (years)",limits=c(1,45), breaks=seq(1,45,1))+  
  # vertical lines for mean and standard errors
  geom_vline(aes(xintercept=mean_11, color="Mean", linetype="Mean"), size=1, show_guide=TRUE)+
  geom_vline(aes(xintercept=se_11_plus, color="Std.Err", linetype="Std.Err"), show_guide=TRUE)+
  geom_vline(aes(xintercept=se_11_minus, color="Std.Err", linetype="Std.Err"), show_guide=TRUE)+
  scale_colour_manual(name="Units", values=c(Std.Err="blue",Mean="red"))+
  scale_linetype_manual(name="Units", values=c(Mean="dashed",Std.Err="solid"), guide=FALSE)+
  ggtitle("Age Frequency Histogram of 2011 Catch")+
  theme(legend.background=element_blank(),
        panel.grid.major=element_blank(), panel.grid.minor=element_blank(), 
        panel.background=element_rect(colour="black", fill="white"))
p11_age

So here we add the 3 geom_vline layers, using color and linetype aesthetics inside the call to aes(...). Then we map the "Mean" and "Std.Err" colors to "red" and "blue" using scale_color_manual(...), and we map the "Mean" and "Std.Err" linetypes to "dashed" and "solid" in the call to scale_linetype_manual(...). Note the use of named vectors in the values=... argument. We also turn off display of the linetype guide by using guide=FALSE in the call to scale_linetype_manual(...). The reason for this is that otherwise the lines in the legend would be solid and dashed (I think this is what you were trying to do with override_aes).

这篇关于在geom_hline中为从geom_vline创建的元素创建图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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