I'm using the following R code to create a faceted ggplot: ggplot(plot.PIs.long, aes(x =…
- User
I'm using the following R code to create a faceted ggplot:
ggplot(plot.PIs.long, aes(x = x_var, y = y_var)) + geom_point(position=position_jitter(width=0.0, height=0))+ facet_wrap(~ y, labeller = labeller(y=label_wrap_gen(width=35)), ncol = 3, scales="free_y", strip.position = "left")+ geom_hline(aes(yintercept=m), linetype="dashed", color="red")+ ylab(NULL)+ xlab()+ theme(strip.background = element_blank(), strip.placement = "outside")+ scale_x_continuous() + theme(legend.position = "none")+ geom_smooth(method = "lm", se = T)+ stat_poly_eq(aes(label = paste(p.value.label,..rr.label.., sep = "~~~")), formula = y ~ x, parse = TRUE, size = 3, label.x.npc = 0, label.y.npc = 0)
Can you show me how to put this into a function that will take inputs of x_var and y_var so that I can use the function plot may x and y variables in the future?
- Assistant
Sure! Here's an example of how you can create a function that takes in the x and y variables as inputs, and returns the corresponding ggplot:
codelibrary(ggplot2) library(ggpmisc) my_plot_function <- function(x_var, y_var) { ggplot(plot.PIs.long, aes_string(x = x_var, y = y_var)) + geom_point(position=position_jitter(width=0.0, height=0))+ facet_wrap(~ y, labeller = labeller(y=label_wrap_gen(width=35)), ncol = 3, scales="free_y", strip.position = "left")+ geom_hline(aes(yintercept=m), linetype="dashed", color="red")+ ylab(NULL)+ xlab()+ theme(strip.background = element_blank(), strip.placement = "outside")+ scale_x_continuous() + theme(legend.position = "none")+ geom_smooth(method = "lm", se = T)+ stat_poly_eq(aes(label = paste(p.value.label,..rr.label.., sep = "~~~")), formula = y ~ x, parse = TRUE, size = 3, label.x.npc = 0, label.y.npc = 0) } - User
What is aes_string?
- Assistant
aes_stringis a function inggplot2that is similar toaes, but it allows you to use variables that are stored as characters instead of actual variable names.For example, instead of writing:
codeggplot(data, aes(x = var1, y = var2))You could write:
codex_var <- "var1" y_var <- "var2" ggplot(data, aes_string(x = x_var, y = y_var))This can be useful when you want to create a function (like in the previous question) where the variable names might not be known ahead of time, or when you have a large number of variables and you want to automate the plotting process.
However, it's worth noting that in the newer versions of
ggplot2,aes_stringhas been replaced by the more flexibleaesfunction with the.datapronoun. So instead of usingaes_string, you can useaesto refer to variables as strings like so:codeggplot(data) + aes(x = !!sym(x_var), y = !!sym(y_var)) - User
Canw e edit the function you wrote above to also provide an input for the data frame where x_var and y_var will originate from.
- Assistant
Yes, we can edit the function to include an input for the data frame from which
x_varandy_varoriginate. Here's an example of how you can modify the function to include adataargument:codelibrary(ggplot2) library(ggpmisc) my_plot_function <- function(data, x_var, y_var) { ggplot(data, aes_string(x = x_var, y = y_var)) + geom_point(position=position_jitter(width=0.0, height=0))+ facet_wrap(~ y, labeller = labeller(y=label_wrap_gen(width=35)), ncol = 3, scales="free_y", strip.position = "left")+ geom_hline(aes(yintercept=m), linetype="dashed", color="red")+ ylab(NULL)+ xlab()+ theme(strip.background = element_blank(), strip.placement = "outside")+ scale_x_continuous() + theme(legend.position = "none")+ geom_smooth(method = "lm", se = T)+ stat_poly_eq(aes(label = paste(p.value.label,..rr.label.., sep = "~~~")), formula = y ~ x, parse = TRUE, size = 3, label.x.npc = 0, label.y.npc = 0) }