SCI绘图-饼图又能耍什么花样呢?

共 8188字,需浏览 17分钟

 ·

2024-04-11 09:10


434dab92b339df5cc049b029bad65bf5.webp

从过年放假到现在,已经断更一个多月,加上最近又沉迷金铲铲不想更新(玩物丧志啊😭,人总是要进步的,是时候收收心,重新开始营业了! 本期内容将手把手教你绘制出饼图和圆环图,同时推荐被广泛使用的饼图绘制工具!

饼图的概念


As usual,在介绍一种新的图表之前,先介绍图的组成、元素和功能等,即使是像Pie Chart这种常见的基础图表(真的不是在水🐥


饼图是一种常见的数据可视化图表,用于展示数据的相对比例。它通常呈圆形,被分成若干个扇形区域,每个扇形区域的大小表示相应数据的比例或百分比。饼图适用于展示数据的分布情况,尤其是用于比较各部分与整体的关系。


使用ggplot2绘制饼图


接下来,手把手教你如何调教ggplot生成优美的SCI图!比起封装好的方法,有时候原生的方法会更加有趣。


首先,模拟出绘图的数据


      
library(ggplot2)



# 创建模拟数据



data <- data.frame(


category = c("Technology", "Healthcare",


"Finance", "Education",


"Entertainment", "Travel"),


value = c(20, 15, 7, 10, 18, 3)


)


ggplot2中饼图实现是通过先绘制堆积的条形图,然后将坐标转为极坐标系来 实现的


      
p <- ggplot(data, aes(x = "", y = value, fill = category)) +


geom_bar(stat = "identity", width = 1) +


scale_fill_brewer(palette = "RdYlBu")



p



20a5f42dd0f5661b2ce43e310af13a2c.webp


      

# 转为极坐标系



p + coord_polar("y", start = 0)


bd95b41b0a61f71d768edf7e6a3c1440.webp

图片美化:删除无用的数字标签,删除网格和灰色背景等
      
ggplot(data, aes(x = "", y = value, fill = category)) +


geom_bar(stat = "identity", width = 1, color='white') +


scale_fill_brewer(palette = "RdYlBu") +


coord_polar("y", start = 0) +


theme_void() +


theme(legend.position = "right",


plot.title = element_text(hjust = 0.5)) +


labs(title = "Pie Chart Example using ggplot2",


fill = "Class")


c5bc325083da9b777a35036eee719fb2.webp


在饼图内部添加百分比标签


      

# 计算标签的坐标



data <- data %>%


arrange(desc(category)) %>%


mutate(prop = value / sum(data$value) *100) %>%


mutate(ypos = cumsum(prop)- 0.5*prop)



# bar chart



ggplot(data, aes(x = '', y = prop, fill = category)) +


geom_bar(stat = "identity", color='white', width = 1) +


scale_fill_brewer(palette = "RdYlBu") +


coord_polar("y", start = 0) +


theme_void() +


theme(legend.position = "right",


plot.title = element_text(hjust = 0.5)) +


geom_text(aes(y=ypos, label = paste0(round(prop), "%")),


color = "black",


size = 4) +


labs(title = "Pie Chart Example using ggplot2",


fill = "Class")


d063e3ee97072671be61c1f4b3ef0e46.webp


调整标签位置


      
ggplot(data, aes(x = '', y = prop, fill = category)) +


geom_bar(stat = "identity", color='white', width = 1) +


scale_fill_brewer(palette = "RdYlBu") +


coord_polar("y", start = 0) +


theme_void() +


theme(legend.position = "right",


plot.title = element_text(hjust = 0.5)) +


geom_text(aes(x=1.6, y=ypos, label = paste0(round(prop), "%")),


color = "black",


size = 4) +


labs(title = "Pie Chart Example using ggplot2",


   fill = "Class")


636590cdb4510afc7bb6a545565fbd17.webp


美化标签,添加连线


      
ggplot(data, aes(x = '', y = prop, fill = category)) +


geom_bar(stat = "identity", color='white', width = 1) +


scale_fill_brewer(palette = "RdYlBu") +


coord_polar("y", start = 0) +


theme_void() +


theme(legend.position = "right",


plot.title = element_text(hjust = 0.5)) +


geom_text_repel(aes(x=1.5, y=ypos,


label = paste0(round(prop), "%")),


color = "black",


size = 4,


nudge_x = 0.15) +


labs(title = "Pie Chart Example using ggplot2",


fill = "Class")


1f50ba24124e7c06faf50b175f90c77b.webp


d8b4a2b4f0fc1ca668a596fab51a0fdb.webp

接着调整x甚至能变成圆环图,意外收获,原理我也不清楚了!
      
ggplot(data, aes(x = '', y = prop, fill = category)) +


geom_bar(stat = "identity", color='white', width = 1) +


scale_fill_brewer(palette = "RdYlBu") +


coord_polar("y", start = 0) +


theme_void() +


theme(legend.position = "right") +


geom_text(aes(x=-0.5, y=ypos, label=''))


d463202fe73ee798c4dd269bd0d6ea07.webp

接着添加一些标签,Donut Chart直接就成了!(圆环图的标准实现其实是从矩形旋转坐标系的得到的)
      
data <- data %>%


mutate(label = paste0(category,"(",round(prop,2),"%",")"))







ggplot(data, aes(x = '', y = prop, fill = category)) +


geom_bar(stat = "identity", color='white', width = 1) +


scale_fill_brewer(palette = "RdYlBu",


labels=sort(data$label))+


coord_polar("y", start = 0) +


theme_void() +


theme(legend.position = "right") +


geom_text(aes(x=-0.5, y=ypos, label='')) +


geom_text_repel(aes(x=1.5, y=ypos, label=category),


nudge_x=0.5)


5869bd56769289ed9f0e796815e6643a.webp

Next, 试试封装好的方法吧!

R自带的pie方法


      
# 模拟数据


pie.sales <- c(0.12, 0.3, 0.26, 0.16, 0.04, 0.12)


names(pie.sales) <- c("Blueberry", "Cherry",


"Apple", "Boston Cream", "Other", "Vanilla Cream")







# 绘图方法


pie(pie.sales, col = c("purple", "violetred1", "green3",


"cornsilk", "cyan", "white"))


bcbcd1d7e995e1bdbbfc5cfccd91cd1d.webp





PieChart using lessR


使用R包lessR来绘制饼图和圆环图


      

# install.packages("lessR")



library(lessR)



# 使用ggplot2部分的示例数据



PieChart(category, value


data=data,


        fill="viridis",


hole=0,


main="Pie Chart using lessR")


1ea9cab4c825db94720329c80bcd8a5b.webp


调整hole参数使其变成圆环图


      
PieChart(category, value, data=data,


fill="viridis",hole=0.65,


add="Donut Chart using lessR", x1=0, y1=0,


main="")


0f16154dd89188d241104313dacd199f.webp


3D饼图


3D的感觉蛮丑的,个人不太推荐!图表的目的是把数据信息清晰地传递给读者,感觉3D饼图有点画蛇添足了!当然,或许你喜欢呢!


    # install.packages("plotrix")
library(plotrix)

data <- c(19, 21, 54, 12, 36, 12)

pie3D(data,
col = hcl.colors(length(data), "Spectral"),
labels = data)


11bc666bfb7a43fd812e5a445f5804d2.webp


饼图,我裂开了


    # install.packages("plotrix")
library(plotrix)

data <- c(19, 21, 54, 12, 36, 12)

pie3D(data,
col = hcl.colors(length(data), "Spectral"),
labels = data,
explode = 0.2)


fbe2c8a5fffc2c7487a25c5d73cbfd67.webp


Question

最后,我有个朋友想问大家下面这种图叫什么?知道的大神请留言!

e658615ceec491197fbf1a662ccacd2b.webp




893f80321607376507fa7f62aa127f0f.webp

点赞👍关注


本期就介绍到这里啦!持续更新ING~


10fea7282c357ac14e4135eb5557e3e9.webp

整理不易,期待您的点赞分享转发...






de753885def7f38ca7cb72427da73959.webp

往期精彩推荐

SCI绘图-网络图可视化最详细教程! SCI绘图-网络(Graph)图可视化工具合集 龙年微信红包封面!限量2000份! 科研论文还不知道如何配色2? 科研论文还不知道如何配色1? SCI绘图-ggplot绘制超好看的火山图 HTTPX,一个超强的爬虫库! GSEA分析实战及结果可视化教程! 一文读懂GSEA分析原理 SCI绘图-箱线图的绘制原理与方法


23bc08a34ee2b89f8f615422cdb670a9.webp

更多合集推荐


🔥#SCI绘图  💥 #生物信息   #科研绘图素材资源  📚 #数学  🌈 #测序原理


我知道你


在看



5bd9a7d6ca516758b8090cc2adddbd9e.webp
浏览 223
点赞
评论
收藏
分享

手机扫一扫分享

分享
举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

分享
举报