4

Here goes my first SO question:

Using ggplot2

  • I have a horizontal histogram made with flipped axis coordinates (coord_flip)
  • I've added vertical ablines using geom_vline (also flipped to be horizontal)
  • The resulting legend for the ablines shows vertical lines in the key

Q: Is it possible to have horizontal lines in the legend key?

enter image description here

library(ggplot2)

dat <- data.frame(vs=c(0,0,1,1),
                  stat = c("25%", "75%", "25%", "75%"),
                  value = c(quantile(mtcars$disp[mtcars$vs==0],0.25),
                            quantile(mtcars$disp[mtcars$vs==0],0.75),
                            quantile(mtcars$disp[mtcars$vs==1],0.25),
                            quantile(mtcars$disp[mtcars$vs==1], 0.75)))

p <- ggplot(mtcars, aes(disp)) +
coord_flip() +
facet_grid(vs~.) +
geom_histogram(aes(y = ..density..), stat="bin") +
geom_vline(mapping=aes(xintercept=value, colour=stat), linetype="solid", size=0.6, alpha=0.7, data=dat) +
guides(col = guide_legend(nrow = 2, title="Quantile")) +
theme(legend.position = "bottom")

p
Roman Luštrik
  • 69,533
  • 24
  • 154
  • 197
JaimieC
  • 43
  • 3
  • 2
    Did you try [this](http://stackoverflow.com/questions/40229263/how-to-rotate-legend-symbols-in-ggplot2)? – Haboryme Jan 04 '17 at 09:46

0 Answers0