Position of y-axis when using facet_grid with option switch = "y"

20 views
Skip to first unread message

marcel.baum...@gmail.com

unread,
Feb 13, 2017, 2:24:00 AM2/13/17
to ggplot2
Dear all,

I am running R 3.3.2 on Windows (64bit), and ggplot2 2.2.0. Here is the plot I generate:

my.data <- structure(list(
    Category = c(
      "Very long Category Name",
      "Very long Category Name",
      "Very long Category Name",
      "Very long Category Name",
      "Category 2",
      "Category 2",
      "Category 2",
      "Category 2"
    ),
    x = c(1, 2, 3, 4,
          1, 2, 3, 4),
    y = c(10, 12, 14, 16, 15, 13, 11, 9)
  ), class = c("tbl_df",
               "tbl", "data.frame"), row.names = c(NA, -8L), .Names = c("Category", "x", "y"))

p <- ggplot(my.data, aes(x = x, y = y))
p <- p + geom_bar(stat = "identity")
p <- p + facet_grid(Category ~ ., switch = "y")
p <- p + theme_bw()
p <- p + theme(strip.text.y = element_text(angle = 180),
               strip.background = element_rect( fill = "white", colour = "white"))
print(p)

You can see that the y axis is to the left of the category names, which looks weird. In previous versions of ggplot2, when using the switch = "y" argument, the order was "Category Name" -> "Axis" -> "Plot".

Any idea how to switch the position of the y-axis with the category facet?

Thanks!

Marcel

marcel.baum...@gmail.com

unread,
Feb 13, 2017, 9:12:36 AM2/13/17
to ggplot2
Hadley provided the answer in my previous post:

ggplot(my.data, aes(x, y)) +

  geom_col() +

  facet_grid(Category ~ ., switch = "y") +

  theme(

    strip.text.y = element_text(angle = 180),

    strip.background = element_rect(fill = "white", colour = "white"),

    strip.placement = "outside"

  ) +

  labs(y = NULL)


Thanks!
Reply all
Reply to author
Forward
0 new messages