ggPlot2 Equation Text Renders Poorly on Shiny

200 views
Skip to first unread message

Amber James

unread,
Jul 20, 2015, 8:26:43 PM7/20/15
to shiny-...@googlegroups.com
Hello everyone! 
I apologize for bothering you but I am stuck with a ggplot graph that is providing poor rendering for Shiny. 

I have been reading up online on how to display the equation of a regression lines onto a ggplot2 graph. However, I have been unable to find something that would fit my needs. I am working with a team of data analysts and they want me to design a simplistic system that allows them to choose different types of regression equations. (ie : lm, glm, gam, loess, rlm ) When they generate these plots they want to have the equation for the plot displayed with the graph. 

I found the following 2 links with gave me a head start on what I needed to do: 

However, I have two main problems. First, I am attempting to use this library from github: ggplot_smooth_func.R
The issue with this is that the graph displayed is not generating properly when using Shiny. 


Here is some example code:

library(shiny)
library(ggplot2)
shinyApp(
  ui = fluidPage( plotOutput('plot1')),
  server = function(input, output) {    
      output$plot1 <- renderPlot({
        ggplot(data = mtcars, aes(x = cyl, y = vs, label=carb)) +
          stat_smooth_func(geom="text",method="lm",hjust=0,parse=TRUE) +
          geom_smooth(method="lm",se=FALSE) +
          geom_point()
    })
    
  })

If I generate the plot within RStudio the equation renders just fine. :/ 

In addition to this problem, I need to be able to display multiple lines of equations. The users I am working with will be using multiple sets of data and, because of this, they will need to have each line's regression equation. Is it possible to do this? I realize this is a shiny based forum, I am just asking in case someone knows already.

Also, would the regression equations for the lm, glm, gam, loess, rlm be the same ? I am assuming they are not but I am only finding solutions dealing with the lm equation. 


Thanks so much for your help and time! 


Amber James 

Kevin Lindquist

unread,
Jul 21, 2015, 1:24:18 AM7/21/15
to shiny-...@googlegroups.com
This is just a guess, but I had some issues with ggplot2 graphics quality when running shiny on a linux server. It turned out that the quality of the graphics output was significantly improved by installing the Cairo package on the server. I think you just need to install the package, since my recollection is that shiny (or ggplot2) will automatically use Cairo if it's installed.

Amber James

unread,
Jul 21, 2015, 1:53:39 AM7/21/15
to shiny-...@googlegroups.com
Thanks so much. That fixed it right up!! 
Reply all
Reply to author
Forward
0 new messages