首页 > 解决方案 > geom_point 中的 R 填充百分比

问题描述

我想制作一个圆形图表,其中rec列中的值对应于应该用黑色填充的圆形百分比。我在 Photoshop 中做了一个示例(参见图 1 中的 obs A 和 B)。在我的搜索中,我找到了一些解释bar_plot,但没有一个在我的脚本中起作用

有谁知道如何做到这一点或建议另一个图表选项来证明我想要什么?

ggplot(Dataset, aes(sp,log(num))) + 
  geom_point(aes(size = pref, color=as.factor(ex)), shape = 21, stroke = 2)+
  scale_size_continuous(range = c(4,10)) +
  scale_colour_manual(values=c("black", "red")) + # set the border to the bg color
  ggthemes::theme_few() +
  guides(color = FALSE) + # remove the legend for the border
  theme(axis.text.x = element_text(angle = 90, vjust = .5))

数据:

grupo   sp  num porc_rec    rec pref    ex
ave A   47  74.46808511 35  7   0
ave B 22    22.72727273 5   3   0
ave C   5   0   0   0   0
ave D   4   0   0   0   0
ave E   2   0   0   0   0
ave F   2   0   0   0   0
ave G   2   0   0   0   1
ave H   2   0   0   0   0
ave I   1   0   0   1   0
ave J   1   0   0   0   0
ave L 1 0   0   0   0
ave M   1   0   0   0   0
ave N   1   0   0   0   0
ave O   1   0   0   0   0
ave P   1   0   0   0   0
ave Q   1   0   0   0   0
ave R   1   0   0   0   0
ave S   1   0   0   0   1
ave T   1   0   0   0   1
ave U   1   0   0   0   1

在此处输入图像描述

标签: rggplot2geometrypercentagepoint

解决方案


使用该scatterpie软件包,您可以生成类似于您要求的内容(半径缩放并不完美,但应该可以调整以获得您想要的结果:

dat = structure(list(group = c("ave", "ave", "ave", "ave", "ave", "ave", 
        "ave", "ave", "ave", "ave", "ave", "ave", "ave", "ave", "ave", 
        "ave", "ave", "ave", "ave", "ave"), sp = c("A", "B", "C", "D", 
        "E", "F", "G", "H", "I", "J", "L", "M", "N", "O", "P", "Q", "R", 
        "S", "T", "U"), num = c(47L, 22L, 5L, 4L, 2L, 2L, 2L, 2L, 1L, 
        1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), porc_rec = c(74.46808511, 
        22.72727273, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
        0, 0), rec = c(35L, 5L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
        0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), pref = c(7L, 3L, 0L, 0L, 0L, 
        0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), 
        ex = c(0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 
        0L, 0L, 0L, 0L, 1L, 1L, 1L)), class = "data.frame", row.names = c(NA, 
        -20L))

library(ggplot2)
library(scatterpie)

dat$idx = as.numeric(1:nrow(dat))

input = dat[,c("idx","num","porc_rec","ex","pref")]
input$recip = 100 - input$porc_rec
input$radius = abs(0.4 / scale((input$pref + 1)/ max(input$pref),center = 7))
ggplot() + geom_scatterpie(data=input, aes(x=idx, y=log(num),color = factor(ex), r = radius), cols=c("porc_rec","recip")) +
  coord_equal() +
  scale_fill_manual(values = c("black","white")) + scale_colour_manual(values = c("black","red"))

在此处输入图像描述


推荐阅读