R语言 这个图的名字是什么?

mwg9r5ms  于 2023-03-27  发布在  其他
关注(0)|答案(2)|浏览(134)

我想知道这个图的名字。这将是一个很好的额外的R包或ggplot2扩展的名称来创建一个。我发现最接近的一个被称为冲积。

f8rj6qna

f8rj6qna1#

当你看到这个图时,你会意识到它只是下表:

df <- structure(list(`2008` = c("Labrador Retriever", "Yorkshire Terrier", 
"German Shepherd", "Golden Retriever", "Beagle", "Boxer", "Dachshund", 
"Bulldog"), `2015` = c("Labrador Retriever", "German Shepherd", 
"Golden Retriever", "Bulldog", "Beagle", "French Bulldog", "Yorkshire Terrier", 
"Poodle"), `2022` = c("French Bulldog", "Labrador Retriever", 
"Golden Retriever", "German Shepherd", "Poodle", "Bulldog", "Rottweiler", 
"Beagle")), class = "data.frame", row.names = c(NA, -8L))

df
#>                 2008               2015               2022
#> 1 Labrador Retriever Labrador Retriever     French Bulldog
#> 2  Yorkshire Terrier    German Shepherd Labrador Retriever
#> 3    German Shepherd   Golden Retriever   Golden Retriever
#> 4   Golden Retriever            Bulldog    German Shepherd
#> 5             Beagle             Beagle             Poodle
#> 6              Boxer     French Bulldog            Bulldog
#> 7          Dachshund  Yorkshire Terrier         Rottweiler
#> 8            Bulldog             Poodle             Beagle

我们可以使用ggplot来获取这个表,并像这样重新创建图:

library(tidyverse)

df %>%
  mutate(Rank = 9 - row_number()) %>%
  pivot_longer(-Rank, names_to = "Year", values_to = "Breed") %>%
  mutate(Year = as.numeric(Year)) %>%
  group_by(across(everything())) %>%
  summarize(Time = Year + c(-3, 0, 3), Main = c(FALSE, TRUE, FALSE)) %>%
  arrange(Time) %>%
  mutate(Breed = factor(Breed, c("Labrador Retriever", "Yorkshire Terrier",
                                 "German Shepherd", "Golden Retriever",
                                 "Dachshund", "Bulldog", "Poodle", "Boxer",
                                 "Rottweiler", "Beagle", "French Bulldog"))) %>%
  ggplot(aes(Time, Rank)) +
  geom_path(aes(color = Breed), linewidth = 15) +
  geom_text(aes(label = ifelse(Main, as.character(Breed), "")), 
            color = "white", size = 6, 
            data = . %>% filter(Breed %in% c("German Shepherd", "Dachshund",
                                             "French Bulldog"))) +
  geom_text(aes(label = ifelse(Main, as.character(Breed), "")), 
            color = "black", size = 6, 
            data = . %>% filter(!Breed %in% c("German Shepherd", "Dachshund",
                                              "French Bulldog"))) +
  scale_color_manual(values = c("#caaa2f", "#7ebcf9", "#a5211c", "#f2d44b",
                                "#70ac0c", "#fea67a", "#ba98f0", "#2cc9bc",
                                "#937d27", "#a4db48", "#ed5f1a"), guide = "none") +
  scale_x_continuous(breaks = c(2008, 2015, 2022), position = "top") +
  theme_void() +
  theme(plot.margin = margin(20, 20, 20, 20),
        plot.background = element_rect(color = NA, fill = "gray95"),
        axis.text.x.top = element_text(size = 20, face = 2,
                                       margin = margin(10, 10, 10, 10)))

7nbnzgx9

7nbnzgx92#

这是一个排名流程图(也称为排名或凹凸图)。
您可以使用基本的ggplot2(使用geom_line和geom_point)创建它。
例如,如果你有一个狗的数据集,每年的狗的排名,你可以把它画成这样:

ggplot(dogs, aes(x = year, y = ranking, group = breed)) +
     geom_line(geom_line(aes(color = breed, alpha = 1), size = 2) +
     geom_point(aes(color = breed, alpha = 1), size = 4) +
     scale_y_reverse(breaks = 1:nrow(dogs))

请参阅此博客文章了解更多信息和其他样式:R-Bloggers

相关问题