Shiny + ggplot2 , how to use aes_string but include a fixed label ?

The part of this Shiny code which takes two variables from a df using FancyTable_rows_selected references a dataframe called Expression and generates a scatterplot :

  observe({
    req(input$fancyTable_rows_selected)
    selRow <- myCSV()[input$fancyTable_rows_selected,]
    print(selRow[[1]])
    output$plot <- renderPlot({
      ggplot(Expression, aes_string(x=selRow[[1]], y=selRow[[2]])) + 
        geom_point(size=2) + 
        geom_smooth(method=lm, size=2) +  
        geom_rug(col="darkred", size=0.9, sides="bl", alpha = 0.3) +
        theme(aspect.ratio = 1,
              axis.title = element_text(size = 15, face = "bold", color = "black"),
              axis.text = element_text(size = 12, face = "bold", color = "black"))
    }) 
  })

So when I click the rows listing "C1QC" and "C1QB", I get a scatterplot and the labels are printed accordingly :

image

My question: How do I change this to have the fixed word "Log_Expression" before each printed variable? Such that the printed axes in this case would be "Log_Expression C1QC" and "Log_Expression C1QB" ?

I played around with the labs, xlab and ylab, aes, aes_ and aes_string functions, but I can't seem to make anything work.

Thanks!

I am not entirely sure that the following matches what you are trying to do but here is one solution.

library(ggplot2)
DF <- data.frame(X = "XVals", Y = "YVals", stringsAsFactors = FALSE)
DAT <- data.frame(XVals = 1:30, YVals = 1:30 + rnorm(30))
ggplot(DAT, aes_string(x = DF[[1]], y = DF[[2]])) + geom_point() +
  labs(x = paste("Log_Expression", DF[[1]]), y = paste("Log_Expression", DF[[2]]))

Created on 2020-02-29 by the reprex package (v0.3.0)

1 Like

It was the paste function that I was missing. Thanks!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.