我正在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)
答案 0 :(得分:0)
我注意到了一些事情:
ggplot
呼叫中缺少逗号,多余的右括号。ggplot
或theme
在plotly::layout()
调用中设置主题方面。scale_y_continuous
并相应地设置breaks
参数。如果需要美元标签,可以在标签中使用scales
包和dollar()
函数。例如scale_y_continuous(breaks = seq(0,3000,1000), labels = dollar)
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)