2017-03-07 141 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对错误指定的问题抱歉。但你给我正确的信息。作品! –