首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >R语言ggplot2每周一图活动:第三周~柱形图和散点图

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

作者头像
用户7010445
发布2022-05-23 15:50:10
发布2022-05-23 15:50:10
5170
举报

代码数据来源

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

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-05-02,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 小明的数据分析笔记本 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 代码数据来源
  • 加载需要用到的R包
  • 读取数据
  • 柱形图
  • 散点图
  • 拼图
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档