[英]ggplot Converted into ggplotly Not Displayed all charts in Shiny
我在下拉列表中有 3 個圖,使用 ggplotly [convert from ggplot] “bar”、“area”、“histogram”。 根據選擇,相應的圖表將顯示在“rfp”plotly 區域 [一次,我只選擇一個圖]。 但我面臨的問題是,我無法根據選擇看到前兩個圖表“條形圖”、“區域”。 根據選擇,我只能將第三張圖表視為 plotly 區域的一部分。 [但是當我使用ggplot時,我沒有遇到這個問題]
ui.r
tabPanel('Charts', plotlyOutput("rfp"))
服務器.r
output$rfp <- renderPlotly({
if ((input$ChartType=="Bar" )){
SimBar <-ggplot(dataset, aes(x = fct_rev(fct_infreq(dataset[,prd])), y=..count..)) +
geom_bar(stat="count")
print(ggplotly(SimBar))}
if ((input$ChartType=="Area" ){
Area <- ggplot(dataset, aes_string(x=dataset[,prd]))+
geom_density(stat = "bin", alpha=0.5)
print(ggplotly(Area))}
if ((input$ChartType=="Histogram" ){
Hist <- ggplot(dataset, aes((x=dataset[,prd])))+
geom_histogram (stat = "count")
print(ggplotly(Hist))}
}
任何“ renderXXX
”都像 function 一樣工作:它返回其主體的最后一條語句的結果。 在這里,您的最后一條語句是if(input$ChartType=="Histogram"){... }
。 這是NULL
如果input$ChartType
ChartType 不是"Histogram"
。 您可以執行以下操作:
output$rfp <- renderPlotly({
if(input$ChartType=="Bar"){
gg <- ggplot(dataset, aes(x = fct_rev(fct_infreq(dataset[,prd])), y=..count..)) +
geom_bar(stat="count")
}else if(input$ChartType=="Area"){
gg <- ggplot(dataset, aes_string(x=dataset[,prd])) +
geom_density(stat = "bin", alpha=0.5)
}else if(input$ChartType=="Histogram"){
gg <- ggplot(dataset, aes((x=dataset[,prd]))) +
geom_histogram(stat = "count")
}
ggplotly(gg)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.