r - 使用带有自定义图例的 ggplot2 绘制线图
问题描述
我有三个不同的数据集,
> dput(logit_mean)
structure(c(1.34189585608394, 0.847629766112133, 0.502984724184437,
0.293946235601552, 0.206499805664176, 0.226630939673008, 0.340325142928746,
0.533567920732087, 0.792344778383729, 1.10264122118437, 2.10410207183928,
2.39007043292364, 2.7472025599441, 3.13582023485062, 3.51624523959319,
3.84879935612177, 4.09380436638634, 4.21158205233687, 4.16245419592333,
3.9067425790957, 1.46025341360241, 2.14156603885982, 2.75516433443725,
3.29118077503553, 3.7397478353555, 4.09099799009798, 4.3350637139638,
4.46207748165381, 4.46217176786882, 4.32547904730968), .Dim = c(10L,
3L))
> dput(logit_LCL_mean)
structure(c(1.05351660368681, 0.628916609291152, 0.258130717327059,
0.00463564423558981, -0.118711150605381, -0.119775198249695,
-0.00943890367383587, 0.2120834094038, 0.516104794995947, 0.771900259001094,
1.728942625138, 1.93004332451069, 2.1051342500183, 2.32579479415003,
2.55055241931885, 2.774582433554, 2.95504501785115, 3.07188155790784,
3.06353387892773, 2.81246169848574, 1.23766486839515, 1.92345141391083,
2.49077865317756, 2.98569639891182, 3.39229201620524, 3.69476128054215,
3.89382753174118, 4.02023529154679, 4.00128330320072, 3.69675304553835
), .Dim = c(10L, 3L))
> dput(logit_UCL_mean)
structure(c(1.65351862117267, 1.07141678565209, 0.727549044031012,
0.528002186532385, 0.457316509237024, 0.495293082125037, 0.621734110908969,
0.803998291666232, 1.04499958137129, 1.44068951524647, 2.49956079692368,
2.76677000672866, 3.22892029161779, 3.73203241679821, 4.22148180147624,
4.65402060247669, 4.99516431524168, 5.21764445415144, 5.35973642037135,
5.50118639749062, 1.6808252495808, 2.3732336294389, 3.05627843729075,
3.64261086868091, 4.15079922999075, 4.55990311955228, 4.83697444552739,
4.94477232896115, 4.95551580166221, 5.06294719673336), .Dim = c(10L,
3L))
我想用 ggplot2 创建以下图。我可以使用以下代码在没有 ggplot2 的情况下做到这一点
plot(logit_mean[,1]~time, type="n", ylab="",
ylim=c(min(logit_mean,logit_LCL_mean,logit_UCL_mean),
max(logit_mean,logit_LCL_mean,logit_UCL_mean))
,
main="Logit functions", cex.main=1, font.main=1
)
col.set=c("green", "blue", "purple", "deeppink",
"darkorchid","darkmagenta","black","khaki")
for ( g in 1:ncol(logit_mean)){
lines(logit_mean[,g]~time, col=col.set[g])
lines(logit_LCL_mean[,g]~time, col=col.set[g], lty=2)
lines(logit_UCL_mean[,g]~time, col=col.set[g], lty=2)
}
legend("topleft", inset=0.05,legend=paste0("Clus_", 1:ncol(logit_mean)),
col=col.set, lty=1, box.lty=0, cex=0.8)
我怎么能用ggplot2做到这一点。任何帮助表示赞赏。剧情一定和这张图差不多
这是我在第一个数据集上尝试的。如何在同一个情节中包含其他两个
dat=data.frame(tm=c(1:10),logit_mean)
dat_long=melt(dat,id="tm")
col_set=c("green", "blue","green")
ggplot(data=dat_long,
aes(x=tm, y=value, colour=variable)) +
geom_line()+scale_color_manual(values=col_set)+theme_bw()
解决方案
这里的关键是将您的数据以长格式放入单个数据框中,其中一列表示均值,一列表示上下置信区间。然后,您需要添加一列标记集群以及一列用于时间:
df <- data.frame(mean = as.vector(logit_mean),
lower = as.vector(logit_LCL_mean),
upper = as.vector(logit_UCL_mean),
clus = rep(c("clus_1", "clus_2", "clus_3"), each = 10),
time = rep(1:10, 3))
情节本身很简单。我添加了一些样式,使其更像您的基本 R 示例:
ggplot(df, aes(time, mean, colour = clus)) +
geom_line() +
geom_line(aes(y = upper), linetype = 2) +
geom_line(aes(y = lower), linetype = 2) +
scale_colour_manual(values = c("green", "blue", "purple")) +
labs(title = "Logit functions", y = "") +
theme_classic() +
theme(legend.position = c(0.15, 0.75),
plot.title.position = "plot",
plot.title = element_text(hjust = 0.5, size = 16))
虽然我个人认为我更清楚geom_ribbon
:
ggplot(df, aes(time, mean, colour = clus)) +
geom_line() +
geom_ribbon(aes(ymin = lower, ymax = upper, fill = clus),
alpha = 0.1, linetype = 2) +
scale_fill_manual(values = c("green", "blue", "purple")) +
scale_colour_manual(values = c("green", "blue", "purple")) +
labs(title = "Logit functions", y = "") +
theme_bw() +
theme( plot.title.position = "plot",
plot.title = element_text(hjust = 0.5, size = 16))
推荐阅读
- javascript - 角度驱动表单如何禁用在数字输入时写入十进制数字的选项
- php - PHP - 如何在邮件正文中插入换行符?
- .net - 与 WCF 服务通信时值不能为空错误
- python - 如何在 Windows 10 最佳实践中不断运行 python 脚本?
- python - 使用德语变音符号对 pandas 数据框进行排序
- arrays - 有没有办法“自动化/循环”过滤器功能,在谷歌表格中有多行的结果
- sql - 比较 SQL 中的 int 列
- xml-parsing - Oracle:如何从表中的属性中提取 XML 数据
- vb.net - 如何使正在运行的 VB 程序重新从头开始
- typescript - 在 TypeScript 中进行单元测试时,存根依赖项的正确方法是什么?