我可以用R中的提示功能制作世界地图吗?
Can I make a worldmap with the prompt function in R?
我是编程新手,我偶然发现了一些我无法解决的问题,我希望你们能解决。
我正在处理 FIFA 19 数据集。请参阅下面的部分数据集:
Name Nationality Overall Club Balance
1 L. Messi Argentina 94 FC Barcelona 95
2 Cristiano Ronaldo Portugal 94 Juventus 70
3 Neymar Jr Brazil 92 Paris Saint-Germain 84
4 De Gea Spain 91 Manchester United 43
5 K. De Bruyne Belgium 91 Manchester City 77
6 E. Hazard Belgium 91 Chelsea 94
我想要的是一张世界地图,您可以在其中插入具有 readline(prompt) 功能的俱乐部,然后为您提供地图,以及该俱乐部的所有球员,然后显示这些球员在世界上的哪个位置来自(所以每个俱乐部每个国籍的计数,如果有意义的话)。
我在下面的网站上看到了如何制作这个数据集的世界地图。
制作玩家总数世界地图的代码(因此对于整个数据集)如下。它给了我正确的输出。
overall_data <- fifa %>%
group_by(Nationality) %>%
summarise(Count = n(),
Avg_Overall = mean(Overall),
Avg_Potential = mean(Potential),
Avg_Pot_Diff = mean(Potential-Overall))
worldmap = map_data("world")
merged_data <- merge(x = worldmap, y = overall_data, by.x = "region", by.y = "Nationality", all.x = TRUE) %>% arrange(order)
ggplot(data = merged_data, aes(x = long, y = lat, group = group)) +
geom_polygon(aes(fill = Count)) +
labs(fill='Total Player Counts')
即这张世界地图:Worldmap Total Player Count
但这不允许我为每个俱乐部制作不同的地图。所以我做了一个单独的数据文件,计算每个俱乐部,每个国家有多少人。我使用来自以下来源的 crosstab() 函数制作了一个交叉表:("http://pcwww.liv.ac.uk/~william/R/crosstab.r")。这是我使用的代码:
nat_per_club <- crosstab(fifa, row.vars = "Nationality", col.vars = "Club", type ="f")
nat_per_club_crosstab <- nat_per_club$crosstab
nat_per_club_dataframe <- data.frame(nat_per_club_crosstab)
nat_per_club_dataframe <- nat_per_club_dataframe[!(nat_per_club_dataframe$Freq==0), ]
这给了我以下 table:
Nationality Club Freq
8 Armenia SSV Jahn Regensburg 1
60 Germany SSV Jahn Regensburg 19
87 Kosovo SSV Jahn Regensburg 1
94 Lithuania SSV Jahn Regensburg 1
104 Morocco SSV Jahn Regensburg 1
121 Poland SSV Jahn Regensburg 1
这正是我想要的,因为它显示了每个俱乐部和每个国籍的频率。现在的问题是,如何使用 readline(prompt) 函数在世界地图中实现它。因此,如果我插入例如 SSV Jahn Regensburg,它会向我显示他们的球员来自世界何处的世界地图?
这是我尝试过的方法,但不幸的是它不起作用。
worldmap2 = map_data("world")
merged_data2 <- merge(x = worldmap2, y = nat_per_club_dataframe, by.x = "region", by.y = "Nationality", all.x = TRUE) %>% arrange(order)
ggplot(data = merged_data2, aes(x = long, y = lat, group = group)) +
geom_polygon(aes(fill = Freq)) +
labs(fill='Total Player Count')
players_from_where <- function() {
club_name <- as.character(readline(prompt="Club: "))
for(i in 1:nrow(nat_per_club_dataframe)){
if(nat_per_club_dataframe[i, "Club"] == club_name){
ggplot(data = merged_data2, aes(x = long, y = lat, group = group)) +
geom_polygon(aes(fill = Freq)) +
labs(fill='Total Player Counts')
}
}
}
希望有人能帮忙!
不需要 for
循环。您可以简单地使用 dplyr::filter
或 subset
过滤您的聚合数据集作为输入的俱乐部名称,将其合并到地图数据并绘图。
library(tidyverse)
players_from_where <- function(club_name) {
x <- filter(nat_per_club_dataframe, Club %in% club_name)
worldmap <- map_data("world")
merged_data2 <- merge(x = worldmap, y = x, by.x = "region", by.y = "Nationality", all.x = TRUE) %>%
arrange(order)
ggplot(data = merged_data2, aes(x = long, y = lat, group = group)) +
geom_polygon(aes(fill = n)) +
labs(fill = "Total Player Counts")
}
# club_name <- as.character(readline(prompt = "Club: "))
club_name <- "FC Barcelona"
players_from_where(club_name)
数据
nat_per_club_dataframe <- structure(list(Nationality = c(
"Argentina", "Belgium", "Brazil",
"Chile", "Croatia", "France", "Germany", "Netherlands", "Portugal",
"Senegal", "Spain", "Uruguay", "Austria", "Brazil", "Colombia",
"France", "Germany", "Netherlands", "Poland", "Portugal", "Spain"
), Club = c(
"FC Barcelona", "FC Barcelona", "FC Barcelona", "FC Barcelona",
"FC Barcelona", "FC Barcelona", "FC Barcelona", "FC Barcelona",
"FC Barcelona", "FC Barcelona", "FC Barcelona", "FC Barcelona",
"FC Bayern München", "FC Bayern München", "FC Bayern München",
"FC Bayern München", "FC Bayern München", "FC Bayern München",
"FC Bayern München", "FC Bayern München", "FC Bayern München"
), n = c(
1L, 1L, 4L, 1L, 1L, 3L, 1L, 1L, 1L, 1L, 17L, 1L, 1L,
1L, 1L, 3L, 18L, 1L, 1L, 1L, 2L
)), class = "data.frame", row.names = c(
NA,
-21L
))
我是编程新手,我偶然发现了一些我无法解决的问题,我希望你们能解决。
我正在处理 FIFA 19 数据集。请参阅下面的部分数据集:
Name Nationality Overall Club Balance
1 L. Messi Argentina 94 FC Barcelona 95
2 Cristiano Ronaldo Portugal 94 Juventus 70
3 Neymar Jr Brazil 92 Paris Saint-Germain 84
4 De Gea Spain 91 Manchester United 43
5 K. De Bruyne Belgium 91 Manchester City 77
6 E. Hazard Belgium 91 Chelsea 94
我想要的是一张世界地图,您可以在其中插入具有 readline(prompt) 功能的俱乐部,然后为您提供地图,以及该俱乐部的所有球员,然后显示这些球员在世界上的哪个位置来自(所以每个俱乐部每个国籍的计数,如果有意义的话)。
我在下面的网站上看到了如何制作这个数据集的世界地图。
制作玩家总数世界地图的代码(因此对于整个数据集)如下。它给了我正确的输出。
overall_data <- fifa %>%
group_by(Nationality) %>%
summarise(Count = n(),
Avg_Overall = mean(Overall),
Avg_Potential = mean(Potential),
Avg_Pot_Diff = mean(Potential-Overall))
worldmap = map_data("world")
merged_data <- merge(x = worldmap, y = overall_data, by.x = "region", by.y = "Nationality", all.x = TRUE) %>% arrange(order)
ggplot(data = merged_data, aes(x = long, y = lat, group = group)) +
geom_polygon(aes(fill = Count)) +
labs(fill='Total Player Counts')
即这张世界地图:Worldmap Total Player Count
但这不允许我为每个俱乐部制作不同的地图。所以我做了一个单独的数据文件,计算每个俱乐部,每个国家有多少人。我使用来自以下来源的 crosstab() 函数制作了一个交叉表:("http://pcwww.liv.ac.uk/~william/R/crosstab.r")。这是我使用的代码:
nat_per_club <- crosstab(fifa, row.vars = "Nationality", col.vars = "Club", type ="f")
nat_per_club_crosstab <- nat_per_club$crosstab
nat_per_club_dataframe <- data.frame(nat_per_club_crosstab)
nat_per_club_dataframe <- nat_per_club_dataframe[!(nat_per_club_dataframe$Freq==0), ]
这给了我以下 table:
Nationality Club Freq
8 Armenia SSV Jahn Regensburg 1
60 Germany SSV Jahn Regensburg 19
87 Kosovo SSV Jahn Regensburg 1
94 Lithuania SSV Jahn Regensburg 1
104 Morocco SSV Jahn Regensburg 1
121 Poland SSV Jahn Regensburg 1
这正是我想要的,因为它显示了每个俱乐部和每个国籍的频率。现在的问题是,如何使用 readline(prompt) 函数在世界地图中实现它。因此,如果我插入例如 SSV Jahn Regensburg,它会向我显示他们的球员来自世界何处的世界地图?
这是我尝试过的方法,但不幸的是它不起作用。
worldmap2 = map_data("world")
merged_data2 <- merge(x = worldmap2, y = nat_per_club_dataframe, by.x = "region", by.y = "Nationality", all.x = TRUE) %>% arrange(order)
ggplot(data = merged_data2, aes(x = long, y = lat, group = group)) +
geom_polygon(aes(fill = Freq)) +
labs(fill='Total Player Count')
players_from_where <- function() {
club_name <- as.character(readline(prompt="Club: "))
for(i in 1:nrow(nat_per_club_dataframe)){
if(nat_per_club_dataframe[i, "Club"] == club_name){
ggplot(data = merged_data2, aes(x = long, y = lat, group = group)) +
geom_polygon(aes(fill = Freq)) +
labs(fill='Total Player Counts')
}
}
}
希望有人能帮忙!
不需要 for
循环。您可以简单地使用 dplyr::filter
或 subset
过滤您的聚合数据集作为输入的俱乐部名称,将其合并到地图数据并绘图。
library(tidyverse)
players_from_where <- function(club_name) {
x <- filter(nat_per_club_dataframe, Club %in% club_name)
worldmap <- map_data("world")
merged_data2 <- merge(x = worldmap, y = x, by.x = "region", by.y = "Nationality", all.x = TRUE) %>%
arrange(order)
ggplot(data = merged_data2, aes(x = long, y = lat, group = group)) +
geom_polygon(aes(fill = n)) +
labs(fill = "Total Player Counts")
}
# club_name <- as.character(readline(prompt = "Club: "))
club_name <- "FC Barcelona"
players_from_where(club_name)
数据
nat_per_club_dataframe <- structure(list(Nationality = c(
"Argentina", "Belgium", "Brazil",
"Chile", "Croatia", "France", "Germany", "Netherlands", "Portugal",
"Senegal", "Spain", "Uruguay", "Austria", "Brazil", "Colombia",
"France", "Germany", "Netherlands", "Poland", "Portugal", "Spain"
), Club = c(
"FC Barcelona", "FC Barcelona", "FC Barcelona", "FC Barcelona",
"FC Barcelona", "FC Barcelona", "FC Barcelona", "FC Barcelona",
"FC Barcelona", "FC Barcelona", "FC Barcelona", "FC Barcelona",
"FC Bayern München", "FC Bayern München", "FC Bayern München",
"FC Bayern München", "FC Bayern München", "FC Bayern München",
"FC Bayern München", "FC Bayern München", "FC Bayern München"
), n = c(
1L, 1L, 4L, 1L, 1L, 3L, 1L, 1L, 1L, 1L, 17L, 1L, 1L,
1L, 1L, 3L, 18L, 1L, 1L, 1L, 2L
)), class = "data.frame", row.names = c(
NA,
-21L
))