Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 在ggplot2中匹配图例项目和颜色,其中一些几何图形段不包括在图例中_R_Ggplot2 - Fatal编程技术网

R 在ggplot2中匹配图例项目和颜色,其中一些几何图形段不包括在图例中

R 在ggplot2中匹配图例项目和颜色,其中一些几何图形段不包括在图例中,r,ggplot2,R,Ggplot2,我似乎无法使我的图例标签和颜色在ggplot2中正确匹配。我有一些geom_片段不想包含在图例中。我尝试过各种各样的选择,但都没有效果。现有的问题似乎都没有涉及到不在情节上标记某些元素的问题,因此这可能增加了复杂性。代码如下: 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&

我似乎无法使我的图例标签和颜色在ggplot2中正确匹配。我有一些geom_片段不想包含在图例中。我尝试过各种各样的选择,但都没有效果。现有的问题似乎都没有涉及到不在情节上标记某些元素的问题,因此这可能增加了复杂性。代码如下:

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
库(“发行版”)
图书馆(“ggplot2”)
百分比值在“a1”变量中的颜色参数周围添加aes()。然后使用scale\u color\u 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"))
库(“发行版”)
图书馆(“ggplot2”)
#计算
百分比值在“a1”变量中的颜色参数周围添加aes()。然后使用scale\u color\u 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"))
库(“发行版”)
图书馆(“ggplot2”)
#计算
百分比值要添加到,我发现如果不创建颜色名称的美学映射,可以避免很多混淆的风险。相反,对平均线使用有意义的标签,如
“mean”
。这样,如果你以后改变了对颜色选择的想法,你就不会得到一些荒谬的东西,比如将
“蓝色”
映射到
“橙色”

通常,美学映射不应直接映射到颜色,而应指定稍后将映射到颜色的值(手动或自动)

编辑:

根据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"))
相反,您(非常直观地)将第75个组件的颜色更改为紫色:

scale_colour_manual(values=c("logn"="blue",
                             "mean"="red",
                             "75th"="purple", # makes sense
                             "995th"="orange"))
将代码更改为在美学映射中使用有意义的标签只会使其更干净、更直观、更易于阅读和维护。它不会更改图形的最终输出:

但是,如果你打算尝试一个包含大量组件和信息的复杂图表,这是一个小小的习惯改变,它可能会为你在未来的道路上节省大量的挫折,我发现,如果不创建颜色名称的美学映射,可以避免很多混淆的风险。相反,对平均线使用有意义的标签,如
“mean”
。这样,如果你以后改变了对颜色选择的想法,你就不会得到一些荒谬的东西,比如将
“蓝色”
映射到
“橙色”

通常,美学映射不应直接映射到颜色,而应指定稍后将映射到颜色的值(手动或自动)

编辑:

根据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"))
相反,您(非常直观地)将第75个组件的颜色更改为紫色:

scale_colour_manual(values=c("logn"="blue",
                             "mean"="red",
                             "75th"="purple", # makes sense
                             "995th"="orange"))
将代码更改为在美学映射中使用有意义的标签只会使其更干净、更直观、更易于阅读和维护。它不会更改图形的最终输出:


但是,如果您打算尝试一个包含大量组件和信息的图例的复杂图形,这是一个小小的习惯改变,它可能会为您在未来的道路上节省大量的挫折。

我同意!请考虑添加代码和图片来说明这一点,我同意!请考虑添加代码和图像来说明这个重要的点。非常感谢,太好了。非常感谢。