[英]R: plotly and shiny barplot
我正在根据World Phones的示例进行绘图 。
sidebarPanel不起作用,您能告诉我为什么吗?
data <- data.frame(x=rep(c('A','B','C'), each = 1, times = 3),
y=runif(9, 5, 10),
group=rep(c('2011','2012','2013'), each = 1, times= 3))
ui.R:
library(shiny)
library(plotly)
# Define the overall UI
shinyUI(
# Use a fluid Bootstrap layout
fluidPage(
# Give the page a title
titlePanel("Barchart"),
# Generate a row with a sidebar
sidebarLayout(
# Define the sidebar with one input
sidebarPanel(
selectInput("letter", "Letter:", levels(data[,1])),
hr()),
# Create a spot for the barplot
mainPanel(
plotlyOutput("dataPlot")
))))
server.R:
library(shiny)
library(plotly)
shinyServer(function(input, output) {
# Fill in the spot we created for a plot
output$dataPlot <- renderPlotly({
# Render a barplot
p <- plot_ly(data, x = input$letter, y=y, group=group,type = "bar")
p})
})
Batanicheck给出的每件事都是正确的,但对图进行一些修改即可获得准确的输出。
更换
> p <- plot_ly(data[data$x==input$letter,], x=group, y=y,type = "bar")
WITH
> p <- plot_ly(data[data$x==input$letter,], x=x, y=y, group=group,type = "bar")
对我来说,它可以在浏览器以及Rstudio浏览器中使用...
我不能完全理解你所看到的
但是例如,如果您只想按年份绘制一个字母
1)你有不好的df
为例- A
只存在到2011年
2)我使用data <- data.frame(x=rep(c('A','B','C'), each = 1, times = 3), y=runif(9, 5, 10), group=rep(c('2011','2012','2013'), each = 3, times= 1))
3)试试p <- plot_ly(data[data$x==input$letter,], x=group, y=y,type = "bar")
不要忘记将data
声明到ui
和server
并且仅在浏览器中工作,而不是在rstudion查看器中工作(对我来说)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.