如何解决在条形图中添加饼图
我的目标是创建一个分类变量的条形图,然后在其中添加一个饼形图,如所附图像所示。
我的数据与图片中的数据相同,并且在下面:
#For bar grapgh
chromosomes = c("1A","1B","1D","2A","2B","2D","3A","3B","3D","4A","4B","4D","5A","5B","5D","6A","6B","6D","7A","7B","7D")
Frequency = c(668,752,213,826,948,334,625,834,264,488,391,136,745,917,234,543,848,182,901,740,241)
data_bar <- data.frame(chromosomes,Frequency)
#For pie chart
Genome = c("A","B","D")
Count = c(4796,5430,1604)
data_pie <- data.frame(Genome,Count)
如果有人可以引导我或将我引向可以找到答案的地方,我将不胜感激
解决方法
这是一个ggplot2
解决方案:
pie <- ggplot(data_pie,aes(x = 1,y = Count,fill = Genome)) +
geom_col(color = "black") +
scale_fill_manual(values = c("red2","forestgreen","dodgerblue3")) +
coord_polar(theta = "y") +
theme_void() +
theme(legend.position = "none")
ggplot(data_bar) +
geom_col(aes(chromosomes,Frequency,fill = substr(chromosomes,2,2)),color = "black",width = 0.5) +
scale_fill_manual(values = c("red2","dodgerblue3")) +
theme_classic() +
theme(legend.position = "none") +
annotation_custom(ggplotGrob(pie),xmin = "2B",xmax = "6A",ymin = 500)
,
仅使用基本R函数,请参见以下代码:
## Your data ---------------------------------
#For bar grapgh
chromosomes = c("1A","1B","1D","2A","2B","2D","3A","3B","3D","4A","4B","4D","5A","5B","5D","6A","6B","6D","7A","7B","7D")
Frequency = c(668,752,213,826,948,334,625,834,264,488,391,136,745,917,234,543,848,182,901,740,241)
#For pie chart
Genome = c("A","B","D")
Count = c(4796,5430,1604)
## One idea to start with --------------------
plot.new()
par(mfrow = c(2,1),# set the layout,you might also consider layout() or split.screen() as suggested in ?par
mar=c(4,4,1,# this set up give enough space to see axis labels and titles
oma=c(0,0)) # remove outer margins
pie(Count,labels = Genome)
barplot(Frequency~chromosomes)
输出:
我认为可以通过调整par()
参数使它看起来更整洁,但我对它们并不十分熟悉。
也有cowplot
和gridExtra
软件包与ggplot2
很好地配合。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。