2017-03-07 154 views
-1

我試圖開發一個簡單的Shiny APP來顯示條形圖。在下面的示例中(https://shiny.rstudio.com/gallery/telephones-by-region.html),情節是按地區電話。我想簡單一點:只需要一個候選人(我的數據:dados)。R ggplot發生錯誤

主要問題是使用條形碼內部的input命令。原來

barplot(WorldPhones[,input$region]*1000, 
     main=input$region, 
     ylab="Number of Telephones", 
     xlab="Year") 

ggplot(data=dados, aes(x=dados[input$candidato])) + 
    geom_bar(stat="count") 

這樣做可以取代,我得到了錯誤:

Don't know how to automatically pick scale for object of type tbl_df/tbl/data.frame. Defaulting to continuous. Warning: Error in : Discrete value supplied to continuous scale

數據樣本:

Candidato1 Candidato2 Candidato3

 <chr>    <chr>    <chr> 

POSITIVO NEGATIVO POSITIVO POSITIVO
NEGATIVO POSITIVO NEGATIVO NEGATIVO
POSITIVO POSITIVO NEGATIVO NEGATIVO

我該如何解決這個問題?似乎ggplot不能接受我的字符串,但外面閃亮,工作正常。

Tks,Ricardo。

+0

你應該解釋從'input $ candidato'來自哪裏?你想用它做什麼?列中是列名還是列值?如果'input $ candidato'表示列中的過濾器值,那麼列名稱是什麼? –

+0

你可能想要'ggplot(data = dados,aes_string(x = input $ candidato))+ geom_bar(stat =「count」)''。不要在'aes'中使用'$'。 – Axeman

回答

-1

這是很難理解你的問題,其實你應該更簡單介紹一下吧...

雖然看着你的代碼,我可以直接告訴你,問題是這部分代碼:

aes(x=dados[input$candidato]) 

您還沒有定義好,如果input$candidato應該過濾掉的行或列....

我假設你的意思列,則代碼應該看起來像:

aes(x=dados[, input$candidato]) 
+0

這不僅不是使用'ggplot'的正確方法,而且data.frames確實可以通過使用'df [i]'表示法在列上進行索引,因爲它們是'list'。 – Axeman

+0

@Malvina_a對錯誤指定的問題抱歉。但你給我正確的信息。作品! –