Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you want to expand the y-limits for a plot with faceting in ggplot2, you could do the following approach:</p> <pre><code>library(ggplot2) library(reshape2) set.seed(1) N &lt;- 24 dates = seq(as.Date("2014-01-01"), as.Date("2015-12-01"), by = "1 month") dummy_data &lt;- data.frame(dates = dates, x = rnorm(N,0,1), group = sample(c("A", "B", "C"), size = N, replace = TRUE)) dummy_data_m &lt;- melt(dummy_data, id.vars = c("dates", "group")) plot = ggplot(data = dummy_data_m, aes(x = dates, y = value, colour = variable)) + geom_point() + facet_grid(group ~., scales="free_y") print(plot) # Want to modify y-axis on this plot for each panel # to expand plot axes, build the y ranges in a data.frame and add a new layer to the plot using geom_blank # pick an x.value in the range of your x-values in dummy_data x.value = as.Date("2014-01-01") x.value = as.Date("2014-01-01") # Say you want to expand the y-axis ranges for the different subpanels to be (-5, 5), (-4, 4), (-2, 2). # If you simply plot at this point the y limits are roughly ~(-1.5, 1.5) for each plot lower_y = data.frame(dates = x.value, group = c("A", "B", "C"), value = c(5, 4, 2)) y_ranges = rbind(lower_y, upper_y) y_ranges_m = melt(y_ranges, id.vars = c("dates", "group")) plot = plot + geom_blank(data = y_ranges_m, aes(x = dates, y = value, colour = variable)) print(plot) </code></pre> <p>For the opposite case, if you want to decrease the y range, then remove observations from the data in the panels for which you want to shrink the y-axis. For example (instead of expanding the y-axis as above):</p> <pre><code># do not want to plot any values where y &lt; 0 for group A dummy_data2 = dummy_data x_adjusted = dummy_data2$x x_adjusted[x_adjusted &lt; 0 &amp; dummy_data2$group == "A"] = NA dummy_data2$x = x_adjusted dummy_data_m2 &lt;- melt(dummy_data2, id.vars = c("dates", "group")) plot = ggplot(data = dummy_data_m2, aes(x = dates, y = value, colour = variable)) + geom_point() + facet_grid(group ~., scales="free_y") print(plot) # this will show a plot where the panel for A has a y-axis lower limit above 0 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload