首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >R语言ggplot2每周一图活动:第四周~簇状柱形图和堆积柱形图

R语言ggplot2每周一图活动:第四周~簇状柱形图和堆积柱形图

作者头像
用户7010445
发布2022-05-23 15:55:21
发布2022-05-23 15:55:21
8040
举报

堆积柱形图代码数据来源

https://github.com/kaustavSen/tidytuesday/blob/master/2021/week_11.R

感兴趣的可以尝试下原始代码

我将数据整理部分的代码去掉了,直接用整理好的数据作图

代码语言:javascript
复制
library(readr)
library(tidyverse)

bechdel_test_df<-read_csv("bechdel_test_df.csv")
bechdel_test_text <- read_csv("bechdel_test_text.csv")
bechdel_step_df <- read_csv("bechdel_step_df.csv")
library(ggplot2)

bechdel_test_df %>% 
  mutate(clean_test = factor(clean_test, 
                             levels = c("ok", "dubious", "men", "notalk", "nowomen")),
         clean_test = fct_rev(clean_test)) %>% 
  ggplot(aes(year_group, category_prop)) +
  geom_col(aes(fill = clean_test), width = 1, color = "white", 
           size = 0.6, show.legend = FALSE) +
  geom_segment(data = bechdel_step_df, 
               aes(x = x_coord, xend = x_end_coord, 
                   y = category_prop, yend = category_prop), 
               size = 1.5) +
  geom_segment(data = filter(bechdel_step_df, year_group != "2010 -\n'13"), 
               aes(x = x_end_coord, xend = x_end_coord, 
                   y = y_coord, yend = y_end_coord), 
               lineend = "round", size = 1.5) +
  geom_segment(aes(x = 0.12, xend = 9.5, y = 0, yend = 0), 
               size = 0.8) +
  geom_segment(aes(x = 0.25, xend = 9.5, y = 1, yend = 1),
               size = 0.8, color = "#cdcdcd") +
  geom_segment(data = tibble(x = 0.12, xend = 0.5, y = c(0.25, 0.5, 0.75)), 
               aes(x = x, xend = xend, y = y, yend = y), 
               size = 0.8, color = "#cdcdcd") +
  geom_text(data = tibble(x = 0, y = c(0, 0.25, 0.5, 0.75, 1), 
                          label = c(0, 25, 50, 75, 100)), 
            aes(x = x, y = y, label = label), 
            family = "serif", size = 6, hjust = 1) +
  geom_text(data = tibble(x = 0.2, y = 1, label = "%"), 
            aes(x = x, y = y, label = label), 
            family = "serif", size = 7, hjust = 1) +
  geom_text(data = tibble(x = c(0.5, 2.5, 4.5, 6.5, 8.5), 
                          y = -0.06, 
                          label = c("1970-\n'74", "1980-\n'84", "1990-\n'94", "2000-\n'04", "2010-\n'13")), 
            aes(x = x, y = y, label = label), 
            family = "serif", size = 6, hjust = -0.2, lineheight = 0.55) +
  geom_segment(data = bechdel_test_text, 
               aes(x = 9.5, xend = 9.75, y = y, yend = y), 
               size = 0.8) +
  geom_text(data = bechdel_test_text, 
            aes(x = 9.85, y = y, label = label), 
            family = "serif", hjust = 0, 
            vjust = 0.5, size = 6, lineheight = 0.6) +
  annotate("text", x = 3.75, y = 0.22, 
           label = "PASS", family = "serif", 
           fontface = "bold", size = 25, 
           hjust = 0, vjust = 0.5) +
  annotate("text", x = 4, y = 0.75, 
           label = "FAIL", family = "serif",
           fontface = "bold", size = 25, hjust = 0, vjust = 0.5) +
  scale_y_continuous(expand = c(0, 0)) +
  scale_fill_manual(values = c("ok" = "#008fd5", "dubious" = "#6bb2d5",
                               "men" = "#ffc9bf", "notalk" = "#ff9380", "nowomen" = "#ff2700")) +
  labs(title = "The Bechdel Test Over Time", 
       subtitle = "How women are represented in movies", 
       x = "", y = "", 
       caption = "Original plot by Fivethirtyeight | Replicated in R by Kaustav Sen") +
  coord_cartesian(clip = "off") +
  theme_void() +
  theme(
    plot.title.position = "plot",
    plot.title = element_text(family = "serif", face = "bold", 
                              size = 30, hjust = -0.12, margin = margin(b = 5)),
    plot.subtitle = element_text(family = "serif", size = 24, hjust = -0.12, margin = margin(b = 25)),
    plot.caption = element_text(family = "serif", size = 14, hjust = 0.5, vjust = -25, color = "grey70"),
    plot.margin = margin(20, 90, 25, 45),
    plot.background = element_rect(fill = "#f0f0f0", color = "#f0f0f0")
  )

image.png

簇状柱形图的代码

原始代码链接

https://www.rebeccabarter.com/blog/2018-05-29_getting_fancy_ggplot2/

代码语言:javascript
复制
library(readr)
success_rates<-read_csv("success_rates.csv")
library(ggplot2)
ggplot(success_rates) +
  # add bar for each discipline colored by gender
  geom_bar(aes(x = discipline, y = success, fill = gender),
           stat = "identity", position = "dodge") +
  # name axes and remove gap between bars and y-axis
  scale_y_continuous("Success Rate", expand = c(0, 0)) +
  scale_x_discrete("Discipline") +
  scale_fill_manual(values = c("#468189", "#9DBEBB")) +
  # remove grey theme
  theme_classic(base_size = 18) +
  # rotate x-axis and remove superfluous axis elements
  theme(axis.text.x = element_text(angle = 90, 
                                   hjust = 1, vjust = 0),
        axis.line = element_blank(),
        axis.ticks.x = element_blank()) 

image.png

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

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 堆积柱形图代码数据来源
  • 簇状柱形图的代码
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档