带有交互式 y 的闪亮 ggplot 绘制不正确
shiny ggplot with interactive y does not plot correctly
我正在尝试创建一个界面,用户可以在其中 select 从下拉菜单中输入 y 变量,然后可以观察带有观察值的条形图 selected。
但是,输出不正确,因为应用程序无法读取我的 Y 变量。每当我从下拉菜单中更改 Y 变量时,图表根本没有改变。
附件是我的 csv 数据 sheet 的屏幕截图,以及我从 运行 下面的代码中得到的不正确的输出图表:
shinyUI(fluidPage(
titlePanel("Volvo Sales Analysis"),
fluidRow(
column(4,selectInput("Year",
label = "Choose a year",
choices = as.list(c("Y2004","Y2005","Y2006","Y2007","Y2008","Y2009","Y2010","Y2011","Y2012",
"Y2013", "Y2014", "Y2015")),selected = "Y2015")),
column(12,plotOutput("brandplot"))
)
))
shinyServer(
function(input, output) {
brand_sales<-read.csv("C:/Shiny R/Sales by brand.csv", header=TRUE,check.names=FALSE)
output$brandplot= renderPlot({
ggplot(brand_sales,aes(x=brand_sales$Brands,y=input$Year,fill=Brands))+xlab("Brands")+ylab("Sales Volume")+geom_bar(stat="identity")
})
})
在 "as.list(c("Y2004","Y2005"....)" 函数中我没有使用 "colnames(brand_sales)" 因为如果我这样做,列名 "Brand" 将也显示在下拉菜单中,我不希望这样。我只希望用户在 2004 年到 2015 年之间 select 一年。
在引用 ggplot 中的列时,我也尝试过 "aes_string()"。但输出页面生成错误 "object 'Brands' not found".
感谢 aosmith 的回答,我得以解决问题并让图表正确显示。事实证明,ggplot 的正确代码应该是:
"ggplot(brand_sales,aes_string(x="Brands",y=input$Year,fill="Brands"))+xlab("Brands")+ylab("Sales Volume")+geom_bar(统计="identity")"
关键是用引号将 X 和 Fill 变量括起来,因为这是在 aes_string 中命名列的正确方法。
再次感谢aosmith和MrFlink的投入!
我正在尝试创建一个界面,用户可以在其中 select 从下拉菜单中输入 y 变量,然后可以观察带有观察值的条形图 selected。 但是,输出不正确,因为应用程序无法读取我的 Y 变量。每当我从下拉菜单中更改 Y 变量时,图表根本没有改变。
附件是我的 csv 数据 sheet 的屏幕截图,以及我从 运行 下面的代码中得到的不正确的输出图表:
shinyUI(fluidPage(
titlePanel("Volvo Sales Analysis"),
fluidRow(
column(4,selectInput("Year",
label = "Choose a year",
choices = as.list(c("Y2004","Y2005","Y2006","Y2007","Y2008","Y2009","Y2010","Y2011","Y2012",
"Y2013", "Y2014", "Y2015")),selected = "Y2015")),
column(12,plotOutput("brandplot"))
)
))
shinyServer(
function(input, output) {
brand_sales<-read.csv("C:/Shiny R/Sales by brand.csv", header=TRUE,check.names=FALSE)
output$brandplot= renderPlot({
ggplot(brand_sales,aes(x=brand_sales$Brands,y=input$Year,fill=Brands))+xlab("Brands")+ylab("Sales Volume")+geom_bar(stat="identity")
})
})
在 "as.list(c("Y2004","Y2005"....)" 函数中我没有使用 "colnames(brand_sales)" 因为如果我这样做,列名 "Brand" 将也显示在下拉菜单中,我不希望这样。我只希望用户在 2004 年到 2015 年之间 select 一年。
在引用 ggplot 中的列时,我也尝试过 "aes_string()"。但输出页面生成错误 "object 'Brands' not found".
感谢 aosmith 的回答,我得以解决问题并让图表正确显示。事实证明,ggplot 的正确代码应该是:
"ggplot(brand_sales,aes_string(x="Brands",y=input$Year,fill="Brands"))+xlab("Brands")+ylab("Sales Volume")+geom_bar(统计="identity")"
关键是用引号将 X 和 Fill 变量括起来,因为这是在 aes_string 中命名列的正确方法。
再次感谢aosmith和MrFlink的投入!