如何在 ggvis 中同时缩放点大小和颜色?
How to scale point size and color at the same time in ggvis?
考虑 data.frame 这样的:
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)))
使用 ggvis
,我想生成一个点线图,其中的线按 group
分组。 type==3
的点的大小应该是100,type==1
和type==2
的点的大小都是50。点的颜色应该是green
,[=18] =]和red
分别对应type1
、type2
和type3
。这是我的 ggvis
代码:
df <- data.frame(df,id=1:nrow(df))
all_values <- function(x) {
if(is.null(x)) return(NULL)
row <- df[df$id == x$id, ]
paste0(names(row), ": ", format(row), collapse = "<br />")
}
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",domain = c(1,2,3), range = c(50,50,100)) %>%
scale_nominal("fill",domain = c(1,2,3), range = c('green','blue','red')) %>%
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")
我得到 Error: length(x) not less than or equal to 2
的错误。
为什么会发生这种情况,我该如何解决?
原来scale_nominal("size",domain = c(1,2,3), range = c(50,50,100))
应该换成scale_nominal("size",domain = c(1,2,3), range = c('50','50','100'))
.
错误的罪魁祸首是为 range
定义的值超过 2 个。 range
的定义表明:For numeric values, the range can take the form of a two-element array with minimum and maximum values.
For ordinal data, the range may by an array of desired output values, which are mapped to elements in the specified domain
。在这种情况下,值应该用字符定义。
这应该可以解决您的错误。
考虑 data.frame 这样的:
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)))
使用 ggvis
,我想生成一个点线图,其中的线按 group
分组。 type==3
的点的大小应该是100,type==1
和type==2
的点的大小都是50。点的颜色应该是green
,[=18] =]和red
分别对应type1
、type2
和type3
。这是我的 ggvis
代码:
df <- data.frame(df,id=1:nrow(df))
all_values <- function(x) {
if(is.null(x)) return(NULL)
row <- df[df$id == x$id, ]
paste0(names(row), ": ", format(row), collapse = "<br />")
}
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",domain = c(1,2,3), range = c(50,50,100)) %>%
scale_nominal("fill",domain = c(1,2,3), range = c('green','blue','red')) %>%
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")
我得到 Error: length(x) not less than or equal to 2
的错误。
为什么会发生这种情况,我该如何解决?
原来scale_nominal("size",domain = c(1,2,3), range = c(50,50,100))
应该换成scale_nominal("size",domain = c(1,2,3), range = c('50','50','100'))
.
错误的罪魁祸首是为 range
定义的值超过 2 个。 range
的定义表明:For numeric values, the range can take the form of a two-element array with minimum and maximum values.
For ordinal data, the range may by an array of desired output values, which are mapped to elements in the specified domain
。在这种情况下,值应该用字符定义。
这应该可以解决您的错误。