我正在尝试在 Shiny 应用程序中绘制一个简单的条形图。
全球
library(shiny)
library(ggplot2)
library(plotly)
dat <- data.frame(
time = factor(c("Lunch","Dinner"), levels=c("Lunch","Dinner")),
total_bill = c(14.89, 17.23)
)
用户界面
ui <- navbarPage(title = "My Shiny App",
tabPanel(title = "App",
pageWithSidebar(
headerPanel("Shiny App"),
sidebarPanel(
actionButton(inputId = "clicks",
label = "Click me")
),
mainPanel("Main Panel",
plotlyOutput("norm"),
actionButton("renorm", "Resample")
)
)
),
tabPanel(title = "Tab2",
plotOutput("unif"),
actionButton("reunif", "Resample")
),
tabPanel(title = "Tab3",
plotOutput("chisq"),
actionButton("rechisq", "Resample")
)
)
服务器
server <- function(input, output) {
output$norm <- renderPlotly({
ggplot(data=dat, aes(x=time, y=total_bill, fill=time)) +
geom_bar(stat="identity") +
guides(fill=FALSE)
})
这使我在Shiny应用程序中的绘图如下。我对此有三个问题。
FALSE
通过 ggplot ,图例仍会显示Dinner
先然后Lunch
以下这些小的修改完成了所有这三件事。
情节上有一个错误。这是一个有点神秘的解决方法。
由于某种原因,plotly 覆盖了guide(fill=FALSE)
,我不知道为什么真的,plotly 文档不是很广泛。这是摆脱与 plotly 一起使用的指南的另一种方法。
无论如何,这是代码
library(shiny)
library(ggplot2)
library(plotly)
dat <- data.frame(
time = factor(c("Lunch","Dinner"), levels=c("Lunch","Dinner")),
total_bill = c(14.89, 17.23)
)
ui <- navbarPage(title = "My Shiny App",
tabPanel(title = "App",
pageWithSidebar(
headerPanel("Shiny App"),
sidebarPanel(
actionButton(inputId = "clicks",
label = "Click me")
),
mainPanel("Main Panel",
plotlyOutput("norm"),
actionButton("renorm", "Resample")
)
)
),
tabPanel(title = "Tab2",
plotOutput("unif"),
actionButton("reunif", "Resample")
),
tabPanel(title = "Tab3",
plotOutput("chisq"),
actionButton("rechisq", "Resample")
)
)
server <- function(input, output) {
output$norm <- renderPlotly({
# reorder the levels
oldlevels <- levels(dat$time)
neworder <- order(dat$total_bill,decreasing=T)
dat$time <- factor(dat$time,levels=oldlevels[neworder])
# get the plotly into a temp var
gplt <- ggplotly(ggplot(data=dat, aes(x=time, y=total_bill, fill=time)) +
geom_bar(stat="identity") +
theme(legend.position="none"))
for (i in 1:length(gplt$x$data)){
gplt$x$data[[i]]$text <- c(gplt$x$data[[i]]$text, "")
}
gplt
})
}
shinyApp(ui,server)
产生:
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句