Closed ixxmu closed 2 years ago
冬尽今宵长
本节来介绍如何使用ggplot2批量绘制饼图,后台回复20211120获取本文数据及代码,下面通过一个小例子来进行展示
package.list=c("tidyverse","ggsci","extrafont","ggtext")
for (package in package.list) {
if (!require(package,character.only=T, quietly=T)) {
install.packages(package)
library(package, character.only=T)
}
}
chain_investment <- read_csv('chain_investment.txt')
# A tibble: 6,035 x 5
category meta_cat group_num year gross_inv_chain
<chr> <chr> <dbl> <dbl> <dbl>
1 Total basic infrastructure Total infrastructure 1 1947 73279.
2 Total basic infrastructure Total infrastructure 1 1948 83218.
3 Total basic infrastructure Total infrastructure 1 1949 95760.
4 Total basic infrastructure Total infrastructure 1 1950 103642.
5 Total basic infrastructure Total infrastructure 1 1951 102264.
6 Total basic infrastructure Total infrastructure 1 1952 107278.
7 Total basic infrastructure Total infrastructure 1 1953 117635.
8 Total basic infrastructure Total infrastructure 1 1954 125306.
9 Total basic infrastructure Total infrastructure 1 1955 126923.
10 Total basic infrastructure Total infrastructure 1 1956 133562.
# ... with 6,025 more rows
df1 <- chain_investment %>%
filter(meta_cat == "Social" &
category %in% c("Education", "Health", "Public safety")) %>%
group_by(year) %>%
mutate(total = sum(gross_inv_chain)) %>%
ungroup() %>%
mutate(prop = gross_inv_chain / total,
category = factor(category,
levels = c("Public safety", "Health", "Education")))
df2 <- chain_investment %>%
filter(meta_cat == "Education") %>%
mutate(category = ifelse(grepl("S&L", category), "State and Local Government", category)) %>%
group_by(year, category) %>%
summarise(gross_inv_chain = sum(gross_inv_chain),
.groups = "drop") %>%group_by(year) %>%
mutate(total = sum(gross_inv_chain)) %>%
ungroup() %>%
mutate(prop = gross_inv_chain / total)
df3 <- chain_investment %>%
filter(grepl("Total", category) &
meta_cat == "Total infrastructure") %>%
group_by(year) %>%
mutate(total = sum(gross_inv_chain)) %>%
ungroup() %>%
mutate(prop = gross_inv_chain / total)
df <- df1 %>% bind_rows(df2) %>% bind_rows(df3) %>%
mutate(years=year) %>%
unite(.,col="type",meta_cat,years,sep="-",remove = T,na.rm = F) %>%
mutate(type=str_remove(type,"NA-"))
# A tibble: 639 x 7
category type group_num year gross_inv_chain total prop
<chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl>
1 Public safety Social-1947 17 1947 983. 12049. 0.0816
2 Public safety Social-1948 17 1948 906. 20287. 0.0447
3 Public safety Social-1949 17 1949 1255. 30922. 0.0406
4 Public safety Social-1950 17 1950 1459. 37087. 0.0393
5 Public safety Social-1951 17 1951 1183. 38568. 0.0307
6 Public safety Social-1952 17 1952 952. 37809. 0.0252
7 Public safety Social-1953 17 1953 1155. 38341. 0.0301
8 Public safety Social-1954 17 1954 1430. 46720. 0.0306
9 Public safety Social-1955 17 1955 1556. 49631. 0.0313
10 Public safety Social-1956 17 1956 1458. 48918. 0.0298
# ... with 629 more rows
theme_set(theme_minimal(base_size = 12))
theme_update(
panel.grid.minor=element_blank(),
panel.grid.major=element_blank(),
panel.background=element_rect(fill = bcolor,color = NA),
plot.background=element_rect(fill = bcolor,color = NA),
axis.title=element_blank(),
axis.line=element_blank(),
axis.text=element_blank(),
axis.ticks=element_blank(),
strip.text=element_markdown(size=8,color="black",hjust = 0.5),
plot.margin=margin(t =5,r = 0,b = 5,l = 5)
)
ggplot(df %>% filter(year == 1957 | year == 1987 | year == 2017),
mapping = aes(x = "",y = prop,fill = category)) +
geom_col() +
coord_polar("y") +
scale_fill_npg()+
guides(fill = "none") +
facet_wrap(~ type)
ggsave("2021-11-20\\pie.png",
plot = last_plot(),
device = "png",
width = 5.5,
height = 7,
type = "cairo")
欢迎大家扫描下方二位码加入微信交流群或者QQ交流群
关注下方公众号下回更新不迷路,如需要加入微信交流群,请在菜单栏处添加作者微信,备注单位+方向+姓名即可邀您进群;公众号主要分享生物信息学与R语言数据可视化的经典案例,希望对大家有所帮助
往期推荐
gggibbous绘制迷你月球图版折线图
手把手带你绘制CNS级别的气泡图
ggplot2带你绘制质感美美的云雨图
箱线图|小提琴图,你需要的他都有
如何绘制出美丽的基因密度图
手把手教你绘制基因组图
11月送书福利活动-10本R语言经典著作
手把手教你配置Linux系统(Ubuntu 中)
手把手教你配置Linux系统(Ubuntu 下)
手把手教你配置Linux系统(Ubuntu 上)
Microsoft Edge一款你值得使用的浏览器
Alpha多样性指数计算及可视化
https://mp.weixin.qq.com/s/PDbbxB7nKVu0xEpDJh-D7A