r - 使用 ggplot 和管道在多因子图的图例中拟合线型
问题描述
以下代码用于使用颜色和线型的组合绘制许多不同的条件。它使用一条线绘制“R”,使用虚线绘制“W”。
我想将图例分成两部分:“H C”和“I C”的条件,以及“R”和“W”的模型类型。在Condition中,我们想要表示线条的颜色。在Model type中,我们想要表示线条的类型(例如黑色的虚线和非虚线)。
由于我使用的是管道,因此到目前为止我还没有找到解决方案。
library(ggplot2)
library(scales)
tribble( ~y, ~cb, ~z, ~x,
1 , 0, "H C R" ,1,
2 , 1, "H C R" ,1,
2 , 0, "I C R" ,1,
3 , 1, "I C R" ,1,
1.5 , 0, "H C W" ,1,
2 , 1, "H C W" ,1,
2 , 0, "I C W" ,1,
2 , 1, "I C W" ,1,
3 , 0, "H C R" ,2,
3 , 1, "H C R" ,2,
0.5 , 0, "I C R" ,2,
2 , 1, "I C R" ,2,
2 , 0, "H C W" ,2,
2 , 1, "H C W" ,2,
1 , 0, "I C W" ,2,
1 , 1, "I C W" ,2)-> datos
aux<-datos %>%
group_by(x, z, cb) %>%
summarise(media = mean(y),
desvio = rnorm(1),
error_est = desvio / sqrt(n()),
intervalo_sup = media + (2*error_est),
intervalo_inf = media - (2*error_est)) #%>%
library(RColorBrewer)
display.brewer.pal(11,name = "Spectral")
brewer.pal(n = 11, name = "Spectral")
# Que la línea de condición "H C" apareciera en el color anaranjado que utiliza la paleta "Spectral".
# Que la línea de condición "I C" apareciera en el color verde oscuro que utiliza la paleta "Spectral".
colores<-brewer.pal(n = 11, name = "Spectral")[c(3,3,9,9)]
ggplot() +
geom_line(data=aux%>%filter(grepl("W",z)),
aes(x = x, y = media, color = z,group = z),
size=0.5,
linetype = "dashed") +
geom_errorbar(data=aux%>%filter(grepl("W",z)),aes(x = x, color = z,
ymax = intervalo_sup,
ymin = intervalo_inf),
width=0.3) +
geom_line(data=aux%>%filter(!grepl("W",z)),
aes(x = x, y = media, color = z,group = z),
size=0.5) +
geom_errorbar(data=aux%>%filter(!grepl("W",z)),
aes(x = x, color = z,
ymax = intervalo_sup,
ymin = intervalo_inf),
width=0.3)+
labs(x = "x", y = "y", color = "Condition") +
scale_color_manual(values=colores) +
scale_x_continuous(breaks = seq(1,7, by=1)) +
theme(legend.position="bottom", legend.text=element_text(size=12)) +
guides(color=guide_legend(ncol=2)) +
theme(axis.text=element_text(size=14),
axis.title=element_text(size=14))+
facet_wrap(~cb)
解决方案
解决方案使用stringr
.
library(dplyr)
library(ggplot2)
library(scales)
library(RColorBrewer)
library(stringr)
#Selection of desired colors
display.brewer.pal(11,name = "Spectral")
colores<-brewer.pal(n = 11, name = "Spectral")[c(3,9)]
#Stats
datos %>%
group_by(x, z, cb) %>%
summarise(media = mean(y),
desvio = rnorm(1),
error_est = desvio / sqrt(n()),
intervalo_sup = media + (2*error_est),
intervalo_inf = media - (2*error_est))%>%
#Two columns for color and linetype (be careful when using stringr)
mutate(Model=str_split(z," ")[[1]][3],
Condition=str_remove(z,Model)
)%>%
#Plot
ggplot(aes(x = x, y = media, color = Condition,group = z)) +
geom_line(aes(linetype=Model, color=Condition),size=0.5)+
geom_errorbar(aes(x = x, color = Condition,
ymax = intervalo_sup,
ymin = intervalo_inf),
width=0.3)+
scale_color_manual(values=colores) +
scale_x_continuous(breaks = seq(1,7, by=1)) +
theme(legend.position="bottom", legend.text=element_text(size=12)) +
theme(axis.text=element_text(size=14),
axis.title=element_text(size=14))+
facet_wrap(~cb)+
# Legends' title
guides(color=guide_legend("Condition"),
linetype=guide_legend("Model"))
推荐阅读
- c - (x % y) 和 (x & (y-1)) 是否等效?
- javascript - Javascript 自上而下的运动问题
- java - 在将 java 项目导出为 JAR 并尝试在 ECLIPSE 的动态 Web 项目中使用它后,找不到资源文件
- javascript - 带有firestore的云功能找不到任何数据
- php - Woocommerce WorldPay 支付网关状态
- django - DRF M2M 嵌套序列化器字段错误地键入为字符串
- algorithm - 当我们评估运行时,不等式符号重要吗?
- html - HTML 电子邮件 iPhone 媒体查询全宽 TD 不起作用
- pandas - concise way of flattening multiindex columns
- linux - 我有 apache 软件版本的文件记录,需要根据版本对它们进行排序