Scales = free works for facet_wrap but doesn't for facet_grid
Referring to this link:
facet_grid
split the data into facets by one or two variables that vary on the horizontal and/or vertical direction, while facet_wrap
places the facets next to each other, wrapping with a certain number of columns or rows. In other words, facet_wrap
only has horizontal dimension.
Therefore, using the example from that link, sp + facet_grid(. ~ sex)
would behave the same as sp + facet_grid( ~ sex)
. In your case, facet_grid(. ~ referencia)
and facet_wrap( ~ referencia)
should produce the same plot.
For two or more dimensional facets, facet_grid
produces a grid of plots based on the parameter (vertical ~ horizontal)
. facet_wrap
, on the other hand, would just stack the plots horizontally. User then can set the layout by specifying the number of columns or rows.
Now, when the scales = "free"
argument is added, facets in facet_grid
would still be bounded by the grid, therefore plots on the same row cannot have different y-axis. Similarly, there can only single x-axis for each column. Using facet_wrap
though, each plot is displayed independently, so it can "free" its x-axis and y-axis.
In my opinion, facet_grid
is useful when you want to relatively compare the plots within a category, which can be accomplished by setting the same axis scales. Meanwhile, facet_wrap
is more useful for plots that more independent between one another.
Looking to solve problem with facet_wrap/facet_grid that isn't resolved with space = free
You can get this easily with lemon::facet_rep_grid(repeat.tick.labels = T)
instead of ggplot2::facet_grid()
.
library(tidyverse)
library(lemon)
b <- structure(list(Race = c("Asian", "Asian", "Asian", "Asian", "Asian", "Asian", "Asian", "Asian", "Asian", "Asian", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black"), Symptom = structure(c(10L, 9L, 8L, 7L, 6L, 5L, 4L, 3L, 2L, 1L, 10L, 9L, 8L, 7L, 6L, 5L, 4L, 3L, 2L, 1L), .Label = c("Symptom10", "Symptom9", "Symptom8", "Symptom7", "Symptom6", "Symptom5", "Symptom4", "Symptom3", "Symptom2", "Symptom1"), class = "factor"), Percentage = c(60L, 50L, 20L, 70L, 90L, 100L, 10L, 30L, 40L, 60L, 40L, 20L, 50L, 50L, 85L, 30L, 30L, 20L, 80L, 40L), Group = c("Benign", "Benign", "Warning", "Warning", "Warning", "Fatal", "Fatal", "Fatal", "Fatal", "Fatal", "Benign", "Benign", "Warning", "Warning", "Warning", "Fatal", "Fatal", "Fatal", "Fatal", "Fatal")), row.names = c(NA, -20L), class = c("tbl_df", "tbl", "data.frame"))
ggplot(data=b, aes(x=Percentage, y= Symptom, fill = Race)) +
geom_bar(stat="identity", position=position_dodge()) +
facet_rep_grid(Group~., scales = "free", space = "free", repeat.tick.labels = T)
Created on 2022-01-18 by the reprex package (v2.0.1)
scales = free has no effect on facet_grid
facet_grid
enforces all facet plots to have the same y axis per row and the same x axis per column. This is why it's called a grid. You have to use facet_wrap(Season ~ year, scales = "free")
instead to allow having separate y axes for each facet.
facet_wrap(scales=free_y) not producing desired result
You'll have to remove this from your ggplot code
scale_y_continuous(limits=c(0, max(dat$y)))
because it is 'fixing' the range of y-axis across all the facets
How to change the facet scales using ggplot2?
An alternative is to use facet_wrap()
instead of facet_grid()
, e.g.
library(tidyverse)
Source <- c(rep("Water", 12), rep("Oil", 12))
Range <- rep((c(rep("First", 4), rep("Second", 8))),2)
Xaxis <- c(0,1,2,5,0,1,2,5,10,20,30,40,
0,1,2,5,0,1,2,5,10,20,30,40)
Yaxis <- c(0,1,2,5,0,1,2,5,10,20,30,40,
0,1,2,5,0,1,2,5,10,20,30,40)
DF <- data.frame(Source, Range, Xaxis, Yaxis)
ggplot(data = DF, aes(x = Xaxis, y = Yaxis)) +
geom_smooth(method = "lm") +
geom_point() +
facet_wrap(Range~Source,
scales = "free")
ggplot: create a facet grid with free scales
The dev/github version of ggh4x has facet_grid2()
which allows these independent axes within a grid layout. (Disclaimer: I'm the author of ggh4x.) If you find any bugs or unclear documentation, feel free to leave an issue on the github since I don't think this has been field-tested a lot.
library(ggplot2)
library(ggh4x) # devtools::install_github("teunbrand/ggh4x")
# Create data
nX <- 3
nY <- 3
nVal <- 2
df <- data.frame(M = sort(rep(paste0("M",1:nX), nVal * nY)),
n = rep(sort(paste0("n",rep((1:nY)-1, nVal))),nX),
G = rep(1:2,nX * nY),
val = c(100,300,20,10,1,2,10,25,NA,NA,0.1,0.2,25,27,2,5,0.5,0.6))
# Delete observations to resemble my data
df <- subset(df, !is.na(val))
# scales does not work in facet_grid(), thus obscuring trends for low values
ggplot(df, aes(x = G,
y = val)) +
geom_line()+
ylim(0,NA)+
facet_grid2(n ~ M,scales = "free_y", independent = "y")
Created on 2021-06-09 by the reprex package (v1.0.0)
ggplot2, facet_grid, free scales?
Perhaps it's because you have only one y axis, using your way. Did you try something like this?
mt + facet_grid(cyl ~ ., scales="free")
Facet_grid with all panels with a free_y
facet_wrap does this, and is what I use, although it does mean you have the faceting value as a title for each individual facet, which can be a pain.
ggplot(mpg, aes(displ, hwy, colour = as.factor(cyl))) +
geom_point() +
facet_wrap(year ~ drv,
scales = "free_y")
Understanding facet_grid scale=free
Instead of facet_grid
use facet_wrap
for example,
facet_wrap(reformulate("Name","."), scales = 'free', nrow = 1) +
With facet_grid
one can not get both x and y scales free; see here https://github.com/tidyverse/ggplot2/issues/1152
Related Topics
How Do We Plot Images at Given Coordinates in R
Using Functions and Environments
Importing Multiple Excel Files with Filenames in R
Locator Equivalent in Ggplot2 (For Maps)
R: Replacing Nas in a Data.Frame with Values in the Same Position in Another Dataframe
How to Efficiently Read the First Character from Each Line of a Text File
Error in Get(As.Character(Fun), Mode = "Function", Envir = Envir)
Displaying Image on Point Hover in Plotly
Extracting Orthogonal Polynomial Coefficients from R's Poly() Function
Boxplot, How to Match Outliers' Color to Fill Aesthetics
Remove Certain Legend Variables and Legend Values from Ggplot2
Replace Nan Values in a List with Zero (0)
Extracting Data Used to Make a Smooth Plot in Mgcv
Understanding Ddply Error Message - Argument "By" Is Missing, with No Default