Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Legend does not show line type in ggplot2 density plot

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:

enter image description here

How can I get the legend to show a dotted line for the third density line (variable R3)?

Thank you in advance!

like image 575
FreshF Avatar asked Dec 04 '13 14:12

FreshF


People also ask

How do I change the legend text in ggplot2?

You can use the following syntax to change the legend labels in ggplot2: p + scale_fill_discrete(labels=c('label1', 'label2', 'label3', ...))

How do I add a legend in ggplot2?

You can place the legend literally anywhere. To put it around the chart, use the legend. position option and specify top , right , bottom , or left . To put it inside the plot area, specify a vector of length 2, both values going between 0 and 1 and giving the x and y coordinates.

What is Ggplot density?

10 mins. Data Visualization using GGPlot2. A density plot is an alternative to Histogram used for visualizing the distribution of a continuous variable. The peaks of a Density Plot help to identify where values are concentrated over the interval of the continuous variable.

How do I get rid of Ggplot legend?

Example 1: Remove All Legends in ggplot2 We simply had to specify legend. position = “none” within the theme options to get rid of both legends.


2 Answers

Put linetype= inside aes() for each stat_density() with the same names as for colors= and then use scale_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")

enter image description here

like image 139
Didzis Elferts Avatar answered Sep 25 '22 13:09

Didzis Elferts


The rule is simple:

 each aes is key in the legend.

Since your linetype's are not in the aes , they are not showed in the legend.

Here an example:

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

enter image description here

like image 36
agstudy Avatar answered Sep 24 '22 13:09

agstudy