ggplot画鸡冠花图

时间:2022-07-22
本文章向大家介绍ggplot画鸡冠花图,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

鸡冠花图,又称为玫瑰图,可以通过极坐标coord_polar()改变barplot来获得。

首先画一个简单的bar图

library(ggplot2)
bar <- ggplot(data = diamonds) +
geom_bar(
mapping = aes(x = cut, fill = cut),
show.legend = FALSE,
width = 1
) +
theme(aspect.ratio = 1) +
labs(x = NULL, y = NULL)
bar

通过coord_polar() 函数使用极坐标系将barplot改为鸡冠花图。

bar + coord_polar()

还可以通过调整width来调整距离,如将width改成0.5

library(ggplot2)
bar <- ggplot(data = diamonds) +
geom_bar(
mapping = aes(x = cut, fill = cut),
show.legend = FALSE,
width = 0.5
) +
theme(aspect.ratio = 1) +
labs(x = NULL, y = NULL)
bar + coord_polar()

选自《R数据科学》

欢迎关注微信公众号:生信编程日常