2

I am trying to create a function that will enable me to plot a series of data frames. I am trying to set the title of my plot to a variable that will change based on a variable indicating risk factor.

This is the set-up of a data frame for smoking and a data frame for second hand smoke exposure from riskf (original data frame)

smoking <- df.maker(subsetF$Smoking, subsetM$Smoking)
second_smoking <- df.maker(subsetF$Second.hand.smoke.exposure,      
subsetM$Second.hand.smoke.exposure


plot_function <- function(risks, riskf){

plot <- ggplot(risks, aes(provinces, value )) +  geom_bar(aes(fill = variable), 
position = "dodge", stat="identity") + scale_fill_discrete(name="Gender") + 
xlab("Provinces and Territories") + ylab("Percentage(%)") + ggtitle("") + 
theme_bw() + theme(panel.border = element_blank()) + theme(plot.title     
= element_text(size=20), axis.title.x = element_text(size=14), 
axis.title.y = element_text(size=14)) + geom_hline(yintercept=mean(risks[, 3]))

return(plot)
}


plot_function(smoking)

When I plug in smoking for risks, I want my title to be "Smoking by Province in Canada 2014", and if I plug in second_smoking for risks, I want the title to be "Second Hand Smoke by Province in Canada 2014", etc...

1
  • 2
    Can't you just add another parameter to use for the title? How is R supposed to know to change "second_smoking" into "Second Hand Smoke"? Is there somewhere you plan to define these translations? Commented Dec 8, 2015 at 3:25

1 Answer 1

1

Add in a vector with keys:

plot_function <- function(risks){

  riskf  <- gsub("()","",sys.call()[2])
  titles <- c(smoking="Smoking by Province in Canada 2014",
              second_smoking="Second Hand Smoke by Province in Canada 2014")

  plot <- ggplot(risks, aes(provinces, value )) +  geom_bar(aes(fill = variable), 
  position = "dodge", stat="identity") + scale_fill_discrete(name="Gender") + 
  xlab("Provinces and Territories") + ylab("Percentage(%)") + 
  theme_bw() + theme(panel.border = element_blank()) +
  theme(plot.title = element_text(size=20), axis.title.x = element_text(size=14), 
  axis.title.y = element_text(size=14)) + geom_hline(yintercept=mean(risks[, 3]))+
  ggtitle(titles[riskf])

  return(plot)
}

And because I don't have your data, here is what the parts that I added do:

plot_function <- function(x) {
  riskf  <- gsub("()","",sys.call()[2])
  titles <- c(smoking="Smoking by Province in Canada 2014",
              second_smoking="Second Hand Smoke by Province in Canada 2014")

  print(titles[riskf])
}

smoking <- 1:3

plot_function(smoking)
#                              smoking 
# "Smoking by Province in Canada 2014" 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.