pass string to facet_grid : ggplot2
reformulate()
seems to work just fine.
FUN <- function(data, x, y, fac1, fac2) {
ggplot(data = data, aes_string(x=x, y=y)) +
geom_point() + facet_grid(reformulate(fac2,fac1))
}
FUN(mtcars, 'hp', 'mpg', 'cyl', 'am')
Why passing a string in R ggplot2::facet_grid() works for rows but not for columns?
I think @Limey's explanation in the comments is the answer to your question, but if you're looking for a practical solution (outside of those you've linked) you can turn the string into a symbol (using sym()
) then pass it to vars with the bang-bang operator, e.g.
library(tidyverse)
f_varname <- sym("cyl")
ggplot(mpg, aes(displ, cty)) +
geom_point() +
facet_grid(cols = vars(!!f_varname))
Passing string variable facet_wrap() in ggplot using R
(Turning @kohske's comment into an answer so that it can be accepted and "closed"):
facet_wrap(as.formula(paste("~", response)))
How to pass a list of arguments to facet_grid()
You just forgot to name the second argument so you passed it to margin
instead of passing it to scales
(and you need double brackets for the argument to be a vector):
facet_plot <- function(facet.args){
ggplot(iris, aes(x = Sepal.Length, y = Petal.Length)) +
geom_point() +
facet_grid(paste0('~', facet.args$facets), scales= facet.args[[which(names(facet.args) != 'facets')]])
}
facet_plot(list(facets = 'Species', scales = 'free_x'))
To be more general you could use do.call
:
facet_plot <- function(facet.args){
facet.args$facets <- paste0('~', facet.args$facets)
ggplot(iris, aes(x = Sepal.Length, y = Petal.Length)) +
geom_point() +
do.call(facet_grid,facet.args)
}
facet_plot(list(facets = 'Species', scales = 'free_x'))
R Passing arguments for ggplot through a function call with facet_wrap and data subsetting
In order to pass character strings as variable into ggplot
, you need to make some changes in my.plot
part before wrapping it into a function.
For subset the dataset, you need to pass the names of the column with [[ ]]
in order to make it work. For the definition of x
and y
, you can use aes_string
(https://ggplot2.tidyverse.org/reference/aes_.html). Finally, for facetting, pass your character vector as a formula (as explained in this post: Passing string variable facet_wrap() in ggplot using R).
my.plot =
ggplot(subset(data, data[[subSetVar]] %in% subSetval)) +
geom_line(aes_string(x=xVar, y=yVar)) +
facet_wrap(as.formula(paste("~", facetVar)), ncol=3) +
labs(x = xLabel, y = yLabel) +
My_Theme
Then, it should work and get you the same graph :
my.plot <- makePlots(DF, "variable", "ux1", "Time", "value", "model",
"Time [s]", expression(paste("U"[X],","[1])))
Does it answer your question ?
Pass variable columns with optional facets in a function via ggplot in R
Using aes_string
will allow you to pass character strings into your ggplot2 function, allowing you to programmatically change it more easily:
my.plot = function(x, y, data)
{
p=ggplot(data, aes_string(x=x, y=y))+geom_bar(stat="identity")
print(p)
}
my.plot(x="xVar", y="yVar", data=dataTest)
my.plot(x="xVar", y="zVar", data=dataTest)
pass character strings to ggplot2 within a function
It is now recommended to use .data
pronoun
FUN <- function(dat, x, y) {
ggplot(dat, aes(x = .data[[x]], y = .data[[y]])) +
geom_point()
}
FUN(mtcars, "mpg", "hp")
Couple of other alternatives -
#Using get
FUN <- function(dat, x, y) {
ggplot(dat, aes(x = get(x), y = get(y))) +
geom_point()
}
#Using sym
FUN <- function(dat, x, y) {
ggplot(dat, aes(x = !!sym(x), y = !!sym(y))) +
geom_point()
}
How to change facet labels?
Change the underlying factor level names with something like:
# Using the Iris data
> i <- iris
> levels(i$Species)
[1] "setosa" "versicolor" "virginica"
> levels(i$Species) <- c("S", "Ve", "Vi")
> ggplot(i, aes(Petal.Length)) + stat_bin() + facet_grid(Species ~ .)
Related Topics
Rank Variable by Group (Dplyr)
How to Set Seed for Random Simulations with Foreach and Domc Packages
Shiny Saving Url State Subpages and Tabs
Error with Ggplot2 Mapping Variable to Y and Using Stat="Bin"
Ggplot Graphing of Proportions of Observations Within Categories
How to Use Plyr to Number Rows
Initialize an Empty Tibble with Column Names and 0 Rows
Note in R Cran Check: No Repository Set, So Cyclic Dependency Check Skipped
How to Create a Raster from a Data Frame in R
How to Rank Within Groups in R
How to Do a Regression of a Series of Variables Without Typing Each Variable Name
Add a Page Refresh Button by Using R Shiny