美文网首页
ggplot2优雅的绘制圆形柱状图

ggplot2优雅的绘制圆形柱状图

作者: R语言数据分析指南 | 来源:发表于2021-07-27 00:17 被阅读0次

本节来介绍如何利用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语言数据分析指南,持续分享数据可视化的经典案例及一些生信知识,希望对大家有所帮助

相关文章

网友评论

      本文标题:ggplot2优雅的绘制圆形柱状图

      本文链接:https://www.haomeiwen.com/subject/wyknmltx.html