X轴/ Y轴闪亮R

时间:2018-11-07 21:43:18

标签: r shiny

我正在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 个答案:

答案 0 :(得分:0)

我注意到了一些事情:

  1. 您的代码中有一些错误。 ggplot呼叫中缺少逗号,多余的右括号。
  2. 名称必须匹配。您的数据具有大写的列名,而输入选择器则没有。那会导致一些错误。
  3. 使用ggplotthemeplotly::layout()调用中设置主题方面。
  4. 如果要在y中中断1000美元,当为该轴选择Dollars时,必须建立逻辑来执行此操作,否则会引发错误。为此,我将使用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)