闪亮:动态更改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()