每个组都有一个单独的geog_vline的ggplot

时间:2019-12-22 11:27:55

标签: r ggplot2 geom-vline

我大约有160个国家(产出$ ccname),log GDP(产出$ lnrgdpch),产出$ endyear作为年份变量,以及二元变量,用于说明领导者去世(1 =死亡)时的产出$ natural2。我正在尝试为领导者去世的每个国家以2-3个数字创建情节(无论构面如何)。这部分在下面的行中已经成功。

我正努力根据该国领导人去世的年份,在每幅土地上增加一条垂直线。为此,我创建了一个名为output_death的新数据集,该数据集仅在output $ natural2 == 1时才包含数据。

for (var in unique(output$ccname[output$natural2==1])) {
  dev.new()
  plot(ggplot(output[output$ccname==var,], aes(endyear,lnrgdpch))+ 
         geom_point() + geom_line() + 
         geom_vline(data=output_death,aes(group=ccname,xintercept=endyear))+
         labs(y="Ln real GDP ", x = "Year") +
         ggtitle(var))
}

目前,对于每个国家/地区的每位领导人死亡,我都会得到一条直线,因此我的感觉是我需要以某种方式对geom_vline参数进行分组。任何帮助表示赞赏。另外,由于我在这里,如果我能在一个图形中包含多个至少4x4的绘图,我也将非常高兴。 我现在得到的示例图:

enter image description here

最小的可复制示例:

> output <- data.frame(ccname=c("Angola","Angola","Angola","Angola",
+                            "Angola","Angola","Angola","Angola",
+                            "Angola","Angola","US","US","US","US",
+                            "US","US","US","US","US","US"), 
+                  endyear=c(1940:1949,1940:1949), 
+                  leader = c("David", "NA", "NA", "NA","Henry","NA",
+                             "Tom","NA","Chris","NA","NA","NA","NA",
+                             "Alia","NA","NA","NA","NA","NA","NA"), 
+                  natural2 = c(0,NA,NA,NA,0,NA,1,NA,0,NA,NA,NA,NA,1,NA,NA,NA,NA,NA,NA),lnrgdpch=c(1:20),
+    id1=c(0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0),
+    id2=c(0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0),
+id1.PRE=c(0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0),
+id2.PRE=c(0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0), id1.POST=c(0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0), id2.POST=c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0))

> output
          ccname endyear leader natural2 lnrgdpch id1 id2 id1.PRE
Angola.1   Angola 1940  David             0               1   0   0       0
Angola.2   Angola 1941     NA            NA               2   0   0       1
Angola.3   Angola 1942     NA            NA               3   0   0       1
Angola.4   Angola 1943     NA            NA               4   0   0       1
Angola.5   Angola 1944  Henry             0               5   0   0       1
Angola.6   Angola 1945     NA            NA               6   0   0       1
Angola.7   Angola 1946    Tom             1               7   1   0       0
Angola.8   Angola 1947     NA            NA               8   0   0       0
Angola.9   Angola 1948  Chris             0               9   0   0       0
Angola.10  Angola 1949     NA            NA              10   0   0       0
US.1           US 1940     NA            NA              11   0   0       0
US.2           US 1941     NA            NA              12   0   0       0
US.3           US 1942     NA            NA              13   0   0       0
US.4           US 1943   Alia             1              14   0   1       0
US.5           US 1944     NA            NA              15   0   0       0
US.6           US 1945     NA            NA              16   0   0       0
US.7           US 1946     NA            NA              17   0   0       0
US.8           US 1947     NA            NA              18   0   0       0
US.9           US 1948     NA            NA              19   0   0       0
US.10          US 1949     NA            NA              20   0   0       0
          id1.POST id2.PRE id2.POST
Angola.1         0       0        0
Angola.2         0       0        1
Angola.3         0       0        1
Angola.4         0       0        1
Angola.5         0       0        1
Angola.6         0       0        1
Angola.7         0       0        0
Angola.8         1       0        0
Angola.9         1       0        0
Angola.10        1       0        0
US.1             0       1        0
US.2             1       1        0
US.3             1       1        0
US.4             1       0        0
US.5             1       0        1
US.6             1       0        1
US.7             0       0        1
US.8             0       0        1
US.9             0       0        1
US.10            0       0        0

>output_death<-subset(output, natural2==1)

1 个答案:

答案 0 :(得分:0)

首先,仅提供更简洁的示例数据:

output <- data.frame(ccname=c("Angola","Angola","Angola","Angola",
                                                         "Angola","Angola","Angola","Angola",
                                                         "Angola","Angola","US","US","US","US",
                                                         "US","US","US","US","US","US"), 
                                       endyear=c(1940:1949,1940:1949), 
                                       leader = c("David", "NA", "NA", "NA","Henry","NA",
                                                                              "Tom","NA","Chris","NA","NA","NA","NA",
                                                                              "Alia","NA","NA","NA","NA","NA","NA"), 
                                       natural2 = c(0,NA,NA,NA,0,NA,1,NA,0,NA,NA,NA,NA,1,NA,NA,NA,NA,NA,NA),lnrgdpch=c(1:20),
                         id1=c(0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0),
                      id2=c(0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0),
                    id1.PRE=c(0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0),
                  id2.PRE=c(0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0), id1.POST=c(0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0), id2.POST=c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0))

output_death <- output[output$natural2==1,]

将其分解成小步骤可能更容易思考。首先,让我们将国家/地区存储在自己的向量中:

countries <- levels(output$ccname)

接下来,在创建for循环之前,您需要创建一个空对象,该对象将存储循环的每次迭代的结果:

result <- vector(mode = "list", length = length(countries))

现在,我们应用循环并将每个图存储在result输出的元素中:

for (var in countries) {
result[[var]] <- ggplot(output[output$ccname==var,], aes(endyear,lnrgdpch))+ 
         geom_point() + geom_line() + 
         geom_vline(data=output_death,aes(group=ccname,xintercept=endyear))+
         labs(y="Ln real GDP ", x = "Year") +
         ggtitle(var)
}

您还可以将名称应用于列表元素,以使其更易于处理:

names(result) <- countries