Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot2: How to swap y-axis and right facet strip and how to order facet values

Tags:

r

ggplot2

With the following code:

library(ggplot2)
set.seed(6809)
diamonds <- diamonds[sample(nrow(diamonds), 1000), ]
diamonds$cut <- factor(diamonds$cut,
         levels = c("Ideal", "Very Good", "Fair", "Good", "Premium"))

# Repeat first example with new order
p <- ggplot(diamonds, aes(carat, ..density..)) +
        geom_histogram(binwidth = 1)
p + facet_grid(color ~ cut)

I can create the following figure:

enter image description here

My questions are:

  1. How can I sort the right strip according to my desired order e.g. (G, F, D, E, I, J, H)?
  2. How can I swap the right strip to with the y-axis (density)?
like image 439
neversaint Avatar asked Apr 08 '15 02:04

neversaint


People also ask

How do I rearrange facets in ggplot2?

To reorder the facets accordingly of the given ggplot2 plot, the user needs to reorder the levels of our grouping variable accordingly with the help of the levels function and required parameter passed into it, further it will lead to the reordering of the facets accordingly in the R programming language.

How do you change a facet label?

Facet labels can be modified using the option labeller , which should be a function. In the following R code, facets are labelled by combining the name of the grouping variable with group levels. The labeller function label_both is used.

Can you Facet_wrap by two variables?

facet_wrap() with two variables Compute the counts for the plot so we have two variables to use in faceting: marvel_count <- count(marvel, year, align, gender) glimpse(marvel_count) ## Observations: 155 ## Variables: 4 ## $ year <dbl> 1939, 1939, 1940, 1940, 1940, 1941, 1941, 1943, 1944, 19...

What is the function of Facet_grid () in Ggplot ()?

facet_grid() forms a matrix of panels defined by row and column faceting variables. It is most useful when you have two discrete variables, and all combinations of the variables exist in the data. If you have only one variable with many levels, try facet_wrap() .


1 Answers

Update for ggplot2 2.2.1

With ggplot2 version 2, you can switch the positions of the axis labels and facet labels. So here's updated code that takes advantage of these features:

# Reorder factor levels
diamonds$color = factor(diamonds$color, levels=c("G","F","D","E","I","J","H"))

ggplot(diamonds, aes(carat, ..density..)) +
  geom_histogram(binwidth=1) +
  facet_grid(color ~ cut, switch="y") +   # Put the y facet strips on the left
  scale_y_continuous("density", position="right") +   # Put the y-axis labels on the right
  theme(strip.text.y=element_text(angle=180))

enter image description here

Original Answer

As @joran said, you have to modify the grid object if you want full control over what goes where. That's painful.

Here's another approach that's still a hassle, but easier (for me at least) than modifying the grid object. The basic idea is that we orient the various facet and axis labels so that we can rotate the plot 90 degrees counter-clockwise (to get the facet labels on the left side) while still having all the labels oriented properly.

To make this work, you need to modify the graph in several ways: Note my addition of coord_flip, all the theme stuff, and scale_x_reverse. Note also that I've switched the order of the facet variables, so that color starts out on top (it will be on the left after we rotate the graph).

# Reorder factor levels
diamonds$color = factor(diamonds$color, levels=rev(c("G","F","D","E","I","J","H")))

p <- ggplot(diamonds, aes(carat, ..density..)) +
  geom_histogram(binwidth = 1) +
  facet_grid(cut ~ color) + coord_flip() +
  theme(strip.text.x=element_text(angle=-90),
        axis.text.y=element_text(angle=-90, vjust=0.5, hjust=0.5),
        axis.text.x=element_text(angle=-90, vjust=0.5, hjust=0),
        axis.title.x=element_text(angle=180),
        axis.title.y=element_text(angle=-90)) +
  scale_x_reverse()

One option is to save the graph and then rotate it in another program (such as Preview, if you're on a Mac). However, with the help of this SO answer, I was able to rotate the plot within R. It required some trial and error (with my limited knowledge of how to manipulate grid objects) to get the right size for the viewport. I saved it as a PNG for posting on SO, but you can of course save it as a PDF, which will look nicer.

png("example.png", 500,600)
pushViewport(viewport(width = unit(8, "inches"), height = unit(7, "inches"))) 
print(p, vp=viewport(angle=90))
dev.off()

And here's the result:

enter image description here

like image 81
eipi10 Avatar answered Oct 27 '22 07:10

eipi10