简体   繁体   English

R:密密麻麻的闪亮油条

[英]R: plotly and shiny barplot

I'm making a barplot with plotly based on the World Phones example . 我正在根据World Phones的示例进行绘图

The sidebarPanel does not work, can you tell me why? 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: 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: 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})
})

Every thing given by Batanicheck is correct, but a little modification to the plot get your exact output. Batanicheck给出的每件事都是正确的,但对图进行一些修改即可获得准确的输出。

Replace 更换

> p <- plot_ly(data[data$x==input$letter,], x=group, y=y,type = "bar")

WITH WITH

> p <- plot_ly(data[data$x==input$letter,], x=x, y=y, group=group,type = "bar")

For me it works in browser as well as Rstudio viewer... 对我来说,它可以在浏览器以及Rstudio浏览器中使用...

I cant fully understan what you wnat to see 我不能完全理解你所看到的

but for example if you want to plot only one letter by years 但是例如,如果您只想按年份绘制一个字母

1) You have bad df as example -- A exist only into 2011 1)你有不好的df为例- A只存在到2011年

2) I use 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)) 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) try p <- plot_ly(data[data$x==input$letter,], x=group, y=y,type = "bar") 3)试试p <- plot_ly(data[data$x==input$letter,], x=group, y=y,type = "bar")

Dont forget declare data into ui and server 不要忘记将data声明到uiserver

And work only in browser not into rstudion viewer ( for me) 并且仅在浏览器中工作,而不是在rstudion查看器中工作(对我来说)

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

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