r - 图例在 ggplot2 密度图中不显示线型

标签 r ggplot2 legend-properties density-plot

我从一个包含 3 个变量的数据框中创建了一个带有 ggplot 的密度图。一条密度线是虚线,但图例显示了这条线的实线。

数据如下所示:

> 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

情节的代码是:
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")

最后是情节:

enter image description here

如何让图例显示第三条密度线(变量 R3)的虚线?

先感谢您!

最佳答案

linetype=aes()每个stat_density()colors= 同名然后使用 scale_linetype_manual()根据需要设置类型。如果线型和颜色使用相同的图例名称,则两个图例将放在一起。

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")

enter image description here

关于r - 图例在 ggplot2 密度图中不显示线型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20378276/

相关文章:

r - 不规则时间序列数据 - 我可以让它规则吗?在 r

r - 如何在指定值的线图中添加阴影置信区间

r - 我如何使用 ggplot() 来创建这个图?

r - 如何理解哪个图例是哪个图例并在 R 的 ggplot 中删除其中一个?

javascript - 单击图例中的项目时如何禁用 ZingChart 中的栏消失

R tryCatch 处理一种错误

r - 将 data.frame 对象转换为 Zoo 对象

r - 在 Shiny 的文档中显示 ggvis 绘图

r - 在 ggplot 方面添加标签

matlab - 传奇;键/颜色之前的文本/描述?