0

I have around 160 countries (output$ccname), log GDP (output$lnrgdpch), output$endyear as year variable and a binary variable for when a leader died (1= died) output$natural2. I am trying to create plots in 2-3 figures (doesn't matter how they are facetted) for each country in which a leader died. This part has been successful with the line below.

I am struggling with adding a vertical line to each plot based on the year in which a leader in that country died. For this I created a new data set called output_death which includes only data when 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))
}

Currently, I get a line for each leader death in each country plot, so my feeling is that I need to group the geom_vline argument somehow. Any help is appreciated. Also, since I am here, I would also be very happy if I could have many at least 4x4 plots in one figure. An example plot I get now:

enter image description here

Minimal reproducible example:

> 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)
4

1 回答 1

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
于 2019-12-23T03:55:40.560 回答