ggplot2绘图R_学习专题生信分析技巧

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

2019-04-28  本文已影响141人  热衷组培的二货潜
啦啦啦,我只是一个小搬运工。
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
    <dbl> <chr> <chr>         <chr>          <chr>          <chr> <chr>  <chr>     <chr> <chr>     <dbl> <chr>  <lgl>  <date>     <date>     <chr>    <chr>  <dbl>     <dbl> <dbl>      <dbl>   <dbl>     <dbl>
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
    <dbl> <chr>                       <chr>                 <chr> <chr>   <chr>    <date>     <date>     <dbl>      <dbl>     <dbl>
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
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")
上一篇下一篇

猜你喜欢

热点阅读