3

My data set is as follows

dput(data2)
structure(list(School = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 
3L, 3L), .Label = c("School1", "School2", "School3"), class = "factor"), 
    Year = c(2015L, 2014L, 2013L, 2015L, 2014L, 2013L, 2015L, 
    2014L, 2013L), Rate = c(70L, 50L, 30L, 80L, 90L, 11L, 60L, 
    50L, 40L)), .Names = c("School", "Year", "Rate"), class = "data.frame", row.names = c(NA, 
-9L))


   School Year Rate
1 School1 2015   70
2 School1 2014   50
3 School1 2013   30
4 School2 2015   80
5 School2 2014   90
6 School2 2013   11
7 School3 2015   60
8 School3 2014   50
9 School3 2013   40

I plot this data using ggplot2 as follows

library(ggplot2)
ggplot(data=data2,aes(x=School,y=Rate)) +
  geom_bar(stat = "identity", fill="orange",width = 0.5) + 
  geom_hline(aes(yintercept = 220,color="red"), size = 1) +
  coord_flip() 

which gives me the following chart and I am trying to rotate the line in the legend as follows enter image description here

I have read posts on Stack Overflow which mentioned that the lines in the legends are shown vertically but that only happens when you use geom_linerange but I cannot use that for my example.

Please can someone help me understand how I can rotate the line within the legend.

One alternative that has come to mind to navigate the viewports using the grid package and then see whether I am able to rotate the viewport used by the legend key.

Vikram
  • 51
  • 2
  • 7
  • Ggplot2 has a lot of awesome add-ins you can use in r-studio and make your life way easier. [Here](http://www.ggplot2-exts.org/gallery/) you can find various add-ins, each one is well optimized for a specific need. In particular, in your case, I think [ggedit](https://github.com/metrumresearchgroup/ggedit) is your Thor hammer. It helps you edit ggplot graphs with a WYSIWYG tool, and you will save time in your next tries. Hope this helps! Goodluck! – Moctar Haiz Jun 03 '18 at 10:02

2 Answers2

3

If your actual plot has more complex legend requirements based on different geoms, the answers here may be helpful: How to rotate legend symbols in ggplot2?

That said, if you just need something for a geom_hline layer, a legend associated with geom_vline would be sufficient to fake it:

ggplot(data = data2, aes(x = School, y = Rate)) +
  geom_col(fill = "orange", width = 0.5) + 

  # do not specify color within aes() for geom_hline
  geom_hline(aes(yintercept = 220), color="red", size = 1) +

  # specify color within aes() for geom_vline instead, but don't let the line
  # be invisible with alpha = 0
  geom_vline(aes(xintercept = 1, color = "legend.label"), alpha = 0) +

  # set up the color legend as per normal. it's based on an invisible geom_vline,
  # but is visible with alpha = 1 & size = 1
  scale_color_manual(
    name = "legend.title",
    values = "red",
    guide = guide_legend(override.aes = list(alpha = 1, size = 1))) +

  coord_flip()

plot

Incidentally, geom_col() is equivalent to geom_bar(stat = "identity"), & looks less cluttered.

Z.Lin
  • 28,055
  • 6
  • 54
  • 94
2

There is a package that implements horizontal version of common ggplot2 Geoms, Stats, and Positions : ggstance

You can use that to avoid the coord_flip of all the plot and use horizontal geom bar instead with x and y as you want it. That way you can use geom_vline that has a vertical legend.

data2 <- tibble::tribble(
  ~School, ~Year, ~Rate,
  "School1", 2015L,   70L,
  "School1", 2014L,   50L,
  "School1", 2013L,   30L,
  "School2", 2015L,   80L,
  "School2", 2014L,   90L,
  "School2", 2013L,   11L,
  "School3", 2015L,   60L,
  "School3", 2014L,   50L,
  "School3", 2013L,   40L
)

library(ggplot2)
library(ggstance) # devtools::install_github("lionel-/ggstance")
#> 
#> Attachement du package : 'ggstance'
#> The following objects are masked from 'package:ggplot2':
#> 
#>     geom_errorbarh, GeomErrorbarh
ggplot(data=data2,aes(x=Rate,y=School)) +
  geom_barh(stat = "identity", fill="orange",width = 0.5) + 
  geom_vline(aes(xintercept = 220,color="red"), size = 1)

Created on 2018-06-03 by the reprex package (v0.2.0).

cderv
  • 6,272
  • 1
  • 21
  • 31
  • Thanks a lot. @Z.Lin - your answer was quite smart, I tried but it messed up some other legends in my chart. Moctair - your answer is going to save my life. I have will combine all the above suggestions and that should solve my problem. After trying some of the answers here, I keep running into this bug https://github.com/tidyverse/ggplot2/issues/2483 and now trying to find my way around it. – Vikram Jun 03 '18 at 22:46
  • Oh! wonderful solution after all the morning looking for how to make slash rightly oriented. Thank you very much! – iago Oct 09 '19 at 11:03