Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add vline to existing plot and have it appear in ggplot2 legend?

Tags:

r

ggplot2

I have some data that I am using to plot a histogram. I also have two sets of thresholds that have some significance.

I am able to plot the histogram and the vlines with the appropriate styles. However, I cannot get my vlines to show up in the legend. I believe that something like this should work, however the legend items don't ever display.

df <- data.frame(val=rnorm(300, 75, 10))

cuts1 <- c(43, 70, 90)
cuts2 <- c(46, 79, 86)

ggplot(data=df, aes(x=val)) +
  geom_histogram() +
  geom_vline(xintercept=cuts1,
             linetype=1,
             color="red",
             labels="Thresholds A",
             show_guide=TRUE) +
  geom_vline(xintercept=cuts2,
             linetype=2,
             color="green",
             labels="Thresholds B",
             show_guide=TRUE)

Alternatively, if I construct a data.frame for my cuts and do an aesthetic mapping, I can get my vlines to show up in the legend. Unfortunately, the legend gives me two instances of the different line types superimposed on each other:

cuts1 <- data.frame(Thresholds="Thresholds A", vals=c(43, 70, 90))
cuts2 <- data.frame(Thresholds="Thresholds B", vals=cuts2 <- c(46, 79, 86))

ggplot(data=df, aes(x=val)) +
  geom_histogram() +
  geom_vline(data=cuts1, aes(xintercept=vals, shape=Thresholds),
             linetype=1,
             color="red",
             labels="Thresholds A",
             show_guide=TRUE) +
  geom_vline(data=cuts2, aes(xintercept=vals, shape=Thresholds),
             linetype=2,
             color="green",
             labels="Thresholds B",
             show_guide=TRUE)

enter image description here

So, in the end, what I'm looking for, is the most straightforward way to manually add two sets of lines to a plot, and then have them appear correctly in the legend.

like image 786
Peter Avatar asked Sep 22 '12 15:09

Peter


People also ask

How to add vertical lines to a ggplot2 plot?

You can quickly add vertical lines to ggplot2 plots using the geom_vline () function, which uses the following syntax: xintercept: Location to add line on the x-intercept. This can be one value or multiple values.

How to add a manual legend to a plot in ggplot2?

Often you may want to add a manual legend to a plot in ggplot2 with custom colors, labels, title, etc. Fortunately this is simple to do using the scale_color_manual () function and the following example shows how to do so.

How do you add legends to a graph?

So, to add legends we need to distribute the lines into multiple groups on the basis of coloring. The key idea is to differentiate the lines by assigning different colors to each line and make them into separate groups.

How to add legends to multiple lines in R?

There is no direct way in R to add legends in case of multiple lines like in Excel and other scripting languages. So, to add legends we need to distribute the lines into multiple groups on the basis of coloring.


1 Answers

The trick is to put the threshold data all in the same data frame, and then map aesthetics, rather than set them:

cuts <- rbind(cuts1,cuts2)

ggplot(data=df, aes(x=val)) +
  geom_histogram() +
  geom_vline(data=cuts, 
             aes(xintercept=vals, 
                 linetype=Thresholds,
                 colour = Thresholds),
             show_guide = TRUE)

enter image description here

like image 161
joran Avatar answered Oct 16 '22 14:10

joran