欢迎关注R语言数据分析指南
❝本节来介绍一下ggplot2 3.5版新更新的内容之coord_radial()用于绘制极坐标图,使用过程非常的简洁,更多详细内容请参考作者官方文档。
❞
官方文档
❝https://www.tidyverse.org/blog/2024/03/ggplot2-3-5-0-coord-radial/#text-annotations
❞
# 运行前请安装最新版ggplot2
#install.packages("ggplot2")
library(tidyverse)
library(patchwork)
library(scales)
极坐标饼图
pie <- ggplot(mtcars, aes(y = factor(1), fill = factor(cyl))) +
geom_bar(width = 1) +
scale_y_discrete(guide = "none", name = NULL) +
guides(fill = "none")
default <- pie + coord_radial() + ggtitle("default")
no_expand <- pie + coord_radial(expand = FALSE) + ggtitle("expand = FALSE")
polar <- pie + coord_polar() + ggtitle("coord_polar()")
default | no_expand | polar
极坐标三点图
p <- ggplot(mpg, aes(displ, hwy)) +
geom_point()
half <- p + coord_radial(start = -0.5 * pi, end = 0.5 * pi) +
ggtitle("−0.5π to +0.5π")
quarter <- p + coord_radial(start = 0, end = 0.5 * pi) +
ggtitle("0 to +0.5π")
half | quarter
甜甜圈图
p + coord_radial(inner.radius = 0.3, r_axis_inside = TRUE)
添加文本
ggplot(mtcars, aes(seq_along(mpg), mpg)) +
geom_col(width = 0.8) +
geom_text(
aes(y = 32, label = rownames(mtcars)),
angle = 90, hjust = 1
) +
coord_radial(rotate_angle = TRUE, expand = FALSE)
极坐标boxplot
ggplot(mpg, aes(class, displ)) +
geom_boxplot() +
coord_radial(start = 0.25 * pi, end = 1.75 * pi) +
guides(
theta = guide_axis_theta(angle = 0),
r = guide_axis(angle = 0)
)
添加辅助线
ggplot(pressure, aes(temperature, pressure)) +
geom_line(colour = "blue") +
scale_x_continuous(
labels = label_number(suffix = "°C"),
sec.axis = sec_axis(~ .x * 9/5 + 35, labels = label_number(suffix = "°F"))
) +
scale_y_continuous(
labels = label_number(suffix = " mmHg"),
sec.axis = sec_axis(~ .x * 0.133322, labels = label_number(suffix = " kPa"))
) +
guides(
theta.sec = guide_axis_theta(theme = theme(axis.line.theta = element_line())),
r.sec = guide_axis(theme = theme(axis.text.r = element_text(colour = "red")))
) +
coord_radial(
start = 0.25 * pi, end = 1.75 * pi,
inner.radius = 0.3
)
关于永久群内容的说明
❝给予长期支持我们的读者们一个特别待遇:购买小编2023版VIP会员文档的读者,「将自动获得2024年及以后更新的绘图文档代码,无需额外付费」。购买2022版会员文档,将获取2022+2024及后续的绘图文档。
目前会员文档(2023+2024)「已经更新上传了140+案例文档」,每个案例都附有相应的数据和代码,并配有对应的注释文档,方便大家学习和参考。
❞
详细内容欢迎到小编的「淘宝店铺」 「R语言数据分析指南」下单购买,内容主要包括各种「高分论文的图表分析复现以及一些个性化图表的绘制」均包含数据+代码。购买会员文档后微信发小编订单号即邀请进新的会员交流群。
「2024更新的绘图内容同时包含数据+代码+markdown注释文档+文档清单」
淘宝店铺(有需要欢迎关注)


