[英]Using dplyr and ggplot for multiple variables
我正在使用 dplyr 对我的数据进行子集化并为一个变量生成多个图表,但我想对多个变量使用相同的子集。 对于样本数据reprex
,
reprex <- structure(list(daycode = c("a", "a", "a", "a", "a", "a", "b",
"b", "b", "b", "b", "b", "c", "c", "c", "c", "c", "c"), status = c("1",
"2", "1", "2", "1", "2", "1", "2", "1", "2", "1", "2", "1", "2",
"1", "2", "1", "2"), IHT = c(20, 65, 31, 62, 19, 58, 27, 27,
28, 28, 23, 25, 27, 26, 59, 34, 16, 18), TJ = c(19L, 141L, 80L,
181L, 24L, 166L, 65L, 103L, 40L, 98L, 45L, 88L, 59L, 86L, 54L,
98L, 21L, 74L)), class = "data.frame", row.names = c(NA, -18L
))
并使用代码
d.plots <- reprex %>%
group_by(status, daycode) %>%
summarise(day.TJ = mean(TJ), sdtj = sd(TJ),
day.IHT = mean(IHT),
sdIHT = sd(IHT)) %>%
mutate(sdTJ.hi = day.TJ + sdtj, sdTJ.lo = day.TJ - sdtj, sdIHT.hi = day.IHT + sdIHT,
sdIHT.lo = day.IHT - sdIHT) %>%
do(plots = ggplot(data = .) +
aes(x = daycode, y = day.TJ, group = 1) +
geom_col(fill = "red")+
geom_text(aes(y = day.TJ + 2, label = signif(day.TJ, 2))) +
geom_line(aes(y = sdTJ.hi), size = 0.8) +
geom_line(aes(y = sdTJ.lo), size = 0.8)+
labs(x = "Day", y = "Avg Jumps" ) +
theme_bw() +
ggtitle(unique(.$status)))
d.plots$plots
我可以获得变量TJ
的两个图表,但也想获得变量 IHT 的相同图表。
我会将 pivot 和 dataframe 转换为更长的格式,因此您将在列中拥有变量( IHT
或JT
),并且可以在不重复代码的情况下使用它。
我会这样做并使用您的代码来准备绘图。 我还将提出更好的数据可视化; 当然我不知道你打算用这些做什么,所以可以随意忽略它。
library(dplyr)
library(tidyr)
library(ggplot2)
d.plots<-reprex %>%
pivot_longer(cols = c('IHT', 'TJ')) %>%
group_by(status, name, daycode) %>%
summarise(day = mean(value),
sd = sd(value)
) %>%
mutate(sd.hi = day + sd,
sd.lo = day - sd) %>%
do(plots = ggplot(data = .) +
aes(x = daycode, y = day, group = 1) +
geom_col(fill = "red")+
geom_text(aes(y = day + 2, label = signif(day, 2))) +
geom_line(aes(y = sd.hi), size = 0.8) +
geom_line(aes(y = sd.lo), size = 0.8)+
labs(x = "Day", y = "Avg Jumps" ) +
theme_bw() +
ggtitle(unique(paste(.$status, .$name))))
d.plots$plots
#> [[1]]
#>
#> [[2]]
#>
#> [[3]]
#>
#> [[4]]
这是我提出的可视化,它用点交换条并利用facet
特征:
reprex %>%
pivot_longer(cols = c('IHT', 'TJ')) %>%
group_by(status, name, daycode) %>%
summarise(day = mean(value),
sd = sd(value)
) %>%
mutate(sd.hi = day + sd,
sd.lo = day - sd) %>%
ggplot(aes(x = daycode, y = day, group = 1)) +
facet_grid(status ~ name) +
geom_point(color = 'red2') +
geom_text(aes(label = signif(day, 2)), nudge_y = 10) +
geom_ribbon(aes(ymin = sd.lo, ymax = sd.hi), alpha = .2) +
theme_bw()
由代表 package (v0.3.0) 于 2020 年 6 月 16 日创建
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.