1

I am trying to put write a function to generate ggplot plots. Heroka helped me out with getting my function to work. But now I have a question on how I can build an auto label wrapper inside this function. I saw a similar and very useful question on this, but the same code doesn't work inside a function.

I'm reproducing my current function for generating ggplot below:

myfunction = function (data, Variable1) {
  ggplot(data=data, aes_string(sprintf("factor(%s)",Variable1), "Variable2"))+
  geom_boxplot(fill="grey", colour="black")+
  labs(title = sprintf("%s and Variable2", Variable1)) +
  labs (x = Variable1, y = "Variable2")
}

When I try to do the below (note that I'm trying to wrap the labels within the call of the ggplot:

myfunction = function (data, Variable1) {
  ggplot(data=data, aes_string(sprintf("factor(%s)",Variable1), "Variable2"))+
  geom_boxplot(fill="grey", colour="black")+
  labs(title = sprintf("%s and Variable2", Variable1)) +
  labs (x = Variable1, y = "Variable2")+
# adding the line to wrap the labels
  scale_x_discrete(labels = function(x) str_wrap(x, width = 10))
}

It doesn't work (plot still generated, but as if I didn't insert that line) I'm confused as to:

  1. I think I need to change the x in that additional line to my actual variable? in this case that would be Variable1, but when I did that it still didn't work

  2. Since we are still within the ggplot call, is this an environment issue again? (such as we need to use aes_string instead of aes when within a function since we are in a local environment). However, with the additional of an extra function within this function, I'm not sure which environment we are in and how to implement the label wrapping line.

double-beep
  • 5,031
  • 17
  • 33
  • 41
debbybeginner
  • 79
  • 1
  • 10
  • Can you include a small data sample which would help reproduce the issue ? What are the labels supposed to look like (i.e. the ones you want wrapped) ? – steveb Jan 17 '16 at 07:36

1 Answers1

1

I would say it looks the same because there is nothing to wrap, 0 and 1 are too short to wrap. I made the factors longer so the str_wrap function had something to work with, and here you see it is working fine:

set.seed(123)
library(stringr)
library(ggplot2)

valstr <- c("This is value 0","This is value 1")

dat <- data.frame(Variable2=rnorm(100),
                  Variable1=valstr,
                  Variable3=sample(valstr,100,T))

myfunction_1 = function (data, Variable1) {
  ggplot(data=data, aes_string(sprintf("factor(%s)",Variable1), "Variable2"))+
    geom_boxplot(fill="grey", colour="black")+
    labs(title = sprintf("%s and Variable2", Variable1)) +
    labs (x = Variable1, y = "Variable2")
}

myfunction_2 = function (data, Variable1) {
  ggplot(data=data, aes_string(sprintf("factor(%s)",Variable1), "Variable2"))+
    geom_boxplot(fill="grey", colour="black")+
    labs(title = sprintf("%s and Variable2", Variable1)) +
    labs (x = Variable1, y = "Variable2")+
    # adding the line to wrap the labels
    scale_x_discrete(labels = function(x) str_wrap(x, width = 10))
}

p1 <- myfunction_1(dat,"Variable1")
p2 <- myfunction_2(dat,"Variable3")
grid.arrange(p1, p2, ncol=2)

yielding:

enter image description here

Mike Wise
  • 22,131
  • 8
  • 81
  • 104
  • thank you! sorry, in my actual data I had much longer labels, (not 0, 1). but I actually figure out the problem, which is my fault, I realised the problem is not that the function is _not_ working, but rather that my labels were long but they were single words. Just to confirm the wrapper can only wrap if it's a label composed of multiple words, right? (i.e. there's natural blank space in between the words where the wrapping can occur), and that the wrapper won't break down single long word (e.g. ThisIsAVeryLongLongLongLongLabel)? Many thanks! – debbybeginner Jan 18 '16 at 04:58