繁体   English   中英

X轴/ Y轴闪亮R

[英]X-axis / Y-axis Shiny R

我在R中使用Shiny代码来生成多个(30)箱形图。 我尝试并排显示大约30个类别(x轴),并希望在底部调整x轴,以便可以以90度角看到每个名称。 我尝试将条件放置在“ ggplot”和“ plotly”行中以调整x轴/ y轴,但在Shiny应用程序上没有任何作用。 我还想调整y轴刻度线,以便它们在每$ 1000的值处刻度。 我可以使用ggplot2重现该图并调整x / y轴,但希望通过Shiny使我的图具有动态性,因此可以应用不同的滤镜。 我正在尝试绘制“名称”(X轴,分类)与“美元”(Y轴,数字)的图,我想查看大约30种不同的“名称”。 我的数据有1万多行。

Sample Data:
ID      Tradelane     Name     Dollar
10R46   Ocean         Ray      2000
10R41   Air           Jay      1000
10R45   Truck         Alfred   500
10R49   Ocean         Mark     5

不胜感激,下面是我的代码:

library(shiny)
library(plotly)

data(supply)
nms <- names(supply)

ui <- fluidPage(

  headerPanel("Supply Metrics"),
  sidebarPanel(
    sliderInput('sampleSize', 'Sample Size', min = 1, max = nrow(supply),
                value = 1000, step = 500, round = 0),
    selectInput('x', 'X', choices = nms, selected = "name"),
    selectInput('y', 'Y', choices = nms, selected = "dollar"),
    selectInput('color', 'Color', choices = nms, selected = "tradelane"),

    selectInput('facet_row', 'Facet Row', c(None = '.', nms), selected = "tradelane"),
    selectInput('facet_col', 'Facet Column', c(None = '.', nms)),
    sliderInput('plotHeight', 'Height of plot (in pixels)', 
                min = 100, max = 2000, value = 1000)
  ),
  mainPanel(
    plotlyOutput('trendPlot', height = "900px")
  )
)

server <- function(input, output) {

  #add reactive data information. Dataset = built in diamonds data
  dataset <- reactive({
    supply[sample(nrow(supply), input$sampleSize),]
  })

  output$trendPlot <- renderPlotly({

    # build graph with ggplot syntax
    p <- ggplot(dataset(), aes_string(x = reorder(input$x, input$y, FUN = median) y = input$y, color = input$color)) + 
      geom_boxplot() + theme(axis.text.x=element_text(size=2,angle=90))



    # if at least one facet column/row is specified, add it
    facets <- paste(input$facet_row, '~', input$facet_col)
    if (facets != '. ~ .') p <- p + facet_grid(facets)

a <- list(size=3, color = "black", tickangle = 45)

ggplotly(p) %>% 
  layout(height = input$plotHeight, xaxis = a, yaxis = a))

  })

}

shinyApp(ui, server)

我注意到了几件事:

  1. 您的代码中有一些错误。 ggplot调用中缺少逗号,多余的ggplot括号。
  2. 名称必须匹配。 您的数据具有大写的列名,而输入选择器则没有。 那会导致一些错误。
  3. 使用theme或使用plotly::layout()ggplot调用中设置主题方面。
  4. 如果要在y中中断1000个美元,则必须建立逻辑以在为该轴选择“美元”时执行此操作,否则会引发错误。 为此,我将使用scale_y_continuous并相应地设置breaks参数。 如果需要美元标签,可以在标签中使用scales包和dollar()函数。 例如scale_y_continuous(breaks = seq(0,3000,1000), labels = dollar)
  5. 您的reorder调用未按预期运行,您正在提供函数字符串,但并非如此。

如果您删除了以上错误,我可以使它工作。 但是,我还没有在轴标签上内置美元请求。

library(shiny)
library(plotly)
library(ggplot2)

supply <- tibble::tribble(
  ~ID,      ~Tradelane,     ~Name,     ~Dollar,
"10R46",   "Ocean",         "Ray", 2000,
"10R41",   "Air",           "Jay", 1000,
"10R45",   "Truck",         "Alfred",   500,
"10R49",   "Ocean",         "Mark", 5)

# data(supply)
nms <- names(supply)

ui <- fluidPage(

  headerPanel("Supply Metrics"),
  sidebarPanel(
    sliderInput('sampleSize', 'Sample Size', min = 1, max = nrow(supply),
                value = 1000, step = 500, round = 0),
    selectInput('x', 'X', choices = nms, selected = "Dame"),
    selectInput('y', 'Y', choices = nms, selected = "Dollar"),
    selectInput('color', 'Color', choices = nms, selected = "Tradelane"),

    selectInput('facet_row', 'Facet Row', c(None = '.', nms), selected = "Tradelane"),
    selectInput('facet_col', 'Facet Column', c(None = '.', nms)),
    sliderInput('plotHeight', 'Height of plot (in pixels)', 
                min = 100, max = 2000, value = 1000)
  ),
  mainPanel(
    plotlyOutput('trendPlot', height = "900px")
  )
)

server <- function(input, output) {

  #add reactive data information. Dataset = built in diamonds data
  dataset <- reactive({
    supply[sample(nrow(supply), input$sampleSize),]
  })

  output$trendPlot <- renderPlotly({

    # build graph with ggplot syntax
    p <- ggplot(dataset(), aes_string(x = input$x, y = input$y, color = input$color)) + 
      geom_boxplot() 


    # if at least one facet column/row is specified, add it
    facets <- paste(input$facet_row, '~', input$facet_col)
    if (facets != '. ~ .') p <- p + facet_grid(facets)

    a <- list(tickangle = 45)

    ggplotly(p) %>% 
      layout(height = input$plotHeight, xaxis = a)

  })

}

shinyApp(ui, server)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM