ggplot2绘制一个特别的柱状图
2021-06-04 本文已影响0人
R语言数据分析指南
本节来介绍一个ggplot2绘图的小案例,绘制一个简单但又富有特色的柱状图,喜欢的小伙伴欢迎关注获取数据及代码
library(tidyverse)
library(ggtext)
library(ggsci)
castaways <- readr::read_csv('castaways.txt')
df <- castaways %>%
select(season, personality_type, order) %>%
filter(!is.na(personality_type)) %>%
separate(personality_type,
into = c("mind","energy", "nature", "tactics"),
sep = 1:3) %>%
group_by(season) %>%
mutate(order_rev = pmin(18,max(order) - order + 1)) %>%
ungroup()
df %>%
count(order_rev, mind) %>%
ggplot(aes(x = order_rev, y = n, fill = mind,
col = mind,alpha = order_rev %in% c(1:3,15:18))) +
geom_col(position = "dodge")+
scale_x_continuous(breaks = 1:18, labels = c(1:17, ">18"))+
scale_fill_nejm()+
scale_color_nejm()+
scale_alpha_discrete() +
theme_light() +
labs(x=NULL)+
scale_y_continuous(expand=c(0,0))+
theme(axis.title.y = element_blank(),
axis.ticks.y = element_blank(),
plot.subtitle = element_markdown(size = 16),
plot.title = element_text(size = 20),
panel.grid.minor = element_blank(),
axis.title = element_text(size = 12),
axis.text = element_text(size = 12),
legend.position = "none")
喜欢的小伙伴欢迎关注我的公众号
R语言数据分析指南,持续分享数据可视化的经典案例及一些生信知识,希望对大家有所帮助