我正在尝试使用新ggvis包绘制堆积区域图.
在ggplot,我设法做到这样:
d<- data.frame(
time=as.numeric( rep( 1:100, 100 ) ),
class=as.factor( sample( 7, 100000, replace=TRUE ) )
)
t <- as.data.frame( table( d$time, d$class ) )
ggplot( t, aes( x=as.numeric( Var1 ), y=Freq, fill=Var2 ) ) +
geom_area( stat="identity" )
Run Code Online (Sandbox Code Playgroud)

有了ggvis,我设法使用条形图在相同的布局中绘制相同的数据:
ggvis( t, x=~as.numeric( Var1 ), y=~Freq, fill=~Var2 )
%>% group_by( Var2 )
%>% layer_bars()
Run Code Online (Sandbox Code Playgroud)

但我不知道怎么说ggvis我想要的是区域,而不是酒吧.layer_areas不存在,都layer_paths和layer_ribbons给我错误的结果.
我玩过路径和缎带的道具,但我无法弄清楚如何分辨ggvis出相互叠加的区域.
使用绘制堆积区域图的正确方法是ggvis什么?
我一直在研究R中的交互式图.我知道有几个包可以创建交互式图,特别是散点图,但我正在寻找某种功能.
例如这个情节.可以将鼠标悬停在按钮上以获得该点后面数据的小数字摘要,即工具提示.
当您拥有包含更多变量的数据集时,通常可以从PCA中探索/可视化分数,或进行多维缩放(MDS).但是如果人们以交互方式绘制数据,就像上面的例子那样,当一个人在这一点上盘旋时,总结并没有给出如此多的信息,即只是一长串的数字.能够生成要显示的自定义绘图会更好,而不仅仅是显示数值.
所以我的问题是:
当一个人在一个散点图中的一个点上盘旋时,是否有可能(在R中可用的某些包中)生成一个可视摘要.这可能是一个条形图,或者只是一些用户指定的绘图函数,它从data.frame中取一行作为参数.
如果可行的话,那将有助于快速了解MDS的结果.
编辑:
以下是在虹膜数据集上执行MDS的一些代码:
library(ggplot2)
library(plotly)
d <- dist(iris[,-5]) # euclidean distances between the rows
fit <- cmdscale(d,eig=TRUE, k=2) # k is the number of dim
# Put coordinates and original data in one data.frame
x <- fit$points[,1]
y <- fit$points[,2]
pDat <- data.frame(x=x,y=y)
pDat <- cbind(pDat,iris)
p <- ggplot(pDat) + geom_point(aes(x,y))
ggplotly(p)
Run Code Online (Sandbox Code Playgroud)
首先,现在工具提示仅包括x,y坐标.我想工具提示包含原始4个变量的值.然后,我想将工具提示显示为条形图,而不是数据点后面的原始4个变量.MDS保留了数据点之间的距离,因此可以用鼠标逐渐悬停,并且看到条形图几乎连续变化,因为距离得以保留.在我的使用案例中,每个点后面有30个变量,因此条形图摘要提供的视觉信息比30个数值更多.
启动ggvis时,我收到消息:
The following object is masked from ‘package:data.table’:
:=
Run Code Online (Sandbox Code Playgroud)
那:=对于运行data.table是必不可少的,它恰好是dplyr的替代品.
我的代码仍然正常运行,但启动消息让我担心,因为我在所有脚本中都使用data.table.
考虑到ggvis的启动消息,在data.table和ggvis中使用:=会产生什么后果?
我运行了一个RStudio Shiny服务器,我从https://github.com/rstudio/ggvis安装了ggvis,但我无法重现服务器中的任何演示示例.
当我使用服务器(3.1.0)中安装的相同版本运行R时,我可以执行以下操作:
> library("shiny")
> library("ggvis")
The ggvis API is currently rapidly evolving. We strongly recommend that you do not rely on this for production, but feel free to explore. If you encounter a clear bug, please file a minimal reproducible example at https://github.com/rstudio/ggvis/issues. For questions and other discussion, please use https://groups.google.com/group/ggvis.
Attaching package: "ggvis"
The following object is masked from "package:stats":
filter
> ggvis::ggvisOutput
function (plot_id = rand_id("plot_id"))
{
ggvisOutputElements(plot_id, spec = NULL, shiny = TRUE) …Run Code Online (Sandbox Code Playgroud) 我无法在闪亮的应用程序中的ggvis绘图中获取输入滑块.这些图表在没有输入滑块的情况下呈现正常但在添加它之后闪亮会抛出此错误:
Listening on http://xxxxxxxxxxxxxx
Error in eval(expr, envir, enclos) : could not find function "compdat" Run Code Online (Sandbox Code Playgroud)
server.R:
library(shiny)
library(ggvis)
data<-data.frame(var1=rnorm(30,5,2.3),var2=rbeta(30,1.5,.8),var3=rnorm(30,10,2.5))
shinyServer(function(input, output,session) {
compdat<-reactive({data[, c(input$xInp,input$yInp)]})
vis1 <-reactive({
compdat %>% ggvis(x= ~compdat()[,1],y= ~compdat()[,2]) %>%
layer_points(fill:="red") %>% layer_smooths(span=input_slider(.1,1,id="scores_ui"))
})
vis1 %>% bind_shiny("scores",controls_id="scores_ui")
vis2<-reactive({
compdat %>% ggvis(x= ~compdat()[,1],y= ~compdat()[,2]) %>%
layer_points(fill:="red") %>% ayer_smooths(span=input_slider(.1,1,id="loadings_ui"))
})
vis2 %>% bind_shiny("loadings",controls_id="loadings_ui")
})Run Code Online (Sandbox Code Playgroud)
ui.R:
shinyUI(fluidPage(
title="PCA Explorer",
h2("Principal Component Explorer"),
fluidRow(
column(6,ggvisOutput("scores"),
uiOutput("scores_ui")),
column(6,ggvisOutput("loadings"),
uiOutput("loadings_ui"))
),
br(),
fluidRow(
column(6,h3("Component Selection"),selectInput('xInp',"X Variable",names(data)), …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用滑块来控制纵向空间数据集中的年份,基本上是一组散点图.我无法弄清楚如何将滑块分配给这个变量 - 你能用ggvis做到吗?
简化的数据集:
data <- data.frame(year=rep(2000:2002, each=23),
x=rnorm(23*3,10), y=rnorm(23*3,10),
count=c(rnorm(23,2), rnorm(23,4), rnorm(23,6)))
Run Code Online (Sandbox Code Playgroud)
我尝试过的:
### This is what is looks like in ggplot2, I'm aiming to be able to toggle
### between these panels
ggplot(data, aes(x, y, size=count)) + geom_point() + facet_grid(~year)
### Here is where I'm at with ggvis
data %>%
ggvis(~x, ~y, size=~count) %>%
layer_points()
# I'm not sure how to assign a variable (year) to a slider, I've been trying
# within the layer_points() function
### I …Run Code Online (Sandbox Code Playgroud) 我正在尝试更改ggvis绘图中的刻度标签.我的数据点是x = c(1,2,3)和y = c(1,2,3).但是,以下代码会导致刻度标签完全没有意义!
library(dplyr)
library(ggvis)
data.frame(x = c(1,2,3), y = c(1,2,3) ) %>%
ggvis(~x,~y ) %>%
layer_lines() %>%
add_axis("x", properties=axis_props(
labels=list(angle=90, fontSize = 10, text = c("one","two","three" ) )
)
)
Run Code Online (Sandbox Code Playgroud)
得到:

我想我也必须格式化刻度,或者至少告诉ggvis哪些刻度标记?
我想得到x轴的整数用于ggvis绘图.
MWE
df <-
structure(list(Factor = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 1L,
2L, 3L, 1L, 2L, 3L), .Label = c("A", "B", "C"), class = "factor"),
X = c(15.5133333333333, 14.63, 14.41, 14.1266666666667, 13.1833333333333,
12.9466666666667, 13.6133333333333, 13.55, 13.5333333333333,
11.5566666666667, 11.3066666666667, 11.4566666666667), Y = c(20L,
20L, 20L, 40L, 40L, 40L, 70L, 70L, 70L, 100L, 100L, 100L)), .Names = c("Factor",
"X", "Y"), row.names = c(NA, -12L), class = "data.frame")
library(ggvis)
ggvis(data=df
, x= ~X
, y= ~Y
, fill= ~Factor
, …Run Code Online (Sandbox Code Playgroud) 我想用Shiny和ggvis来:
1)上传数据集
2)让用户选择2列(x,y)
3)从上传的数据集中返回显示(x,y)的ggvis图
我已经尝试过编辑Shiny Interactivity页面中的示例以及电影资源管理器示例.但是,不显示任何图表.
我认为我的问题是上传数据集,但我不知道从哪里开始...有什么建议吗?
注意 - 我也尝试使用rCharts,但我遇到了类似的问题,没有显示图表.
server.R
library(shiny)
library(dplyr)
library(ggvis)
shinyServer(function(input, output, session) {
fileHeaderNames <- reactive({
infile <- input$datfile
if(is.null(infile))
return(NULL)
d <- read.csv(infile$datapath, header = T)
return(names(d))
})
# dynamic variable names
observe({
updateSelectInput(session, 'x', choices = fileHeaderNames())
updateSelectInput(session, 'y', choices = fileHeaderNames())
}) # end observe
# uploading data set
theData <- reactive({
validate(
need(input$datfile != "", "Please upload a file")
)
infile <- input$datfile
dat <- read.csv(infile$datapath,
header = …Run Code Online (Sandbox Code Playgroud) 我已经阅读过有关SO的类似文章,但无法根据我的具体情况调整答案。我正在使用时间序列数据,并希望将两个不同的数据集合并到同一图中。尽管我可以将数据合并到一个数据框中,但我确实对理解如何引用多个数据集感兴趣。
模拟数据:
require(ggvis)
dfa <- data.frame(
date_a = seq(from= as.Date("2015-06-10"),
to= as.Date("2015-07-01"), by= 1),
val_a = c(2585.150, 2482.200, 3780.186, 3619.601,
0.000, 0.000, 3509.734, 3020.405,
3271.897, 3019.003, 3172.084, 0.000,
0.000, 3319.927, 2673.428, 3331.382,
3886.957, 2859.887, 0.000, 0.000,
2781.443, 2847.377) )
dfb <- data.frame(
date_b = seq(from= as.Date("2015-07-02"),
to= as.Date("2015-07-15"), by= 1),
val_b = c(3250.75429, 3505.43477, 3208.69141,
-2.08175, -27.30244, 3324.62348,
2820.91075, 3250.75429, 3505.43477,
3208.69141, -2.08175, -27.30244,
3324.62348, 2820.91075) )
Run Code Online (Sandbox Code Playgroud)
使用上面提供的数据,我可以使用以下代码创建单独的图:
单独的图:(工作)
dfa %>%
ggvis( x= ~date_a …Run Code Online (Sandbox Code Playgroud)