Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a label for a vertical line with legend in ggplot2

R 3.1.2/ggplot2_1.0.0/Windows7

How can one add 2 vertical lines with legends in a faceted graph without them changing the linetype? In the following example i can't get the legends to appear as I would imagine them to (two solid lines and a adecuate legend) from the code I'm writing. A reproducible example:

library(ggplot2)
library(plyr)
library(e1071)

set.seed(89)
pm <- data.frame(pm10=rnorm(400, 150, 50), estacion=gl(4,100, labels = c('sur', 'norte', 'este', 'oeste')))   # data
curtosis <- ddply(pm, .(estacion), function(val) sprintf("curtosis==%.2f", kurtosis(val$pm10)))
asimetria <- ddply(pm, .(estacion), function(val) sprintf("asimetría==%.2f", skewness(val$pm10)))

p1 <- ggplot(data=pm, aes(x=pm10, y=..density..)) +
  geom_histogram(bin=15, fill='#deebf7', colour='#bdbdbd')+
  geom_density(size=1, colour='#cccccc')+
  geom_vline(data=aggregate(pm[1], pm[2], quantile, .8), mapping=aes(xintercept=pm10, linetype='percentil .8'), size=1, colour='#dfc27d', show_guide = T)+
  geom_vline(data=aggregate(pm[1], pm[2], median), mapping=aes(xintercept=pm10, linetype='mediana'), size=1, colour='#80cdc1', show_guide = T)+
  geom_text(data=curtosis, aes(x=350, y=.010, label=V1), size=3, parse=T)+
  geom_text(data=asimetria, aes(x=350, y=.008, label=V1), size=3, parse=T)+
  guides(linetype=guide_legend(override.aes=list(colour = c("#dfc27d","#80cdc1"))))+
  xlim(0,500)+
  facet_wrap(~ estacion, ncol=2) 

print(p1)

enter image description here

I want the lines to be solid (color is ok) and the legend's title to say: "Medida de tendencia".

like image 200
Panchito Avatar asked Apr 17 '15 22:04

Panchito


People also ask

How do I add a vertical line in ggplot2?

To create a vertical line using ggplot2, we can use geom_vline function of ggplot2 package and if we want to have a wide vertical line with different color then lwd and colour argument will be used. The lwd argument will increase the width of the line and obviously colour argument will change the color.

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 horizontal line in ggplot2?

Example: To add the horizontal line on the plot, we simply add geom_hline() function to ggplot2() function and pass the yintercept, which basically has a location on the Y axis, where we actually want to create a vertical line.


1 Answers

The main idea for rather compicated ggplots like yours is to separate the data preparation from actual plotting. Within the data preparation step you can organize your data according to the plotting concept you have in mind.

In your case this implies precomputing the statistic in question:

df_vline <- rbind(
  aggregate(pm[1], pm[2], quantile, .8), 
  aggregate(pm[1], pm[2], median)
)
df_vline$stat <- rep(c("percentil .8", "mediana"), each = nrow(df_vline) / 2)

and then the mapping scheme is quite straightforward, so you don't have to think about manual overriding of the legend.

ggplot(data=pm, aes(x=pm10, y=..density..)) +
  geom_histogram(bin=15, fill='#deebf7', colour='#bdbdbd')+
  geom_density(size=1, colour='#cccccc')+
  geom_vline(data=df_vline, mapping=aes(xintercept=pm10, colour = stat), 
             linetype = 1, size=1, show_guide = T)+
  geom_text(data=curtosis, aes(x=350, y=.010, label=V1), size=3, parse=T)+
  geom_text(data=asimetria, aes(x=350, y=.008, label=V1), size=3, parse=T)+
  scale_colour_manual(values = c("#dfc27d","#80cdc1"), name = "Medida de tendencia")+
  xlim(0,500)+
  facet_wrap(~ estacion, ncol=2) 

enter image description here

(Nice plot, by the way.)

like image 181
tonytonov Avatar answered Oct 10 '22 10:10

tonytonov