图例不显示ggplot2密度图中的线型 [英] Legend does not show line type in ggplot2 density plot
问题描述
我用一个有3个变量的数据框用ggplot创建了一个密度图。一条密度线是虚线,但图例显示了这条线的实线。
数据如下所示:
> (df)
R1 R2 R3
1 0.085383867 0.04366546 0.055320885
2 0.059148932 0.03477045 0.040804048
3 -0.181279986 -0.10189900 -0.097218145
4 0.002307494 -0.01137235 -0.003585813
5 -0.047816198 -0.04932982 -0.009389939
6 0.030535090 0.02544292 0.017650949
情节是:
ggplot(data = df)+
stat_density(aes(x = R1,color =rho = -0.6),adjust = 4,lwd = 0.5,geom =line,position =identity)+
stat_density(aes(x = R2,color =rho = 0,6 ),调整= 4,lwd = 0.5,geom =line,position =identity)+
stat_density(aes(x = R3,color =rho = 0),linetype = 2,adjust = 4,lwd = 0.5,geom =line,position =identity)+
xlim(-0.5,0.5)+
xlab(Renditen)+
ylab(Dichte )+
ggtitle(Renditeverteilung im Heston-Modell)+
主题(plot.title = element_text(face =bold,size = 16,vjust = 2),axis.title.x = element_text(vjust = -1,size = 12),
legend.textle = element_text(size = 12),legend.margin = unit(1.5,cm ),
legend.key.height = unit(1.2,line),legend.key.size = unit(0.4,cm),legend.key = element_rect(fill = NA),legend。 background = element_rect(color =darkgrey),
plot.margin =单位(c(1,1,1,1),cm))+
scale_colour_manual(values = c(rho = -0,6=红色,rho = 0,6=蓝色,rho = 0=黑色),名称=Korrelation)
最后是情节:
我怎样才能让图例显示第三条密度线的虚线(变量R3)?
提前谢谢!
解决方案将
> linetype =
放置在aes()
中为每个stat_density()
与colors =
和th相同使用scale_linetype_manual()
来根据需要设置类型。如果您对线型和颜色使用相同的图例名称,则两个图例都将放在一起。ggplot(data = df)+
stat_density(aes(x = R1,color =rho = -0,6,linetype =rho = -0,6),
adjust = 4,lwd = 0.5,geom = (x = R2,color =rho = 0,6,linetype =rho = 0,6),
adjust = 4 ,lwd = 0.5,geom =line,position =identity)+
stat_density(aes(x = R3,color =rho = 0,linetype =rho = 0),
调整= 4,lwd = 0.5,geom =line,position =identity)+
xlim(-0.5,0.5)+
xlab(Renditen)+
ylab (Dichte)+
ggtitle(Renditeverteilung im Heston-Modell)+
主题(plot.title = element_text(face =bold,size = 16,vjust = 2),
axis.title.x = element_text(vjust = -1,size = 12),
axis.title.y = element_text(vjust = -0.25,size = 12),
legend.text = element_text(size = 12),legend.title = element_text(size = 12),
legend.margin =单位(1.5,cm),
legend.key.height =单位(1.2,line),
legend.key.size =单位(0.4,cm ),
legend.key = element_rect(fill = NA),
legend.background = element_rect(color =darkgrey),
plot.margin = unit(c(1,1 ,cm))+
scale_colour_manual(values = c(rho = -0,6=red,rho = 0,6=blue,
=rho = 0=black),name =Korrelation)+
scale_linetype_manual(values = c(rho = -0.6= 1,rho = 0,6= 1,
rho = 0= 2),name =Korrelation)
I created a density plot with ggplot from a dataframe with 3 variables. One density line is dotted, but the legend shows a solid line for this line.
The data looks like this:
> head(df) R1 R2 R3 1 0.085383867 0.04366546 0.055320885 2 0.059148932 0.03477045 0.040804048 3 -0.181279986 -0.10189900 -0.097218145 4 0.002307494 -0.01137235 -0.003585813 5 -0.047816198 -0.04932982 -0.009389939 6 0.030535090 0.02544292 0.017650949
The code for the plot is:
ggplot(data=df)+ stat_density(aes(x=R1, colour="rho = -0,6"), adjust=4, lwd=0.5, geom="line", position="identity")+ stat_density(aes(x=R2, colour="rho = 0,6"), adjust=4, lwd=0.5, geom="line", position="identity")+ stat_density(aes(x=R3, colour="rho = 0"), linetype=2, adjust=4, lwd=0.5, geom="line", position="identity")+ xlim(-0.5, 0.5)+ xlab("Renditen")+ ylab("Dichte")+ ggtitle("Renditeverteilung im Heston-Modell")+ theme(plot.title=element_text(face="bold", size=16, vjust=2), axis.title.x=element_text(vjust=-1, size=12), axis.title.y=element_text(vjust=-0.25, size=12), legend.text=element_text(size=12), legend.title=element_text(size=12), legend.margin=unit(1.5, "cm"), legend.key.height=unit(1.2, "line"), legend.key.size=unit(0.4, "cm"), legend.key=element_rect(fill=NA), legend.background=element_rect(colour="darkgrey"), plot.margin=unit(c(1,1,1,1), "cm"))+ scale_colour_manual(values=c("rho = -0,6"="red", "rho = 0,6"="blue", "rho = 0"="black"), name="Korrelation")
And finally the plot:
How can I get the legend to show a dotted line for the third density line (variable R3)?
Thank you in advance!
解决方案Put
linetype=
insideaes()
for eachstat_density()
with the same names as forcolors=
and then usescale_linetype_manual()
to set types as you need. If you use the same legend name for linetypes and colors, both legend will be put together.ggplot(data=df)+ stat_density(aes(x=R1, colour="rho = -0,6",linetype="rho = -0,6"), adjust=4, lwd=0.5, geom="line", position="identity")+ stat_density(aes(x=R2, colour="rho = 0,6",linetype="rho = 0,6"), adjust=4, lwd=0.5, geom="line", position="identity")+ stat_density(aes(x=R3, colour="rho = 0", linetype="rho = 0"), adjust=4, lwd=0.5, geom="line", position="identity")+ xlim(-0.5, 0.5)+ xlab("Renditen")+ ylab("Dichte")+ ggtitle("Renditeverteilung im Heston-Modell")+ theme(plot.title=element_text(face="bold", size=16, vjust=2), axis.title.x=element_text(vjust=-1, size=12), axis.title.y=element_text(vjust=-0.25, size=12), legend.text=element_text(size=12), legend.title=element_text(size=12), legend.margin=unit(1.5, "cm"), legend.key.height=unit(1.2, "line"), legend.key.size=unit(0.4, "cm"), legend.key=element_rect(fill=NA), legend.background=element_rect(colour="darkgrey"), plot.margin=unit(c(1,1,1,1), "cm"))+ scale_colour_manual(values=c("rho = -0,6"="red", "rho = 0,6"="blue", "rho = 0"="black"), name="Korrelation")+ scale_linetype_manual(values=c("rho = -0,6"=1, "rho = 0,6"=1, "rho = 0"=2), name="Korrelation")
这篇关于图例不显示ggplot2密度图中的线型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!