Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Ggplot is displayed without numeric limitations in its labels inside shiny app

I have the shiny app below in which I create a dynamic scatterplot. The thing is that when I create my ggplot outside of the shiny app I get it in a bigger and most readable form, but inside shiny I get the result quite smaller without numeric labels in the 2 axes. Why does this happen?

enter image description here

app.R

library(shiny)
library(ggplot2)

n<-structure(list(ADM3_PCODE = c("ML100101", "ML100201", "ML070301", 
"ML070105", "ML100301", "ML070107", "ML100401", "ML070104", "BF560105", 
"ML060502", "ML060302", "ML070101", "ML070202", "ML070303", "ML070204", 
"ML070205", "ML070106", "ML070305", "ML070103", "ML070102"), 
    Acled_Health = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0), Acled_no_inst = c(4, 0, 0, 0, 0, 0, 0, 
    0, 3, 1, 0, 2, 1, 4, 1, 0, 1, 1, 0, 0), Acled_Total = c(4, 
    0, 1, 0, 0, 0, 0, 0, 3, 1, 0, 2, 1, 4, 1, 0, 1, 1, 0, 0), 
    I3 = c(1.09376534720521, 2.98445151003512, 1.38675338665065, 
    2.71565793377472, 3.18930523219588, 2.90551791702655, 1.53290303794127, 
    1.06882818216774, 1.0422158290422, -0.278650206004393, 1.74575777732344, 
    1.06030283919043, 1.96049964093946, 2.63957106704249, 2.58417173100033, 
    2.28469585057556, 0.531106478645378, 2.52253985112635, 2.72742421204698, 
    0.943157796157148), I4 = c(2.97790487569313, 4.39377989487155, 
    4.20062305434793, 3.5875729546847, 2.87065592255387, 1.93698960334718, 
    2.32611240844526, 3.79504053511001, 3.26867102154575, 3.54402045486982, 
    2.59943279715003, 2.24078712967162, 3.7837957824595, 0.629466739192882, 
    2.23259745421704, 3.22143437813918, 2.832868377928, 1.85546575024811, 
    2.44473249718205, 1.27700101283808), I5 = c(4.50640751725631, 
    4.59205796716096, 2.81987303382463, 5.6939014694895, 4.84552750414595, 
    3.48919571832088, 3.90947133936061, 5.62442105113242, 5.46450373445726,
3.81405499661111, 3.64136973672331, 1.48012372278708, 3.45348478980314, 
    2.79266252795185, 2.92273131958892, 3.12803195965532, 4.8713736085137, 
    4.78116333258574, 5.07934714109017, 4.90711103863146), I6 = c(4.71964925827176, 
    7.88685736651118, 4.48033628274155, 4.63463388102439, 5.41071131184507, 
    4.49985156270955, 4.81313128120664, 5.16036902503232, 4.07947495928142, 
    3.76143006251051, 5.18794193926347, 5.60747967555871, 5.50504418957362, 
    2.85716269808936, 7.16407719804625, 4.77737451764469, 5.7199718544193, 
    6.27105214031155, 3.48487832187209, 5.00715544299671), ADM0_FR = c("Mali", 
    "Mali", "Mali", "Mali", "Mali", "Mali", "Mali", "Mali", "Burkina Faso", 
    "Mali", "Mali", "Mali", "Mali", "Mali", "Mali", "Mali", "Mali", 
    "Mali", "Mali", "Mali"), ADM3_FR = c("Ménaka", "Andéramboukane", 
    "Anchawadi", "Talataye", "Inékar", "Tin Hama", "Tidermène", 
    "Ouattagouna", "Tin-Akoff", "Ber", "Banikane", "Ansongo", 
    "Bourem", "Gao", "Tarkint", "Téméra", "Tessit", "N'tillit", 
    "Bourra", "Bara")), row.names = c("2327", "4748", "7169", 
"14432", "16853", "19274", "21695", "24116", "36192", "38642", 
"41063", "43484", "45905", "48326", "53168", "55589", "58010", 
"60431", "62852", "65273"), class = "data.frame")


ui <- pageWithSidebar(
  headerPanel('Scattetrplot'),
  sidebarPanel(
    selectInput('xcol', 'X Variable', choices = c("Acled_Total", "Acled_no_inst", "Acled_Health"),selected="Acled_Total",
                                                  multiple=F),
    selectInput('ycol', 'Y Variable',  choices = c("I3", "I4", "I5", "I6" ),selected="I3",
                multiple=F),
  ),
  mainPanel(
    plotOutput('plot1')
  )
)

server <- function(input, output) {
  new<-reactive({
    myvars <- input$xcol
    newdata <- n[myvars]
    
  })
  
  
  output$plot1<-renderPlot({
    ggplot(n, aes(x=input$xcol, y=input$ycol,  fill = ADM0_FR, shape = ADM0_FR)) +
      geom_point(position=position_jitter(h=0.1, w=0.1), alpha = 0.5, size = 1.5) +
      scale_fill_manual(values=c("red","blue","orange")) +
      scale_shape_manual(values= c(23, 24, 25)) +
      geom_text(position=position_jitter(h=0.10, w=0.55),
                aes(label= ifelse(new()[,1] > quantile(new()[,1], 0.98),
                                  as.character(ADM3_FR),'')),vjust=3, size=2.75)+
      labs(x = "",y="")+
      theme_bw()
  })
  
  
}

shinyApp(ui, server)  

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The issue is that your inputs are just character strings. Hence, doing e.g. x = input$xcol means you are mapping a constant value on the x aes, i.e. your are doing something like aes(x = "A", y = "B", ..)

To tell ggplot2 that you want to map the column of your data whose name is stored in input$xcol on an aesthetic you could e.g. make use of the .data pronoun from rlang, i.e. do x = .data[[input$xcol]]:

library(shiny)
library(ggplot2)

ui <- pageWithSidebar(
  headerPanel("Scattetrplot"),
  sidebarPanel(
    selectInput("xcol", "X Variable",
      choices = c("Acled_Total", "Acled_no_inst", "Acled_Health"), selected = "Acled_Total",
      multiple = F
    ),
    selectInput("ycol", "Y Variable",
      choices = c("I3", "I4", "I5", "I6"), selected = "I3",
      multiple = F
    ),
  ),
  mainPanel(
    plotOutput("plot1")
  )
)

server <- function(input, output) {
  new <- reactive({
    myvars <- input$xcol
    newdata <- n[myvars]
  })


  output$plot1 <- renderPlot({
    ggplot(n, aes(x = .data[[input$xcol]], y = .data[[input$ycol]], fill = ADM0_FR, shape = ADM0_FR)) +
      geom_point(position = position_jitter(h = 0.1, w = 0.1), alpha = 0.5, size = 1.5) +
      scale_fill_manual(values = c("red", "blue", "orange")) +
      scale_shape_manual(values = c(23, 24, 25)) +
      geom_text(
        position = position_jitter(h = 0.10, w = 0.55),
        aes(label = ifelse(new()[, 1] > quantile(new()[, 1], 0.98),
          as.character(ADM3_FR), ""
        )), vjust = 3, size = 2.75
      ) +
      labs(x = "", y = "") +
      theme_bw()
  })
}

shinyApp(ui, server)
#> 
#> Listening on http://127.0.0.1:4535

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading