Rotate switched facet labels in ggplot2 facet_grid

前端 未结 1 1993
孤独总比滥情好
孤独总比滥情好 2021-02-01 13:07

I would like to plot some barplots on top of each other using facet_grid:

library(ggplot2)

df <- group_by(mpg, manufacturer) %>%
  summarise(cty = mean(ct         


        
相关标签:
1条回答
  • 2021-02-01 13:49

    You just need to add the theme() and specify the angle in strip.text.y.left.

    library(tidyverse)
    library(reshape2)
    
    df <- group_by(mpg, manufacturer) %>%
      summarise(cty = mean(cty), hwy = mean(hwy)) %>%
      ungroup()
    
    df <- melt(df, id.vars = "manufacturer")
    
    ggplot() +
      geom_bar(data =df, aes(x = variable, y = value), stat = "identity") +
      facet_grid(manufacturer ~ ., switch = "y")+
      theme(strip.text.y.left = element_text(angle = 0))
    

    Created on 2020-03-15 by the reprex package (v0.3.0)

    Note that strip.text.y.left was added in ggplot2 3.3.0. For earlier versions, you need to write strip.text.y = element_text(angle = 180).

    0 讨论(0)
提交回复
热议问题