闪亮:动态更改ggplot2中使用的列 [英] Shiny: Change column used in ggplot2 dynamically

查看:74
本文介绍了闪亮:动态更改ggplot2中使用的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试创建一个Shiny应用程序,您可以在其中为每个'selectizeInput'选择ggplot的x轴.

I tried creating a Shiny app where you can choose the x-Axis of a ggplot per 'selectizeInput'.

我知道图库示例,在此之前,选择所需的列.因为在我的情况下,我希望数据结构有点复杂,所以可以动态更改aes()中的x =属性.

I know about the Gallery Example, where this was solved by pre-selecting the desired column. Because in my case the data structure is a bit complex I would prefer, when it is possible to change the x = attribute in aes() dynamically.

为了更好地理解,我添加了一个最小的工作示例.不幸的是,ggplot使用输入作为值,而不是使用相应的列.

For better understanding I added a minimal working example. Unfortunately ggplot uses the input as value, instead to use the corresponding column.

library(shiny)
library(ggplot2)


# Define UI for application that draws a histogram
ui <- shinyUI(fluidPage(

   # Application title
   titlePanel("Select x Axis"),       

   sidebarLayout(
      sidebarPanel(
        selectizeInput("xaxis", 
                       label = "x-Axis",
                       choices = c("carat", "depth", "table"))            
      ),          

      mainPanel(
         plotOutput("Plot")
      )
   )
))

server <- shinyServer(function(input, output) {

   output$Plot <- renderPlot({
     p <- ggplot(diamonds, aes(x = input$xaxis, y = price))
     p <-p + geom_point()
     print(p)
   })
})

# Run the application 
shinyApp(ui = ui, server = server)

推荐答案

aes使用NSE(非标准评估).这对于交互使用非常有用,但对于编程却不是那么好.因此,有两个SE(标准评估)替代方案,aes_(以前为aes_q)和aes_string.第一个使用带引号的输入,第二个使用字符串输入.在这种情况下,使用aes_string可以很容易地解决该问题(因为selectizeInput无论如何都会给我们一个字符串).

aes uses NSE (non-standard evaluation). This is great for interactive use, but not so great for programming with. For this reason, there are two SE (standard evaluation) alternatives, aes_ (formerly aes_q) and aes_string. The first takes quoted input, the second string input. In this case, the problem can be quite easily solved by using aes_string (since selectizeInput gives us a string anyways).

ggplot(diamonds, aes_string(x = input$xaxis, y = 'price')) +
  geom_point()

这篇关于闪亮:动态更改ggplot2中使用的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆