假设我有以下模拟数据:
example <- structure(list(country = c("Italy", "Italy", "Italy", "Italy",
"Italy", "Italy", "Italy", "Italy", "Italy", "Austria", "Austria",
"Austria", "Austria", "Austria", "Austria", "Austria", "Austria",
"Austria", "Germany", "Germany", "Germany", "Germany", "Germany",
"Germany", "Germany", "Germany", "Germany"), date = c(1000, 1200,
1300, 1400, 1500, 1600, 1700, 1750, 1800, 1000, 1200, 1300, 1400,
1500, 1600, 1700, 1750, 1800, 1000, 1200, 1300, 1400, 1500, 1600,
1700, 1750, 1800), fit.0.025quant = c(0.828708096446602, 2.4420979507703,
2.62414677055878, 2.63329989559212, 2.53354700323539, 1.65000976000248,
0.752830563369601, 0.662779471528981, 0.633370287301323, 0.631451022500498,
0.649758893615039, 0.650073875566409, 0.650081079761908, 0.650081254451237,
0.650081079637564, 0.650073875566409, 0.649758893615039, 0.631451022500498,
0.631451022500498, 0.649758893615039, 0.650073875566409, 0.650081079761908,
0.650081254451237, 0.650081079637564, 0.650073875566409, 0.649758893615039,
0.631451022500498), fit.mean = c(1.1903534469417, 2.83004352114819,
2.9806558994056, 2.9875417232584, 2.90452442326781, 1.99995962905406,
1.09518775382276, 1.01031925709265, 1.00141486550036, 0.999999948657743,
0.999999976399706, 0.999999973245836, 0.999999973352837, 0.999999968778261,
0.999999973272607, 0.999999973245836, 0.999999976399706, 0.999999948657743,
0.999999948657743, 0.999999976399706, 0.999999973245836, 0.999999973352837,
0.999999968778261, 0.999999973272607, 0.999999973245836, 0.999999976399706,
0.999999948657743), fit.0.975quant = c(1.61387277731566, 3.17388806195699,
3.32641599958393, 3.33420679881888, 3.24684592681982, 2.34991876603736,
1.46568276539042, 1.36373981325973, 1.37069925216457, 1.36867909649403,
1.35026553811603, 1.34998364436204, 1.34997767551695, 1.34997753475153,
1.34997767544381, 1.34998364436204, 1.35026553811603, 1.36867909649403,
1.36867909649403, 1.35026553811603, 1.34998364436204, 1.34997767551695,
1.34997753475153, 1.34997767544381, 1.34998364436204, 1.35026553811603,
1.36867909649403)), row.names = c(NA, 27L), class = "data.frame")
共有三组(country人).
我想对每country个点使用"pointrange"函数,这样对于每date个点,一个点的平均值等于对应的fit.mean,上点等于对应的"fit.0.975quant",下点等于"fit.0.025quant"
目前,我只能建议将数据切碎,然后分别绘制每country个数据的图表,如下所示:
example.italy <- example[-c(10:27), ]
example.italy$date <- as.factor(example.italy$date)
figure <- ggplot(example.italy,
aes(x = date, y = fit.mean)) + aes(y = fit.mean, group=1) + xlab("Time periods") + ylab("DV") + theme(legend.position = "none") + annotate("pointrange", x = "1000", y = example.italy[1,4], ymin = example.italy[1,3], ymax = example.italy[1,5],
colour = "red", size = 1.5) + annotate("pointrange", x = "1200", y = example.italy[2,4], ymin = example.italy[2,3], ymax = example.italy[2,5],
colour = "red", size = 1.5) + annotate("pointrange", x = "1300", y = example.italy[3,4], ymin = example.italy[3,3], ymax = example.italy[3,5],
colour = "red", size = 1.5) + annotate("pointrange", x = "1400", y = example.italy[4,4], ymin = example.italy[4,3], ymax = example.italy[4,5],
colour = "red", size = 1.5) + annotate("pointrange", x = "1500", y = example.italy[5,4], ymin = example.italy[5,3], ymax = example.italy[5,5],
colour = "red", size = 1.5) + annotate("pointrange", x = "1600", y = example.italy[6,4], ymin = example.italy[6,3], ymax = example.italy[6,5],
colour = "red", size = 1.5) + annotate("pointrange", x = "1700", y = example.italy[7,4], ymin = example.italy[7,3], ymax = example.italy[7,5],
colour = "red", size = 1.5) + annotate("pointrange", x = "1750", y = example.italy[8,4], ymin = example.italy[8,3], ymax = example.italy[8,5],
colour = "red", size = 1.5) + annotate("pointrange", x = "1800", y = example.italy[9,4], ymin = example.italy[9,3], ymax = example.italy[9,5],
colour = "red", size = 1.5)
这将产生:
但如果数据包含15-20个组(国家)怎么办?然后,分割数据并分别绘制每个国家的 map 将是不切实际的.
关于如何一次绘制不同方面的点切线,有什么建议吗?
您可以建议其他类似于pointrange的功能(可能是errorbars?).基本上,对于数据中的每一行,我希望显示从"fit.0.025quant"到"fit.0.975quant"的范围,fit.mean可以删除.