here,但我有大约 30 个不同的类别,因此手动逐一执行代码会花费相当多的时间...
基本上,我的图是显示不同地区不同物种目击时间(T0 到 T8)的线图。我想生成一个图表,在其中我可以选择一个生态区域并可视化显示该区域中某个物种的每个时间点的平均时间的线,即每个存在的物种一条线(无论是否有 1 或 20 条记录) )我有以下图表
set.seed(123)
species <- rep(c("Species A", "Species B", "Species C"), each = 7)
ecoregions <- rep(letters[1:30], each = 2)
times <- rep(0:7, times = 3)
values <- matrix(rpois(21, 10), ncol = 3)
colnames(values) <- paste0("DaSS", 1:3)
Df2 <- data.frame(Tpoint = rep(values, each = length(ecoregions)),
Time = rep(times, times = length(ecoregions)),
Species = rep(species, times = length(ecoregions)),
Ecoregion = rep(ecoregions, each = length(times)))
这是我的 ggplot 代码,用于创建我想要的构面,但由于有很多生态区,这些生态区很小,所以我想用plotly而不是构面创建一个交互式绘图
p<-Df2%>%
ggplot(aes(x = Times, y = values, group =Species, color=Species)) +
stat_summary(fun = "mean", geom = "line")+
#geom_smooth(method = "loess", se = TRUE) +
labs(title = "Amount of time by species and ecoregion",
x = "Time point",
y = "hours")+
theme_bw()+
theme(legend.position="bottom",
strip.text = element_text(size = 14), # Adjust strip text size
strip.background = element_blank(), # Remove strip background
panel.spacing = unit(1, "lines")
) +facet_wrap("Ecoregion")
我使用了类似下面的代码,但运气不太好...当我使用任何按钮时,数据都不会改变。
p <- Df2 %>%
group_by(Species, Ecoregion) %>%
#summarize(mean_Time = mean(Time)) %>%
plot_ly(x = ~Species, y = ~Time, color = ~Species, type = "scatter", mode = "lines") %>%
layout(
xaxis = list(title = "Species"),
yaxis = list(title = "Time"),
updatemenus = list(
list(type = "buttons",
showactive = FALSE,
buttons = button_list)
)
)
# Show the interactive plot
p
如有任何帮助,我们将不胜感激!
Ecoregion
唯一值的按钮列表,在非聚合数据帧上运行。
button_list = lapply(
unique(Df2$Ecoregion),
\(er) list(
method = "restyle",
args = list("transforms[0].value", er),
label = er
)
)
p <- Df2 %>%
plot_ly(
x = ~Species, y = ~Time, color = ~Species,
type = "scatter", mode = "lines",
transforms = list(
list(
type = 'filter',
target = ~Ecoregion,
operation = '=',
value = unique(Df2$Ecoregion)[1]
)
)
) %>%
layout(
xaxis = list(title = "Species"),
yaxis = list(title = "Time"),
updatemenus = list(
list(
type = "buttons",
showactive = FALSE,
buttons = button_list
)
)
)
# Show the interactive plot
p