ggplot2优雅的绘制圆形柱状图

2021-07-27  本文已影响0人  R语言数据分析指南

本节来介绍如何利用ggchicklet包通过ggplot2来绘制圆形柱状图

加载R包

install.packages("ggchicklet", repos = "https://cinc.rud.is")
library(ggchicklet)
library(hrbrthemes)
library(tidyverse)

加载数据

data("debates2019")

数据清洗

df <- debates2019 %>%
  filter(debate_group == 1) %>% 
  mutate(speaker = fct_reorder(speaker,elapsed, sum,.desc=FALSE)) %>%
  mutate(topic = fct_other(topic,
    c("Immigration", "Economy", "Climate Change",
      "Gun Control", "Healthcare", "Foreign Policy")))

数据可视化

ggplot(df,aes(speaker, elapsed, group = timestamp, fill = topic)) +
  geom_chicklet(width = 0.5)+
  scale_y_continuous(
    expand = c(0, 0.0625),
    position = "right",
    breaks = seq(0, 14, 2),
    labels = c(0, sprintf("%d min.", seq(2, 14, 2)))
  ) +
  scale_fill_manual(
    name = NULL,
    values = c(
      "Immigration" = "#ae4544",
      "Economy" = "#d8cb98",
      "Climate Change" = "#a4ad6f",
      "Gun Control" = "#cc7c3a",
      "Healthcare" = "#436f82",
      "Foreign Policy" = "#7c5981",
      "Other" = "#cccccc"
    ),breaks = setdiff(unique(debates2019$topic), "Other")) +
  labs(x = NULL, y = NULL, fill = NULL) +
  theme_classic()+
  theme(axis.text.x = element_text(color="black",vjust=.5,hjust=1,
                                   size=10,angle=90),
        axis.text.y=element_text(color="black",size=10),
        legend.position = "top")

喜欢的小伙伴欢迎关注我的公众号

R语言数据分析指南,持续分享数据可视化的经典案例及一些生信知识,希望对大家有所帮助

上一篇 下一篇

猜你喜欢

热点阅读