0

Given a dataset like this:

XX<-as.factor(c('P','P','P','R','R','R','F','F','F'))
YY<-c(2,3,6,3,5,3,2,3,4)
Facto<-c("A","B","C","A","B","C","A","B","C")
Text<-c("Z","Z","F","Z","H","Z","Z",'Z',"M")
MyData<-data.frame(XX,YY,Facto,Text)
rm(Facto)

My goal is to have a function to create a bar chart with text above each bar

MyBarPlot<-function(Data, ColName){
  dodger = position_dodge(width = 0.9)
  ggplot(Data, aes(x=XX, y=YY, fill=Facto))+
    geom_bar(stat="identity", color="black",position=position_dodge())+
    scale_fill_viridis(discrete = TRUE,option = "D",name=)+
    geom_text(aes(label=Text, group = ColName), position = dodger, col='red')+
    theme_minimal()
  
  
}

When I run the above function, the "fill=" argument works just fine, but the "group by" aspect for geom_text does not.

MyBarPlot(MyData,'Facto')

If I change the "group=" line in the function like this:

MyBarPlot<-function(Data, ColName){
      dodger = position_dodge(width = 0.9)
      ggplot(Data, aes(x=XX, y=YY, fill=Facto))+
        geom_bar(stat="identity", color="black",position=position_dodge())+
        scale_fill_viridis(discrete = TRUE,option = "D",name=)+
        geom_text(aes(label=Text, group = Facto), position = dodger, col='red')+
        theme_minimal()
      
      
    }

I get what I want, but I lose the ability to assign this variable within my function. Is there way I can assign the "group=" dynamically within my function call, just as I'm doing with the "fill=" argument?

3
  • 1
    It seems like your function is not currently using the ColName parameter anywhere. Commented Mar 31, 2021 at 22:33
  • 1
    Here is a good primer on the nonstandard evaluation aspects of using ggplot2 inside functions. I think one reason your function worked as it did is because you have the variables in you global environment that you likely couldn't expect the user of the function to have. Commented Mar 31, 2021 at 22:33
  • Apologies- "fill=Facto" should be fill = "ColName" Commented Mar 31, 2021 at 22:43

1 Answer 1

2

Does this do what you want?

MyBarPlot<-function(Data, ColName){
  dodger = position_dodge(width = 0.9)
  ggplot(Data, aes(x=XX, y=YY, fill= {{ColName}}))+
    geom_bar(stat="identity", color="black",position=position_dodge())+
    viridis::scale_fill_viridis(discrete = TRUE,option = "D",name=)+
    geom_text(aes(label=Text), position = dodger, col='red')+
    theme_minimal()
}


MyBarPlot(MyData,Facto)

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks- I didn't realize including the "group by" command was what was causing the problem. Just dropping it as you suggest fixed it
Also great for the fill= {{ColName}}

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.