使用 for 循环拆分面板并绘制一些 ID
Use a for loop to split a a panel and plot some IDs
多年来,我有一个由许多国家组成的非常大的小组。
例如,假设我有
structure(list(country = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L,
10L, 10L, 10L, 10L), .Label = c("Bangladesh", "Barbados", "Benin",
"Burundi", "Cameroon", "Chile", "Cyprus", "Ecuador", "Equatorial Guinea",
"Gabon", "Ghana", "Guatemala", "Guinea", "Guyana", "Haiti", "India",
"Jamaica", "Jordan", "Lebanon", "Liberia", "Madagascar", "Mali",
"Mexico", "Morocco", "Mozambique", "Nepal", "Nicaragua", "Niger",
"Oman", "Pakistan", "Panama", "Papua New Guinea", "Peru", "Rwanda",
"Senegal", "Seychelles", "Sierra Leone", "Singapore", "Sri Lanka",
"Sudan", "Togo", "Trinidad and Tobago", "Tunisia", "Turkey",
"Uganda", "Zambia", "Zimbabwe"), class = c("pseries", "factor"
)), date = structure(c(12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L,
20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 40L,
41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 53L,
54L, 1L, 2L, 3L, 4L, 5L, 6L), .Label = c("1965", "1966", "1967",
"1968", "1969", "1970", "1971", "1972", "1973", "1974", "1975",
"1976", "1977", "1978", "1979", "1980", "1981", "1982", "1983",
"1984", "1985", "1986", "1987", "1988", "1989", "1990", "1991",
"1992", "1993", "1994", "1995", "1996", "1997", "1998", "1999",
"2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007",
"2008", "2009", "2010", "2011", "2012", "2013", "2014", "2015",
"2016", "2017", "2018"), class = c("pseries", "factor")), dist = c(-2.44153863355447,
-0.565497304455015, 1.4222058591902, 1.18583114700364, 0.936095936859405,
-0.40582369098349, 2.08296049774769, -1.59377116576285, 0.316969269882462,
-0.562808458278515, -0.438155688763343, 0.824980940313303, -0.60293744113302,
0.397182279339227, 1.34777053122572, 0.00539983501313633, 0.0528824118570846,
-0.838507946421168, 0.220969370384196, -1.06945294612116, 0.157324834050942,
0.244080457191975, 0.215931770884978, 0.226176571912671, 0.197663973781873,
0.183399695771442, 0.334605869075331, 0.0683789169881243, -0.0432432418415798,
-0.191834311017179, -0.368292801016647, -0.572222256415503, -0.838601784768107,
-1.15213567684887, -1.49844386483361, 2.39265920977342, 1.90573467650352,
1.3362714819111, 0.158391029649629, 1.12699318386612, 2.38996532864524
), dummy = c(0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1,
1, 0, 1, 1)), row.names = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L,
10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 400L,
401L, 402L, 403L, 404L, 405L, 406L, 407L, 408L, 409L, 410L, 411L,
412L, 413L, 414L, 415L, 416L, 417L, 418L, 419L, 420L), class = "data.frame")
我想提取一些国家并为每个国家绘制变量“diff”。
换句话说,我想为面板中的国家/地区样本创建不同的多个图。
我猜我可以从
开始
countries=c("Bangladesh", "Equatorial Guinea")
for (i in 1:length(countries)) {
assign(countries[i],data_split[[i]])
}
然后情节应该类似,但我迷失了第一部分。
您可以使用 split
拆分数据,然后遍历该结果的组成部分。下面给出了一个默认值 plot
,但您可以根据需要进行调整。
data_split <- split(data, data$country)
for (country in names(data_split)) {
country_data <- data_split[[country]]
if (nrow(country_data) > 0) {
plot(as.numeric(country_data$date), country_data$dist, main = country)
}
}
也就是说,就像评论者指出的那样,facet_grid
或 facet_wrap
在我看来是一种更优雅的方法来完成相同的操作。在创建绘图之前,您可以将数据子集 select 到您想要的国家/地区。
像这样:
countries <- c("Bangladesh", "Equatorial Guinea")
ggplot(data[data$country %in% countries, ], aes(as.numeric(as.character(date)), dist)) +
geom_line() +
facet_wrap(~ country)
多年来,我有一个由许多国家组成的非常大的小组。 例如,假设我有
structure(list(country = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L,
10L, 10L, 10L, 10L), .Label = c("Bangladesh", "Barbados", "Benin",
"Burundi", "Cameroon", "Chile", "Cyprus", "Ecuador", "Equatorial Guinea",
"Gabon", "Ghana", "Guatemala", "Guinea", "Guyana", "Haiti", "India",
"Jamaica", "Jordan", "Lebanon", "Liberia", "Madagascar", "Mali",
"Mexico", "Morocco", "Mozambique", "Nepal", "Nicaragua", "Niger",
"Oman", "Pakistan", "Panama", "Papua New Guinea", "Peru", "Rwanda",
"Senegal", "Seychelles", "Sierra Leone", "Singapore", "Sri Lanka",
"Sudan", "Togo", "Trinidad and Tobago", "Tunisia", "Turkey",
"Uganda", "Zambia", "Zimbabwe"), class = c("pseries", "factor"
)), date = structure(c(12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L,
20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 40L,
41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 53L,
54L, 1L, 2L, 3L, 4L, 5L, 6L), .Label = c("1965", "1966", "1967",
"1968", "1969", "1970", "1971", "1972", "1973", "1974", "1975",
"1976", "1977", "1978", "1979", "1980", "1981", "1982", "1983",
"1984", "1985", "1986", "1987", "1988", "1989", "1990", "1991",
"1992", "1993", "1994", "1995", "1996", "1997", "1998", "1999",
"2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007",
"2008", "2009", "2010", "2011", "2012", "2013", "2014", "2015",
"2016", "2017", "2018"), class = c("pseries", "factor")), dist = c(-2.44153863355447,
-0.565497304455015, 1.4222058591902, 1.18583114700364, 0.936095936859405,
-0.40582369098349, 2.08296049774769, -1.59377116576285, 0.316969269882462,
-0.562808458278515, -0.438155688763343, 0.824980940313303, -0.60293744113302,
0.397182279339227, 1.34777053122572, 0.00539983501313633, 0.0528824118570846,
-0.838507946421168, 0.220969370384196, -1.06945294612116, 0.157324834050942,
0.244080457191975, 0.215931770884978, 0.226176571912671, 0.197663973781873,
0.183399695771442, 0.334605869075331, 0.0683789169881243, -0.0432432418415798,
-0.191834311017179, -0.368292801016647, -0.572222256415503, -0.838601784768107,
-1.15213567684887, -1.49844386483361, 2.39265920977342, 1.90573467650352,
1.3362714819111, 0.158391029649629, 1.12699318386612, 2.38996532864524
), dummy = c(0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1,
1, 0, 1, 1)), row.names = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L,
10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 400L,
401L, 402L, 403L, 404L, 405L, 406L, 407L, 408L, 409L, 410L, 411L,
412L, 413L, 414L, 415L, 416L, 417L, 418L, 419L, 420L), class = "data.frame")
我想提取一些国家并为每个国家绘制变量“diff”。 换句话说,我想为面板中的国家/地区样本创建不同的多个图。
我猜我可以从
开始countries=c("Bangladesh", "Equatorial Guinea")
for (i in 1:length(countries)) {
assign(countries[i],data_split[[i]])
}
然后情节应该类似,但我迷失了第一部分。
您可以使用 split
拆分数据,然后遍历该结果的组成部分。下面给出了一个默认值 plot
,但您可以根据需要进行调整。
data_split <- split(data, data$country)
for (country in names(data_split)) {
country_data <- data_split[[country]]
if (nrow(country_data) > 0) {
plot(as.numeric(country_data$date), country_data$dist, main = country)
}
}
也就是说,就像评论者指出的那样,facet_grid
或 facet_wrap
在我看来是一种更优雅的方法来完成相同的操作。在创建绘图之前,您可以将数据子集 select 到您想要的国家/地区。
像这样:
countries <- c("Bangladesh", "Equatorial Guinea")
ggplot(data[data$country %in% countries, ], aes(as.numeric(as.character(date)), dist)) +
geom_line() +
facet_wrap(~ country)