在ggplot2中匹配图例项和颜色,其中某些geom_segment未包含在图例

时间:2017-10-07 21:58:34

标签: r ggplot2

我似乎无法在ggplot2中正确匹配我的图例标签和颜色。我有一些geom_segments,我不想包含在图例中。我尝试过各种各样的选择但没有工作。现有的问题似乎都没有解决在图上没有标注某些元素的问题,所以这可能会增加复杂性。代码如下:

library("distr")
library("ggplot2")
Percent_values<-c(0.5,0.75,0.9,0.95,0.995,0.999)
Dist1_mean=20219
Dist1_CV=3235/20219
Dist1_SDEV<-Dist1_mean*Dist1_CV
Dist1_parm2<-sqrt(log(1+Dist1_CV^2))
Dist1_parm1<-log(Dist1_mean)-(Dist1_parm2^2)/2
Dist1_quant<-qlnorm(Percent_values,meanlog=Dist1_parm1,sdlog=Dist1_parm2)
#Now draw CDF with vertical line at mean, median and chosen percentile
a1<-stat_function(fun = 
plnorm,args=list(meanlog=Dist1_parm1,sdlog=Dist1_parm2),geom="line", 
colour="blue",size=1.25)
lowerx<-0
upperx<-1.1*Dist1_quant[6]
plot1<-ggplot(data.frame(x = c(lowerx, upperx)), aes(x = x))+a1
plot1<-
plot1+scale_x_continuous(name="Value")+scale_y_continuous(name="Cumulative 
probability")
#add mean vertical line and associated horizontal line to axis
mean_yvalue<-plnorm(Dist1_mean,meanlog=Dist1_parm1,sdlog=Dist1_parm2)
plot1<-plot1+geom_segment(aes(x=Dist1_mean,y=0,
xend=Dist1_mean,yend=mean_yvalue,colour="red"),size=1.25)
plot1<-
plot1+geom_segment(aes(x=0,y=mean_yvalue,xend=Dist1_mean,
yend=mean_yvalue,colour="red"),size=1.25,linetype="dotted",show.legend = 
FALSE)
#and 75th percentile
perc<-0.75
p75<-Dist1_quant[2]
plot1<-
plot1+geom_segment(aes(x=p75,y=0,xend=p75,
yend=perc,colour="green"),size=1.25)
plot1<-plot1+geom_segment(aes(x=0,y=perc,xend=p75,
yend=perc,colour="green"),size=1.25,linetype="dotted",show.legend = FALSE)
#and 99.5th
perc2<-0.995
p995<-Dist1_quant[6]
plot1<-
plot1+geom_segment(aes(x=p995,y=0,xend=p995,
yend=perc2,colour="orange"),size=1.25)
plot1<-
plot1+geom_segment(aes(x=0,y=perc2,xend=p995,
yend=perc2,colour="orange"),size=1.25,linetype="dotted",show.legend = FALSE)
plot1<-plot1+ggtitle("Cumulative density function of estimated future claims 
outgo")+
scale_colour_discrete(name="", labels=c("Lognormal", "Mean","75th 
%ile","99.5th %ile"))
plot1

这会生成一个图表(我似乎无法因某种原因加载)a)当我想要四个时,它只有三个图例项目(“Lognormal”,“Mean”和“75th%ile”)(另外“ 99.5%ile“添加到这三个”,这三个项目分别用红色,绿色和蓝色,而我希望Lognormal为蓝色,Mean为红色,第75个为绿色,99.5为橙色。虚线应保留在图上,但不会出现在图例中。

我做错了什么?据推测,它与美学和“scale_colour_discrete”有关,但我无法弄清楚该怎么做。任何帮助将不胜感激。

感谢。

2 个答案:

答案 0 :(得分:1)

在“a1”变量中的颜色参数周围添加aes()。然后使用scale_color_manual()指定图例的颜色和标签。

对于将来的图形,如果在脚本的第一部分中一起完成计算,则可能更容易跟踪错误,然后在所有计算之后绘制数据。否则,如果所有内容混合在一起并重叠,似乎故障排除可能需要一段时间。

试试这个:

library("distr")
library("ggplot2")

#calculations
Percent_values<-c(0.5,0.75,0.9,0.95,0.995,0.999)
Dist1_mean=20219
Dist1_CV=3235/20219
Dist1_SDEV<-Dist1_mean*Dist1_CV
Dist1_parm2<-sqrt(log(1+Dist1_CV^2))
Dist1_parm1<-log(Dist1_mean)-(Dist1_parm2^2)/2
Dist1_quant<-qlnorm(Percent_values,meanlog=Dist1_parm1,sdlog=Dist1_parm2)
lowerx<-0
upperx<-1.1*Dist1_quant[6]
mean_yvalue<-plnorm(Dist1_mean,meanlog=Dist1_parm1,sdlog=Dist1_parm2)
perc<-0.75
p75<-Dist1_quant[2]
perc2<-0.995
p995<-Dist1_quant[6]

#plot
ggplot(data.frame(x = c(lowerx, upperx)), aes(x = x))+
  stat_function(fun=plnorm,args=list(meanlog=Dist1_parm1,sdlog=Dist1_parm2),
                geom="line",aes(colour="blue"),size=1.25) +
  scale_x_continuous(name="Value")+
  scale_y_continuous(name="Cumulative probability") +
  geom_segment(aes(x=Dist1_mean,y=0,
               xend=Dist1_mean,yend=mean_yvalue,colour="red"),size=1.25) +
  geom_segment(aes(x=0,y=mean_yvalue,xend=Dist1_mean,
               yend=mean_yvalue,colour="red"),size=1.25,linetype="dotted",
               show.legend = FALSE) +
  geom_segment(aes(x=p75,y=0,xend=p75,
               yend=perc,colour="green"),size=1.25) +
  geom_segment(aes(x=0,y=perc,xend=p75,
               yend=perc,colour="green"),size=1.25,linetype="dotted",
               show.legend = FALSE) +
  geom_segment(aes(x=p995,y=0,xend=p995,
               yend=perc2,colour="orange"),size=1.25) +
  geom_segment(aes(x=0,y=perc2,xend=p995,
               yend=perc2,colour="orange"),size=1.25,linetype="dotted",
  show.legend = FALSE) +
  ggtitle("Cumulative density function of estimated future claims outgo") +
  scale_colour_manual(name="", values=c("blue"="blue", "red"="red",
                                      "green"="green","orange"="orange"),
                  labels=c("Lognormal","Mean","75th %ile","99.5th %ile"))

输出:

enter image description here

答案 1 :(得分:1)

要添加到answer by www,我发现如果您不为颜色名称创建美学映射,则可以节省很多混淆的风险。而是使用有意义的标签,如"mean"作为平均线。这样,如果您稍后改变主意选择颜色,那么就不会像手动缩放那样将"blue"映射到"orange"

通常,美学映射不应直接映射到颜色,而应分配,以便稍后(手动或自动)映射到颜色。

修改

在bdemarest的建议中,我添加了更多解释和代码。这里有什么www的重构OP代码:

library("distr")
library("ggplot2")

#calculations
Percent_values <- c(0.5,0.75,0.9,0.95,0.995,0.999)
Dist1_mean = 20219
Dist1_CV = 3235 / 20219
Dist1_SDEV <- Dist1_mean*Dist1_CV
Dist1_parm2 <- sqrt(log(1+Dist1_CV^2))
Dist1_parm1 <- log(Dist1_mean)-(Dist1_parm2^2)/2
Dist1_quant <- qlnorm(Percent_values, meanlog=Dist1_parm1,
                      sdlog=Dist1_parm2)
lowerx <- 0
upperx <- 1.1*Dist1_quant[6]
mean_yvalue <- plnorm(Dist1_mean, meanlog=Dist1_parm1, 
                      sdlog=Dist1_parm2)
perc <- 0.75
p75 <- Dist1_quant[2]
perc2 <- 0.995
p995 <- Dist1_quant[6]

#plot
ggplot(data.frame(x = c(lowerx, upperx)), aes(x = x))+
  stat_function(fun=plnorm,
                args=list(meanlog=Dist1_parm1,
                          sdlog=Dist1_parm2),
                geom="line",
                aes(colour="logn"), # logn label
                size=1.25) +
  scale_x_continuous(name="Value")+
  scale_y_continuous(name="Cumulative probability") +
  geom_segment(aes(x=Dist1_mean, y=0,
                   xend=Dist1_mean,
                   yend=mean_yvalue,
                   colour="mean"), # mean label
               size=1.25) +
  geom_segment(aes(x=0, y=mean_yvalue,
                   xend=Dist1_mean,
                   yend=mean_yvalue,
                   colour="mean"), # mean label
               size=1.25, linetype="dotted",
               show.legend = FALSE) +
  geom_segment(aes(x=p75, y=0,
                   xend=p75, yend=perc,
                   colour="75th"), # 75th percentile label
               size=1.25) +
  geom_segment(aes(x=0, y=perc, xend=p75,
                   yend=perc,
                   colour="75th"), # 75th percentile label
               size=1.25, linetype="dotted",
               show.legend = FALSE) +
  geom_segment(aes(x=p995, y=0, xend=p995,
                   yend=perc2,
                   colour="995th"), # 99.5th percentile label
               size=1.25) +
  geom_segment(aes(x=0, y=perc2, xend=p995,
                   yend=perc2,
                   colour="995th"), # 99.5th percentile label
               size=1.25, linetype="dotted",
               show.legend = FALSE) +
  ggtitle("Cumulative density function of estimated future claims outgo") +
  scale_colour_manual(name="",
                      # labels map onto colors and pretty labels
                      values=c("logn"="blue",
                               "mean"="red",
                               "75th"="green",
                               "995th"="orange"),
                      labels=c("logn"="Lognormal",
                               "mean"="Mean",
                               "75th"="75th %ile",
                               "995th"="99.5th %ile"))

请注意,在aes映射中,颜色名称已替换为描述性标签(例如,"red"变为"mean""blue"变为"logn"),并且scale_colour_manual调用已更改为将这些标签映射到图例的两种颜色和漂亮标签上。这样,如果您以后决定将第75个百分位数段设为紫色而不是绿色,则只需更改底部scale_colour_manual调用中的颜色映射 - 您不必深入挖掘代码并找到合适的geom_segments进行更改,如果您懒得这么做,那么您将无法获得可怕的结果,例如:

scale_colour_manual(values=c("blue"="blue",
                             "red"="red",
                             "green"="purple", # !!!???
                             "orange"="orange"))

相反,您(非常直观地)将75th组件的颜色更改为紫色:

scale_colour_manual(values=c("logn"="blue",
                             "mean"="red",
                             "75th"="purple", # makes sense
                             "995th"="orange"))

更改代码以在美学映射中使用有意义的标签只会使其更清晰,更直观,更易于阅读和维护。它不会更改图表的最终输出:

New output is same as old

但如果你要尝试一个包含大量组件和信息丰富的图例的复杂图形,这是一个很小的习惯改变,可能会让你在路上挫败很多。 / p>