0

I want to plot several densities, where the legend should reveal the parameters for each density function. Unfortunately, ggplot2 does not include the legend (which is weird, in the tutorial it did...)

Generate data:

x <- seq(from=-5, to=5, by=0.1)
y1 = dlaplace(x,0,0.5)
y2 = dlaplace(x,0,1)
y3 = dlaplace(x,0,2)
df = data.frame(x,y1,y2,y3)

Plot

ggplot(data=df, aes(x=x))+
  geom_line(data= df, aes(y=y1), color="red")+
  geom_line(data= df,aes(y=y2), color="blue")+
  geom_line(data= df,aes(y=y3), color="green")+
  ggtitle("Gamma distribution density function") +ylab("density")+ xlab("x")+
  theme(legend.position = "bottom")+
  guides(fill = guide_legend(reverse=TRUE))

I am new to ggplot, and the following threads seemed related but unfortunately did not help me solve it (here and here)

safex
  • 2,398
  • 17
  • 40
  • 2
    Instead of adding three line layers to your plot you should reshape your data from wide to long, add a single `geom_line()` and then map the resulting "key" variable to the `color` aesthetic - this is done inside of `aes()`. You can find more infos here: [Plotting two variables as lines using ggplot2 on the same graph](https://stackoverflow.com/questions/3777174/plotting-two-variables-as-lines-using-ggplot2-on-the-same-graph) – markus Mar 13 '19 at 11:22
  • okay, but wasnt the point of `ggplot` to work with dataframes? I mean it should be able to handle wide, no? – safex Mar 13 '19 at 11:27
  • 2
    Possible duplicate of [Generating a legend in ggplot2](https://stackoverflow.com/questions/40573544/generating-a-legend-in-ggplot2) – jdobres Mar 13 '19 at 11:33
  • 1
    The point is to work with dataframes in long format. – erc Mar 13 '19 at 11:45
  • 1
    Yes, the point is to work with data frames, but what sets `ggplot2` apart is the "grammar of graphics" in its name. The general `ggplot` paradigm is that you're mapping variables to aesthetics, such as groups of observations mapped to colors. If you've followed tutorials, they probably all did things this way. – camille Mar 13 '19 at 13:16

1 Answers1

1

As Markus suggested, you'll need to convert your data to long format by meting it. Use melt function from reshape2. It should look something like this:

plotdf <- as.data.frame(t(df))
plotdf$var <- rownames(plotdf)
plotdf <- melt(plotdf[-c(1),], id.vars = "var")
print(ggplot(plotdf, aes(value, variable, colour = var)) + geom_point()+ scale_y_discrete(breaks=seq(0, 2, by = 0.5)) +
      ggtitle("Gamma distribution density function") +ylab("density")+ xlab("x")+
        theme(legend.position = "bottom")+
        guides(fill = guide_legend(reverse=TRUE)))

Output:

Here's the output plot

Can format the plot more using other ggplot features. Check this: How to melt R data.frame and plot group by bar plot

snair.stack
  • 405
  • 4
  • 13
  • what is the best way to change labels from y1 to say "\alpha = 1" – safex Mar 13 '19 at 13:50
  • Before melting the data, change the variable names as you want. Here, it'd be the new column 'var'. After crating it, replace 'y1' and then melt & plot. That should do it. – snair.stack Mar 14 '19 at 01:36