R语言ggplot2每周一图活动:第三周~柱形图和散点图

2022-05-23 15:50:10 浏览数 (1)

代码数据来源

https://github.com/z3tt/TidyTuesday/blob/main/R/2019_17_animes.Rmd

加载需要用到的R包

代码语言:javascript复制
library(tidyverse)
library(ggrepel)
library(patchwork)
library(ghibli)

读取数据

代码语言:javascript复制
df_ghibli <- readr::read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2019/2019-04-23/tidy_anime.csv") %>% 
  filter(studio == "Studio Ghibli", type == "Movie") %>% 
  dplyr::select(animeID, title_english, title_japanese, genre, score, scored_by, members)

柱形图

代码语言:javascript复制
img_a <- png::readPNG(here::here("img", "totoro.png")) 
a <- grid::rasterGrob(img_a, interpolate = T) 
ghibli_genres <- df_ghibli %>% 
  group_by(genre) %>% 
  count() %>% 
  filter(n > 5) %>% 
  ungroup() %>% 
  mutate(genre = fct_reorder(genre, n)) %>% 
  ggplot(aes(genre, n))  
    geom_col(aes(fill = genre))  
    coord_flip()  
    scale_y_continuous(limits = c(0, 20), expand = c(0.01, 0))  
    scale_fill_ghibli_d("MononokeLight")  
    guides(fill = F)   
    labs(x = "Most common genres", y = "Count")  
    annotation_custom(a, xmin = 0.4, xmax = 6.75, ymin = 12, ymax = 23)  
    theme(axis.text.x = element_text(family = "Roboto Mono"))
ghibli_genres

image.png

散点图

代码语言:javascript复制
img_b <- png::readPNG(here::here("img", "ghibli.png"))
b <- grid::rasterGrob(img_b, interpolate = T) 
set.seed(1)
df_ghibli_unique <-df_ghibli %>% 
  group_by(animeID) %>% 
  summarize_all(first) %>% 
  mutate(title = glue::glue("{title_english}"))
ghibli_scores <- df_ghibli_unique %>% 
  ggplot(aes(score, scored_by))  
    geom_point(aes(size = members), color = "#F4C59D", alpha = 0.6)  
    geom_text_repel(data = filter(df_ghibli_unique, scored_by > 120000), aes(label = title), size = 1.75, family = "Poppins", 
                    color = "#F4C59D", segment.size = 0.3, xlim = c(9.25, 10), box.padding = 0.5, force = 5)  
    scale_x_continuous(limits = c(5, 10))  
    scale_y_continuous(labels = scales::comma, limits = c(0, 600000))   
    scale_size_continuous(name = "Times listed by MAL users:",
                          breaks = c(1000, 10000, 100000, 250000, 500000), 
                          labels = c("  1,000", " 10,000", "100,000", "250,000", "500,000"))  
    guides(size = guide_legend(override.aes = list(alpha = 1)))  
    labs(x = "Average MAL user score", y = "Number of ratings",
         caption = "AAA")  
    annotation_custom(b, xmin = 5, xmax = 8, ymin = 400000, ymax = 600000)  
    theme(axis.text = element_text(family = "Roboto Mono"),
          legend.position = c(0.32, 0.4),
          legend.background = element_rect(fill = "transparent"),
          legend.title = element_text(size = 9),
          legend.text = element_text(family = "Roboto Mono", size = 8))
df_ghibli_unique

image.png

拼图

代码语言:javascript复制
ghibli_genres   ghibli_scores   plot_layout(width = c(1, 1))

image.png

0 人点赞