student_R123
student_R123

Reputation: 1002

Regarding getting correct axis title using ggplot2 in R

I want to draw a boxplots for each column in a data frame. So I have created a function to do that. My data set looks like follows:

set.seed(123)
x1=rnorm(100,0,1)
x2=rnorm(100,0,0.5)
x3=rnorm(100,0,0.6)
data_x=data.frame(x1,x2,x3)
datax_long=data_x %>% gather(x_val ,value ,x1:x3)

> head(datax_long)
  x_val       value
1    x1 -0.56047565
2    x1 -0.23017749
3    x1  1.55870831
4    x1  0.07050839
5    x1  0.12928774
6    x1  1.71506499

I created the following function to fulfill my purpose.

plot_fun=function(var,low_lim,upp_lim,coef,data)
{
  plt=ggplot(data, aes(x = coef, y =var ,color=var)) +
    stat_summary(geom = "errorbarh", 
                 fun.min = function(z) quantile(z, low_lim), 
                 fun = mean,
                 fun.max = function(z) quantile(z, upp_lim)) +
    stat_summary(geom = "point", fun = mean)
  print(plt + labs(y=var)+labs(colour = var))
  
}

It gives me the output that I want except for the y axis label and the title of the legend.

plot_fun(datax_long$x_val,0.025,0.975,datax_long$value,datax_long)

enter image description here

In the above output, the label of the y axis should be "x_val" instead of x1. Also the title of the legend should also be "x_val" instead of x1.

Can anybody help me to figure out how to modify the above function to get the desired results?

Thank you.

Upvotes: 1

Views: 47

Answers (2)

stefan
stefan

Reputation: 123808

Making use of a bit of tidy eval this could be achieved like so:

  1. Instead of passing vectors pass the variables you like to plot to your function.
  2. Inside your function you can call these variables using the curly-curly-operator {{.
  3. For the labels we have to convert the variable names to strings which could be achieved using as_label(enquo(...)).
library(tidyverse)

set.seed(123)
x1=rnorm(100,0,1)
x2=rnorm(100,0,0.5)
x3=rnorm(100,0,0.6)
data_x=data.frame(x1,x2,x3)
datax_long=data_x %>% gather(x_val ,value ,x1:x3)

plot_fun=function(data, var, coef, low_lim,upp_lim)
{
  plt = ggplot(data, aes(x = {{coef}}, y = {{var}} ,color = {{var}})) +
    stat_summary(geom = "errorbarh", 
                 fun.min = function(z) quantile(z, low_lim), 
                 fun = mean,
                 fun.max = function(z) quantile(z, upp_lim)) +
    stat_summary(geom = "point", fun = mean)
  plt + labs(y = as_label(enquo(var)), colour = as_label(enquo(var)))
  
}

plot_fun(datax_long, x_val, value, 0.025,0.975)

Upvotes: 3

Duck
Duck

Reputation: 39585

Try this. You can use your function with a string for the name variable. Then the rest of your code will work properly. Here the code:

#Function
plot_fun=function(var,low_lim,upp_lim,coef,data)
{
  plt=ggplot(data, aes_string(x = coef, y =var ,color=var)) +
    stat_summary(geom = "errorbarh", 
                 fun.min = function(z) quantile(z, low_lim), 
                 fun = mean,
                 fun.max = function(z) quantile(z, upp_lim)) +
    stat_summary(geom = "point", fun = mean)
  print(plt + labs(y=var)+labs(colour = var))
  
}
#Apply
plot_fun('x_val',0.025,0.975,datax_long$value,datax_long)

Output:

enter image description here

Upvotes: 3

Related Questions