I am creating a graph divided into facets. Some of these facets need to be on a smaller scale for better viewing. I have already applied several tricks to achieve this. However, I need to change the limits and divisions of the axes of some facets.
Calling the necessary packages, creating the database and making the plot
devtools::install_github("zeehio/facetscales")
library(ggplot2)
library(facetscales)
#Constructing data frame
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)
#plot
p <- ggplot(data = DF, aes(x = Xaxis, y = Yaxis)) +
geom_smooth(method = "lm",
formula = y ~ x) +
geom_point()
I initially used facet_wrap
with scales = "free"
, but what I really want is to be able to modify the limits and divisions of the x-axis of the facets in the "First"
row.
#plot using facet wrap
p + facet_wrap(Range~Source,
scales = "free") +
ggtitle("Using facet_wrap")
So I chose to use the facetscales
package and followed this (1) and this (2) examples
#axis specifications
scale_x <- list(
"First" = scale_x_continuous(limits = c(0, 10), breaks = c(0, 2, 10)),
"Second"= scale_x_continuous(limits = c(0, 40), breaks = c(0, 5, 40))
)
#plot using facet grid and the facetscales package
p + facet_grid_sc(rows = vars(Range),
cols = vars (Source),
scales = list(x = scale_x)) +
ggtitle("Using facet_grid_sc")
When trying to make the graph with the specifications that I need appears:
error in x$clone() attempt to apply non-function
I don't understand what I'm doing wrong. Could someone help me, please?