Search code examples
rshinyaxis-labelsdygraphs

How to put a superscript and special characters in dygraphs axis labels


I have a shiny app that makes a plot using the dygraphs package. I need to use superscript and special characters in the axis labels on the plot. I have tried expression() and bquote(), but it does not seem that dygraphs can evaluate those functions as the labels are printed as [Object Object].

Here is a minimal example. I need to superscript the 87 and 86 in the y label and use a mu character in the x label.

library(shiny)
library(dygraphs)

ui <- fluidPage(dygraphOutput("dygraph"))

server <- shinyServer(function(input, output, session) {

  sampleplot <- function(){
    norm <- rnorm(1000,mean=50,sd=7)
    dist <- seq(1,1000,by=1)
    dat <- as.data.frame(cbind(dist,norm))
    names(dat) <- c("Distance","Normal")
    dygraph(dat, ylab="87Sr/86Sr",xlab="Distance (um)") %>% 
      dySeries("Normal",drawPoints = TRUE, pointSize = 2, strokeWidth = 0.0)}

  output$dygraph <- renderDygraph({sampleplot()})


    }
  )


shinyApp(ui=ui,server=server)

Solution

  • I read the dygraphs options webpage for JS and found out the axis labels can take HTML, not just text. I looked up how to type superscript http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_sup and special characters http://www.htmlhelp.com/reference/html40/entities/symbols.html in HTML and it worked!

    library(shiny)
    library(dygraphs)
    
    ui <- fluidPage(dygraphOutput("dygraph"))
    
    server <- shinyServer(function(input, output, session) {
    
      sampleplot <- function(){
        norm <- rnorm(1000,mean=50,sd=7)
        dist <- seq(1,1000,by=1)
        dat <- as.data.frame(cbind(dist,norm))
        names(dat) <- c("Distance","Normal")
        dygraph(dat, ylab="<sup>87</sup>Sr/<sup>86</sup>Sr",xlab="Distance (&mu;m)") %>% 
          dySeries("Normal",drawPoints = TRUE, pointSize = 2, strokeWidth = 0.0)}
    
      output$dygraph <- renderDygraph({sampleplot()})
    
    
        }
      )
    
    
    shinyApp(ui=ui,server=server)