[英]How to produce a ggvis plot similar to this ggplot2 one
I'm familiar with ggplot2, here is the ggplot2 code for producing what I want: 我对ggplot2很熟悉,这是用于生成所需内容的ggplot2代码:
library(ggplot2)
library(scales)
set.seed(100)
df <- data.frame(t = rep(seq(from=as.POSIXct('00:15:00',format='%H:%M:%S'),
to=as.POSIXct('24:00:00',format='%H:%M:%S'),by='15 min'),times=2),
y = c(rnorm(96,10,10),rnorm(96,40,5)),
group = factor(rep(1:2,each=96)),
type = factor(rep(1:3,each=64)))
ggplot(data=df,aes(x=t,y=y,col=type))+geom_point(aes(size=type))+
geom_line(aes(group=group))+
scale_x_datetime(labels = date_format('%H:%M', tz = "Asia/Taipei"),
breaks = date_breaks('2 hours'))+
scale_colour_manual(values = c('red','blue','green'))
This plot takes line group, line type, point color, point size and x-axis time format into consideration. 该图考虑了线组,线型,点颜色,点大小和x轴时间格式。 I want to produce a similar plot just like this with
ggvis
and use add_tooltip
to display the point's information (all variables) when hovering. 我想用
ggvis
生成类似的图,并在悬停时使用add_tooltip
显示点的信息(所有变量)。 But I found it hard to specify the blue, red and green colour. 但是我发现很难指定蓝色,红色和绿色。 The
ggvis
code that I tried is like this: 我尝试的
ggvis
代码如下所示:
df <- data.frame(df,id=1:nrow(df))
ggvis(data=df,x=~t,y=~y,stroke=~group) %>%
layer_points(fill=~type,size=~type, key:=~id, fillOpacity := 0.5,
fillOpacity.hover := 0.8,size.hover := 500) %>%
scale_nominal("size", range = c(50,200)) %>%
layer_lines() %>%
add_tooltip(all_values,'click') %>%
add_legend(scales=c("fill","size"), properties = legend_props(legend = list(y = 150))) %>%
set_options(duration = 0) %>%
add_axis(type="x",format="%H:%M")
Could someone offer me some help? 有人可以帮我些忙吗?
This is my replication of your ggplot2 graph: 这是我对ggplot2图的复制:
ggvis(data=df,x=~t,y=~y,opacity=~group,stroke=~type) %>%
layer_points(fill=~type,size=~type, key:=~id, fillOpacity := 0.5,fillOpacity.hover := 0.8,size.hover := 500) %>%
layer_lines() %>%
scale_nominal("size", range = c(50,200)) %>%
add_tooltip(all_values,'click') %>%
add_legend(scales=c("fill","size"), properties = legend_props(legend = list(y = 150))) %>%
set_options(duration = 0) %>%
add_axis(type="x",format="%H:%M") %>%
scale_ordinal("fill", range = c("red", "blue", "green")) %>%
scale_ordinal("stroke", range = c("red", "blue", "green")) %>%
scale_ordinal("opacity", range = c(1, 1))
Note that to perfectly replicate your original plot, I add a auxiliary mapping to opacity to set the lines apart while keeping stroke mapping to type 请注意,为完美复制原始图,我向不透明度添加了辅助映射,以将线条分开,同时保持笔划映射为类型
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.