【问题标题】:Legend does not show line type in ggplot2 density plot图例在 ggplot2 密度图中不显示线型
【发布时间】:2013-12-21 02:40:15
【问题描述】:

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

数据如下:

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

最后是剧情:

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

提前谢谢你!

【问题讨论】:

标签: r ggplot2 legend-properties density-plot


【解决方案1】:

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

【讨论】:

  • 完美答案,正是我需要的!谢谢!
  • 除了对颜色和线型使用相同的名称外,还应添加参数:name="Korrelation"
【解决方案2】:

规则很简单:

 each aes is key in the legend.

由于您的线型不在 aes 中,因此它们不会显示在图例中。

这里是一个例子:

library(ggplot2)

ggplot(mtcars) +
  geom_line(aes(x=mpg,y=cyl,linetype='2')) +
  geom_line(aes(x=mpg,y=disp,linetype='3')) + 
  scale_linetype_discrete(name = "Lines Types  aes")

【讨论】:

    猜你喜欢
    • 2023-02-17
    • 1970-01-01
    • 1970-01-01
    • 2015-06-16
    • 1970-01-01
    • 2013-12-25
    • 2020-05-08
    • 2020-06-26
    • 1970-01-01
    相关资源
    最近更新 更多