[ggplot2主题] ggplot2之自定义主题篇

啦啦啦,我只是一个小搬运工。
  • 链接: https://github.com/meensrinivasan/tidytuesdaysubmissions/tree/master/Anime
  • 能画出什么?


    [ggplot2主题] ggplot2之自定义主题篇_第1张图片
library(tidyverse)
# devtools::install_github("EmilHvitfeldt/paletteer")
library(paletteer)
library(lubridate)
  • 导入数据
anime <- read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2019/2019-04-23/tidy_anime.csv")

> head(anime)
# A tibble: 6 x 28
  animeID name  title_english title_japanese title_synonyms type  source producers genre studio episodes status airing start_date end_date   duration rating score scored_by  rank popularity members favorites
                                                                                          
1       1 Cowb~ Cowboy Bebop  カウボーイビバップ~ []             TV    Origi~ Bandai V~ Acti~ Sunri~       26 Finis~ FALSE  1998-04-03 1999-04-02 24 min ~ R - 1~  8.81    405664    26         39  795733     43460
2       1 Cowb~ Cowboy Bebop  カウボーイビバップ~ []             TV    Origi~ Bandai V~ Adve~ Sunri~       26 Finis~ FALSE  1998-04-03 1999-04-02 24 min ~ R - 1~  8.81    405664    26         39  795733     43460
3       1 Cowb~ Cowboy Bebop  カウボーイビバップ~ []             TV    Origi~ Bandai V~ Come~ Sunri~       26 Finis~ FALSE  1998-04-03 1999-04-02 24 min ~ R - 1~  8.81    405664    26         39  795733     43460
4       1 Cowb~ Cowboy Bebop  カウボーイビバップ~ []             TV    Origi~ Bandai V~ Drama Sunri~       26 Finis~ FALSE  1998-04-03 1999-04-02 24 min ~ R - 1~  8.81    405664    26         39  795733     43460
5       1 Cowb~ Cowboy Bebop  カウボーイビバップ~ []             TV    Origi~ Bandai V~ Sci-~ Sunri~       26 Finis~ FALSE  1998-04-03 1999-04-02 24 min ~ R - 1~  8.81    405664    26         39  795733     43460
6       1 Cowb~ Cowboy Bebop  カウボーイビバップ~ []             TV    Origi~ Bandai V~ Space Sunri~       26 Finis~ FALSE  1998-04-03 1999-04-02 24 min ~ R - 1~  8.81    405664    26         39  795733     43460
  • 定义主题: 主题背景超棒
barchart_theme <-  theme(axis.text.y   = element_text(size=13, face="bold", colour = "black"),
                  axis.text.x   = element_text(size=13, face="bold", colour = "black"),
                  axis.title.x  = element_text(size=13, face="bold"),
                  axis.title.y  = element_text(size=13, face = "bold"),
                  panel.background = element_rect(fill = "white"),
                  axis.ticks.length = unit(.25, "cm"),
                  plot.title = element_text(lineheight=.8, face="bold", hjust = 0.5),
                  plot.caption = element_text(size = 10),
                  plot.subtitle = element_text(hjust = 0.5, size = 10),
                  strip.background = element_rect(fill = "white"),
                  strip.text = element_text(size = 18, hjust = 0, colour = "black", face ="bold"),
                  legend.position = "none",
                  legend.title = element_text(size = 13, face = "bold"),
                  legend.text = element_text(size = 13),
                  panel.border = element_rect(color = "black", fill = NA, size = 2),
                  panel.grid.major.x = element_blank(),
                  panel.grid.minor.x = element_blank(),
                  panel.grid.major.y = element_line(colour = "grey60", linetype = "dashed"))
  • 数据整理
anime2 <- anime %>%
  group_by(animeID) %>%
  select(name, title_english, type, source, genre, start_date, end_date, score, popularity, favorites) %>%
  slice(1)

> head(anime2)
# A tibble: 6 x 11
# Groups:   animeID [6]
  animeID name                        title_english         type  source  genre    start_date end_date   score popularity favorites
                                                                          
1       1 Cowboy Bebop                Cowboy Bebop          TV    Origin~ Action   1998-04-03 1999-04-02  8.81         39     43460
2       5 Cowboy Bebop: Tengoku no T~ Cowboy Bebop: The Mo~ Movie Origin~ Action   2001-09-01 NA          8.41        449       776
3       6 Trigun                      Trigun                TV    Manga   Action   1998-04-01 1998-09-03  8.3         146     10432
4       7 Witch Hunter Robin          Witch Hunter Robin    TV    Origin~ Action   2002-07-02 2002-12-02  7.33       1171       537
5       8 Bouken Ou Beet              Beet the Vandel Bust~ TV    Manga   Adventu~ 2004-09-30 2005-09-02  7.03       3704        14
6      16 Hachimitsu to Clover        Honey and Clover      TV    Manga   Comedy   2005-04-15 2005-09-02  8.12        536      3752
  • 可视化
anime3 <- anime2 %>%
  group_by(genre) %>%
  summarise(median_score = median(score)) %>%
  na.omit() %>%
  arrange(desc(median_score)) %>%
  head(14)
#   geom_errorbar(aes(ymin = pct_5, ymax = pct_97.5), col = "red")
ggplot(anime3 , aes(x = median_score, y = reorder(genre, median_score), color = genre)) +
  geom_segment(aes(yend = genre), xend = 0, color = "black", size = 1) +
  geom_point(size = 8) +
  scale_color_paletteer_d(package = "LaCroixColoR", palette = "paired") +
  labs(x = "Median Score",
       y = "",
       title = "Highest scored Anime genres",
       subtitle = "There was a genre called 'Dementia' in the dataset. Why?") +
  barchart_theme
[ggplot2主题] ggplot2之自定义主题篇_第2张图片
anime4 <- anime2 %>%
  select(name, genre, start_date, score, popularity) %>%
  mutate(year = year(start_date))%>%
  group_by(year, genre) %>%
  summarise(n = n()) %>%
  na.omit() %>%
  filter(genre %in% c("Kids", "Comedy", "Romance", "Action", "Adventure", "Dementia"))
ggplot(anime4, aes(year, n, color = genre)) +
  geom_point(size = 2.5) +
  scale_color_paletteer_d(name = "Genre", package = "LaCroixColoR", palette = "PassionFruit")+
  labs(x = "Year",
       y = "Number of shows",
       title = "Number of shows by genre.", 
       subtitle = "Hmmm.. So they have few shows in the 'Dementia' genre. WHY?") +
  barchart_theme +
  theme(legend.position = "bottom")
[ggplot2主题] ggplot2之自定义主题篇_第3张图片

你可能感兴趣的:([ggplot2主题] ggplot2之自定义主题篇)