我发现这段代码完全符合我的要求,但显然它使用的是用 charlatan 创建的假数据集。它所做的是将数据表的每一行变成可选择的图形线。它为每一行启用工具提示,并在选择一个时淡化其他行。
library(ggplot2)
library(ggiraph)
library(data.table)
library(charlatan)
species <- ch_taxonomic_species(n = 10)
dat <- lapply(species, function(species, n){
data.table(
date = as.Date(seq_len(n), origin = "2018-10-25"),
sales = cumsum(runif(n, -1, 1)),
species = species,
name = ch_name(n = n)
)
}, n = 200)
dat <- rbindlist(dat)
gg <- ggplot(dat, aes(x = date, y = sales,
colour = species, group = species)) +
geom_line_interactive(aes(tooltip = name, data_id = species)) +
scale_color_viridis_d() +
labs(title = "move mouse over lines")
x <- girafe(ggobj = gg, width_svg = 8, height_svg = 6,
options = list(
opts_hover_inv(css = "opacity:0.1;"),
opts_hover(css = "stroke-width:2;")
))
x
我已经有了我的数据集,调用g
它我已经能够制作一个 ggplot 图。我的代码如下所示:
g <- ggplot() +
geom_line(data = dat22, aes(x=YEAR, y=RO_MM, group = HUC8, color= HUC8))+
geom_line(data = dat21, aes(x=YEAR, y=RO_MM, group = HUC8, color= HUC8))+
geom_line(data = dat20, aes(x=YEAR, y=RO_MM, group = HUC8, color= HUC8))+
geom_line(data = dat19, aes(x=YEAR, y=RO_MM, group = HUC8, color= HUC8))+
geom_line(data = dat18, aes(x=YEAR, y=RO_MM, group = HUC8, color= HUC8))+
geom_line(data = dat17, aes(x=YEAR, y=RO_MM, group = HUC8, color= HUC8))+
geom_line(data = dat16, aes(x=YEAR, y=RO_MM, group = HUC8, color= HUC8))+
geom_line(data = dat15, aes(x=YEAR, y=RO_MM, group = HUC8, color= HUC8))+
geom_line(data = dat14, aes(x=YEAR, y=RO_MM, group = HUC8, color= HUC8))+
geom_line(data = dat13, aes(x=YEAR, y=RO_MM, group = HUC8, color= HUC8))+
geom_line(data = dat12, aes(x=YEAR, y=RO_MM, group = HUC8, color= HUC8))+
geom_line(data = dat11, aes(x=YEAR, y=RO_MM, group = HUC8, color= HUC8))+
scale_color_manual(values = c("gray","gray","gray","gray","gray","gray","gray","gray","gray","gray","gray","black"))
dat
我从中创建图表中每一行的每个表看起来都与此类似(但更大):
dat1 <- read.table(text = "HUC8 YEAR RO_MM
bcc1_45Fall_1020004 1961 112
bcc1_45Fall_1020004 1962 243.7
bcc1_45Fall_1020004 1963 233.3
bcc1_45Fall_1020004 1964 190.3
bcc1_45Fall_1020004 1965 200.1
bcc1_45Fall_1020004 1966 220.8
bcc1_45Fall_1020004 1967 130.4
bcc1_45Fall_1020004 1968 90
bcc1_45Fall_1020004 1969 211.2
bcc1_45Fall_1020004 1970 157.6
bcc1_45Fall_1020004 1971 221
bcc1_45Fall_1020004 1972 223.5
bcc1_45Fall_1020004 1973 166.7
bcc1_45Fall_1020004 1974 60
bcc1_45Fall_1020004 1975 144.8
bcc1_45Fall_1020004 1976 150.6
bcc1_45Fall_1020004 1977 129.9
bcc1_45Fall_1020004 1978 223.4
bcc1_45Fall_1020004 1979 235.3
bcc1_45Fall_1020004 1980 143.4
bcc1_45Fall_1020004 1981 55.4
bcc1_45Fall_1020004 1982 214.8
bcc1_45Fall_1020004 1983 217.1
bcc1_45Fall_1020004 1984 126.5
bcc1_45Fall_1020004 1985 106.7",
header = TRUE)
我创建了多线 ggplot,因为在使用 之后我无法找到一种方法从数据集中的每个数据块中制作单独的图形线rbind
,所以我单独做了。我能够创建这个图表:
但我希望它是交互式的。谁能告诉我如何将我的代码用于这个 ggiraph 代码?我希望能够突出显示/选择每一行以直观地将其与黑线进行比较。
我尝试将其添加到我的代码末尾:
geom_line_interactive(aes(tooltip = HUC8, data_id = HUC8)) +
labs(title = "HUC8-01020004")
g
它说它“找不到HUC8”。我已经尝试过了,结果相同:
my_giraph <- g + geom_line_interactive(
aes(data_id = HUC8), size = 1)
girafe(code = print(my_giraph))
还有很多其他的事情,但我没有运气。
寻找@David Gohel!(谁写了 ggiraph 代码)还有人知道吗?