Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting individual y axis limits with facet wrap NOT with scales free_y

Tags:

r

ggplot2

I have this data

library(ggplot2)

dat = data.frame(x = c(1,2,1,2), 
                 group = c("a","a","b","b"), 
                 y = c(10,20,1000,2000))

ggplot(dat, aes(x = x, y = y)) + 
    geom_point() + 
    geom_line() + 
    facet_wrap(~group, ncol = 1) +
    coord_cartesian(ylim = c(0, 30))

You can see the B group does not show up because I set the y limit to 0,30. I want to manually set the individual y limits for each chart. I do NOT want to use scales = "free_y" because I need control over the limits in each chart.

Is there a way this can be done? Can you somehow supply y limits for each chart in a facet wrap?

like image 943
user3022875 Avatar asked Mar 03 '17 20:03

user3022875


People also ask

How do I limit the y axis in R?

To change the axis scales on a plot in base R Language, we can use the xlim() and ylim() functions. The xlim() and ylim() functions are convenience functions that set the limit of the x-axis and y-axis respectively.

How do I change the scale of Y in ggplot2?

Use scale_xx() functions It is also possible to use the functions scale_x_continuous() and scale_y_continuous() to change x and y axis limits, respectively.

What is facet wrap in ggplot2?

facet_wrap() makes a long ribbon of panels (generated by any number of variables) and wraps it into 2d. This is useful if you have a single variable with many levels and want to arrange the plots in a more space efficient manner. You can control how the ribbon is wrapped into a grid with ncol , nrow , as.

What is the difference between Facet_wrap and Facet_grid?

The facet_grid() function will produce a grid of plots for each combination of variables that you specify, even if some plots are empty. The facet_wrap() function will only produce plots for the combinations of variables that have values, which means it won't produce any empty plots.


1 Answers

Unless you want to decrease your plotting area (i.e. not plot some points), you can still have "full" control over your y limits while using scales = "free_y".

You can use the same trick I have given to answer your other question: how to set limits on rounded facet wrap y axis?

dat <- data.table(dat)

dat[,y_min := y*0.5, by = group]
dat[,y_max:= y*1.5, by = group]

ggplot(dat, aes(x = x, y = y)) + 
  geom_point() + 
  geom_line() + 
  facet_wrap(~group, ncol = 1, scales = "free_y") +
  geom_blank(aes(y = y_min)) +
  geom_blank(aes(y = y_max))

For others reading this question, trick is to explicitly create y_min and y_max variables for each group. And "plot" them via geom_blank(). (Nothing is actually plotted, but each facet's plotting area is adjusted based on y_min and y_max values for that group).

If for some reasons, you want to manually give min and max (instead of a rule), none is stopping you. But it is tedious:

dat[group == "a",y_min := 0]
dat[group == "a",y_max := 30]
dat[group == "b",y_min := 0]
dat[group == "b",y_max := 3000]

ggplot(dat, aes(x = x, y = y)) + 
  geom_point() + 
  geom_line() + 
  facet_wrap(~group, ncol = 1, scales = "free_y") +
  geom_blank(aes(y = y_min)) +
  geom_blank(aes(y = y_max))

But, as I have mentioned this works if you want to extend your limits, not decrease them. enter image description here

like image 111
Jav Avatar answered Sep 28 '22 04:09

Jav