Does anyone know if it is possible to change the variables for the x and y axis interactively with ggvis? I can change the size of the data points, their position and opacity, but I can't work out if its possible to allow the user to select a variable from a dropdown that will become the data for the x/y axis.
5 回答
你可以这样做:
library('ggvis');
mtcars %>% ggvis(~mpg, input_select(names(mtcars), map = as.name)) %>% layer_lines()
# or specify by hand
mtcars %>% ggvis(~mpg, input_select(c('wt', 'disp'), map = as.name)) %>% layer_lines()
(关键是使用 map 和合适的函数,在这种情况下 as.name() 可以,但如果您有特殊需要,您可以创建自己的)
请参阅 input_select 的文档:http ://www.rdocumentation.org/packages/ggvis/functions/input_select
描述闪亮解决方案的答案中引用的交互性文档(嗯,我需要信誉点来发布超过 2 个链接,所以我不能这样做,但那里给出了链接!)表明这是可能的(与该答案指出)但那里提供的语法不起作用:
prop(x = input_select(c("disp", "wt")), constant = FALSE)
# which is to be used with props:
props(prop(x = input_select(c("disp", "wt")), constant = FALSE))
但是有一些使用 as.name 的提示(http://ggvis.rstudio.com/properties-scales.html):
var <- "mpg"
prop("x", as.name(var))
该ggvis
软件包旨在与 结合使用dplyr
,例如汇总数据。该dplyr
包还重新导出了magrittr
管道运算符(%>%
请参阅README.md),这使得使用ggvis
' 图形语法的实现特别直观(另请参阅这些包的作者 Hadley Wickham 的这篇文章)。
下面我将说明如何使用该input_select()
函数来更改x
模型的值并y
保持常数。
首先我们需要加载两个必需的库:
library(dplyr)
library(ggvis)
现在我们可以绘制一个data.frame
(我正在使用内置的 pre-loaded iris
):
iris %>%
ggvis(x = input_select(c('Petal.Width', 'Sepal.Length'), map = as.name)) %>%
layer_points(y = ~Petal.Length, fill = ~Species)
输出是:
使用input_select
this 可以更改为:
如果您不想使用dplyr
/magrittr
它看起来像这样:
p <- ggvis(iris, x = input_select(c('Petal.Width', 'Sepal.Length'), map = as.name))
layer_points(p, y = ~Petal.Length, fill=~Species)
您目前无法直接执行此操作ggvis
(v0.3)。从文档中:
Currently, interactive inputs can only be used in two places:
1. as arguments to transforms: layer_smooths(span = input_slider(0, 1))
2. as properties: props(size = input_slider(10, 1000))
This means that interactive inputs can only modify the data, not the underlying plot specification.
In other words, with only basic interactivity there’s no way to add or remove layers, or switch between different datasets.
This is a reasonable limitation because if you’re doing exploration you can always create a new ggvis with R code, or if you’re polishing a plot for presentation, you can embed it in a Shiny app and gain full control over the plot.
因此,解决方案是使用shiny
变量并为其提供输入,然后被动地定义数据集。这是你的server.R:
library(shiny);library(ggvis)
shinyServer(function(input, output) {
plotData <- reactive({
df <- iris[,c("Sepal.Width",input$yVariable,"Species")]
names(df) <- c("x","y","fill")
df
})
reactive({ plotData() %>% ggvis(x=~x,y=~y,fill=~fill) %>%
layer_points() %>%
add_axis("x", title = "Sepal.Width") %>%
add_axis("y", title = input$yVariable) %>%
add_legend("fill", title = "Species")
}) %>% bind_shiny("ggvisPlot")
})
和你的 ui.R:
library(shiny);library(ggvis)
shinyUI(fluidPage(
titlePanel("ggvis with changing data-set"),
sidebarLayout(
sidebarPanel(
selectInput("yVariable", "Y Variable:",
c("Petal.Width" = "Petal.Width",
"Petal.Length" = "Petal.Length"),selected = "Petal.Width")
),
mainPanel(
ggvisOutput("ggvisPlot")
)
)
))
您还可以将绘图构建为交换轴的闪亮反应函数。ggvis重绘情节时可能会有闪光,但会产生你想要的效果。
这修改了上面ideamotor的代码;我还修改了它以使用响应函数而不是响应数据作为 ggvis 的输入,这允许 ggvis ......哦,试试吧,你会看到:
library(shiny);library(ggvis)
shinyServer(function(input, output) {
plotData <- reactive({
df <- iris[,c("Sepal.Width",input$yVariable,"Species")]
names(df) <- c("x","y","fill")
df
})
reactive({
plt <- **plotData** %>% ggvis(fill=~fill) %>%
add_legend("fill", title = "Species")
if (**input$someCheckBox**) {
plt <- plt %>%
layer_points(x = ~x, y = ~y) %>%
add_axis("x", title = "Sepal.Width") %>%
add_axis("y", title = input$yVariable)
} else {
plt <- plt %>%
layer_points(x = ~y, y = ~x) %>%
add_axis("y", title = "Sepal.Width") %>%
add_axis("x", title = input$yVariable)
}
plt
}) %>% bind_shiny("ggvisPlot")
})
是的。您可以执行以下操作:
library(ggvis)
mtcars %>%
ggvis(x = ~mpg, y = input_select(label = "Choose what to plot:",
choices = names(mtcars),
selected = "cyl",
map = as.name)) %>%
layer_points()
如果你想选择这两个变量,只需对 x 执行相同的操作。