R 在 geom_point 中的填充百分比

标签 r ggplot2 geometry percentage point

我想制作一个圆形图表,其中 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

enter image description here

最佳答案

使用 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"))

enter image description here

关于R 在 geom_point 中的填充百分比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54557699/

相关文章:

r - 如何将列中的值除以 R 中的特定行?

r - 让 Shiny (RMarkdown)使用完整的浏览器窗口

r - 同一图上的两个 geom_line 和 geom_point

r - 查看 ggplot2::qplot 中绘图对象的调用

ios - 在 objective-c 中对多边形进行三角剖分

java - 计算沿相对对角线的点偏移

math - 最小化距离的旋转矩阵

使用 na.approx 或 na.spline 替换矩阵数据的 NA

r - 更改 ylim(轴限制)会丢弃超出范围的数据。如何防止这种情况?

r - 像在 gtable 中一样制作 ggplot 面板背景