On aes_string(), factor() and Rstudio's Shiny Package

放肆的年华 提交于 2019-12-12 16:52:24

问题


When I run the following code:

  graph <- ggplot(data = graphData, aes_string(x = input$variable1, y = input$variable1))
  graph <- graph + geom_point( aes_string(colour=input$groupVariable) )

I get the following graph:

Which is problematic because it's not grouped by distinct colors, but rather with shades of blue.

I want this:

I tried to use factor() as in the following:

  graph <- ggplot(data = graphData, aes_string(x = input$variable1, y = input$variable2))
  graph <- graph + geom_point( aes_string(colour=factor(input$groupVariable) ) )

But that just gives me this:

What should I do to get the graph as in the middle image?

Note input is the channel through which Rstudio's Shiny package communicates between it's ui.R and server.R scripts.


回答1:


You can try the following code:

output$plot1 <- renderPlot({

graphData[,input$groupVariable] <- factor(graphData[,input$groupVariable])

ggplot(graphData, aes_string(x=input$variable1, y=input$variable2, color=input$groupVariable)) + geom_point()
})


来源:https://stackoverflow.com/questions/34057280/on-aes-string-factor-and-rstudios-shiny-package

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!