假设我有以下模拟数据:

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)

这将产生:

enter image description here

但如果数据包含15-20个组(国家)怎么办?然后,分割数据并分别绘制每个国家的 map 将是不切实际的.

关于如何一次绘制不同方面的点切线,有什么建议吗?

您可以建议其他类似于pointrange的功能(可能是errorbars?).基本上,对于数据中的每一行,我希望显示从"fit.0.025quant""fit.0.975quant"的范围,fit.mean可以删除.

推荐答案

你需要稍微读懂一下ggploy.与其使用annotate,我建议使用geom_pointrange,然后您可以简单地将列作为美学传递到对geom的单个(!)调用中.这样一来,使用Facet_WRAP就变得非常容易.

ggplot(example, aes(x = date, y = fit.mean)) +
  geom_pointrange(aes(ymin = fit.0.025quant, ymax = fit.0.975quant), color = "red") +
  facet_wrap(~country)

创建于2023-04-03年第reprex v2.0.2

R相关问答推荐

用dDeliverr用第二个表更新一个表

当y大于阈值和值范围时,在时间序列中突出显示区域

如何计算新变量中的通货inflating 率?

如何创建构成多个独立列条目列表的收件箱框列?

变量计算按R中的行更改

使用R中的gt对R中的html rmarkdown文件进行条件格式设置表的单元格

使用R中相同值创建分组观测指标

如何在xyplot中 for each 面板打印R^2

获取列中值更改的行号

整数成随机顺序与约束R?

汇总数据表中两个特定列条目的值

派生程序包| ;无法检索';return()';的正文

KM估计的差异:SvyKm与带权重的调查

R:如果为NA,则根据条件,使用列名模式将缺少的值替换为另一列中的值

停止ggplot将多行减少到一行

'使用`purrr::pwalk`从嵌套的嵌套框架中的列表列保存ggplots时出现未使用的参数错误

远离理论值的伽马密度曲线下面积的近似

为什么我对圆周率图的蒙特卡罗估计是空的?

在点图上绘制置信度或预测区间ggplot2

在鼠标悬停时使用Plotly更改geom_point大小