Jac*_*son 16 statistics r ggplot2 ggvis
有谁知道是否可以用ggvis以交互方式更改x和y轴的变量?我可以改变数据点的大小,它们的位置和不透明度,但是如果可以允许用户从下拉列表中选择一个变量来成为x/y轴的数据,我就无法解决.
Adr*_*ira 16
你可以这样做:
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()
Run Code Online (Sandbox Code Playgroud)
(关键是使用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))
Run Code Online (Sandbox Code Playgroud)
但是有一些使用as.name(http://ggvis.rstudio.com/properties-scales.html)的提示:
var <- "mpg"
prop("x", as.name(var))
Run Code Online (Sandbox Code Playgroud)
该ggvis包被设计为与dplyr例如汇总数据结合使用.该dplyr软件包还重新出口的magrittr管道运营商(%>%见README.md),这使得有工作ggvis'的实施 图形的语法尤其直观的(见本文由这些包的作者,哈德利韦翰).
下面我将说明如何使用该input_select()函数来更改x模型并y保持常量.
首先,我们需要加载两个必需的库:
library(dplyr)
library(ggvis)
Run Code Online (Sandbox Code Playgroud)
现在我们可以绘制一个data.frame(我正在使用内置的预加载iris):
iris %>%
ggvis(x = input_select(c('Petal.Width', 'Sepal.Length'), map = as.name)) %>%
layer_points(y = ~Petal.Length, fill = ~Species)
Run Code Online (Sandbox Code Playgroud)
输出是:
使用input_select这个可以改为:
如果你不想使用dplyr/ magrittr它会是这样的:
p <- ggvis(iris, x = input_select(c('Petal.Width', 'Sepal.Length'), map = as.name))
layer_points(p, y = ~Petal.Length, fill=~Species)
Run Code Online (Sandbox Code Playgroud)
您目前无法直接执行此操作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.
Run Code Online (Sandbox Code Playgroud)
因此,解决方案是使用shiny和输入变量并反应性地定义数据集.这是你的服务器.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")
})
Run Code Online (Sandbox Code Playgroud)
和你的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")
)
)
))
Run Code Online (Sandbox Code Playgroud)