Create a reactive plot with plotly and shiny with specific gridlines ,marker color, background color and specific font

Hello I have a simple shiny app in which I create a scatterplot of variables found in the iris dataset. What I want is to be able to modify the plot in some ways. First of all I would like to be able to set the limits of the gridlines for each axis(by 1 for example), then set the background color to white and the marker color to blue and add trendline.

Preferred styles: Plot title - Calibri (or similar), 10 pt, bold, dark grey

Axis titles – Calibri light (or similar), 16 pt, bold, dark grey

Axis number labels – Calibri, 11 pt

Data labels – Calibri, 9 pt, black
Data markers – dark blue circles
I do not know if all of these or maybe some are available in ggplot2 in combination with plotly or I have to use only plotly as Im new to it and would like some guidance.Thanks

#ui.r
library(shiny)
library(ggplot2)
library(plotly)

fluidPage(

  # App title ----
  titlePanel(div("CROSS CORRELATION",style = "color:blue")),

  # Sidebar layout with input and output definitions ----
  sidebarLayout(

    # Sidebar panel for inputs ----
    sidebarPanel(


    ),
    # Main panel for displaying outputs ----
    mainPanel(

      tabsetPanel(type = "tabs",

                  tabPanel("Correlation Plot",
                           fluidRow(
                             column(3, uiOutput("lx1")),
                           column(3,uiOutput("lx2"))),
                           plotlyOutput("sc"))
      ))
  ))
#server.r
function(input, output) {




  output$lx1<-renderUI({
    selectInput("lx1", label = h4("Select 1st Expression Profile"), 
                choices = colnames(iris[,1:4]), 
                selected = "Lex1")
  })
  output$lx2<-renderUI({
    selectInput("lx2", label = h4("Select 2nd Expression Profile"), 
                choices = colnames(iris[,1:4]), 
                selected = "Lex2")
  })


  output$sc<-renderPlotly({

  p <- ggplot(iris, aes_string(x = input$lx1, y = input$lx2)) + 
    geom_point()
  ggplotly(p) %>% 
    layout(height = 400) 

  })

}

did you look at the options in ?ggplot2::theme? You can change most of what you want in there, with the exception of the data markers which you would set in your geom_point() call.

As for whether they translate into plotly, I am not sure about each of them, but typically the font looks a little different when you put a ggplot object into ggplotly(). If you want to look at how to build your graph in plotly directly, you can take a look at the Plotly in R Library

1 Like

I was not aware of this. Could you give me a small example ofggplot2::theme??

you can do this for the plot title, and the rest would be similar:

theme(plot.title = element_text(family = "calibri", size = 10, color = "dark grey", face = "bold"))

As a note, I am not sure that the family is correct, but I am sure a quick google search can tell you the correct input for font families in R

1 Like

Here is an example with your plot:

library(ggplot2)
library(extrafont)

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) + 
  geom_point() + 
  labs(title = "This is a title") + 
  theme(plot.title = element_text(family = "Calibri", size = 10, 
                                  color = "dark grey", face = "bold"))

which gives you this:

It seems to work. Plotly itself does not seem to work properly as I have a really huge dataset and it provides an one-colored meaningless plot instead of a scatterplot. Is there a choice in theme about setting gridlines? I want to display my data as 0 -200-400..etc

If you are looking to set the placement of them then you should use the scale_x_continuous or scale_y_continuous functions and specify the breaks argument in them. If you want to customize the way they look you will have to specify panel.grid.major in theme to change both x and y gridlines. if you just want to do one or the other, you specify like this, panel.grid.major.x or panel.grid.major.y

1 Like

great answers I applied them. One more thing..is it possible to to change the default grey background color of the plot to white and the gridlines from white to grey.

theme_bw() i found it.