r - 使用 scatterpie 或 ggforce 制作多个饼图

标签 r ggplot2 ggforce scatterpie

我正在尝试在图像顶部绘制多个饼图。我想使用 custom_annotation 来绘制光栅图像。但现在我什至无法获得多个饼图。

最终我想要 6 个饼图绘制在图像顶​​部的不同位置。 imXimY 给出饼图在图像上的位置坐标。

head(wholebody_cutLH_wide_t[c(1,2,102,103,104)])
Acidobacteriaceae Actinomycetaceae  imX imY radius
1      0.000000e+00     7.665687e-05 2.00 5.5    0.5
2      0.000000e+00     4.580237e-04 1.50 1.0    0.5
3      0.000000e+00     4.112573e-04 1.75 2.0    0.5
4      6.431473e-04     3.856008e-02 0.30 1.0    0.5
5      0.000000e+00     3.013013e-04 1.50 4.8    0.5
6      3.399756e-05     1.372986e-02 1.50 5.2    0.5

现在这是我对 scatterpie 的尝试:

ggplot(wholebody_cutLH_wide_t) +
 #  annotation_custom(g, xmin=-Inf, xmax=Inf, ymin=-Inf, ymax=Inf) +
 geom_scatterpie(aes(x=imX, y=imY,r=radius),
            data=wholebody_cutLH_wide_t, cols=NA,color=sample(allcolors,101)) +
 scale_color_manual(values=sample(allcolors,101)) +
 scale_x_continuous(expand=c(0,0), lim=c(0,3)) +
 scale_y_continuous(expand=c(0,0), lim=c(0,6)) +
 theme(legend.position="none",
    panel.background = element_rect(fill = "transparent") # bg of the panel
    , plot.background = element_rect(fill = "transparent") # bg of the plot
    , panel.grid.major = element_blank() # get rid of major grid
    , panel.grid.minor = element_blank(), # get rid of minor grid
    line = element_blank(),
    text = element_blank(),
    title = element_blank()
  )  

现在我的错误是:

Error: Only strings can be converted to symbols

这是我对 dplyrggforce 的尝试:

dat_pies<-left_join(wholebody_cutLH,
                wholebody_cutLH %>%
                  group_by(tax_rank) %>%
                  summarize(Cnt_total = sum(count_norm))) %>%
group_by(tax_rank) %>%
mutate(end_angle = 2*pi*cumsum(count_norm)/Cnt_total,      # ending angle for   each pie slice
     start_angle = lag(end_angle, default = 0),   # starting angle for each pie slice
     mid_angle = 0.5*(start_angle + end_angle))

ggplot(dat_pies) + 
geom_arc_bar(aes(x0 = imX, y0 = imY, r0 = 0, r = rpie,
               start = start_angle, end = end_angle, fill = Volume)) +
geom_text(aes(x = rlabel*sin(mid_angle), y = rlabel*cos(mid_angle), label = Cnt),
        hjust = 0.5, vjust = 0.5) +
coord_fixed() +
scale_x_continuous(expand=c(0,0), lim=c(0,3)) +
scale_y_continuous(expand=c(0,0), lim=c(0,6)) +

这里我的错误是:

Error in ggplot(dat_pies) + geom_arc_bar(aes(x0 = imX, y0 = imY, r0 = 0,  : 
could not find function "+<-"

任何有关这些方法的帮助都会很棒。 谢谢

最佳答案

在第一次尝试时,您应该使用:

ggplot(wholebody_cutLH_wide_t) +
  geom_scatterpie(aes(x=imX, y=imY,r=radius),
     data=wholebody_cutLH_wide_t,
     cols=colnames(wholebody_cutLH_wide_t)[1:2],
     color=NA, alpha=.8)

enter image description here

关于r - 使用 scatterpie 或 ggforce 制作多个饼图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47718439/

相关文章:

反转数据框中的行顺序

r - 如何用按平均值着色的较小矩形填充矩形

r - ggplot2 和 GLM : plot a predicted probability

r - tidyr::spread tidyr::pivot_wider 每个键有多个不同的值

r - 同时为 dplyr 中的分组值导出唯一值和汇总值的有效方法

r - 在 ggplot2 中订购饼图切片

r - ggforce facet_zoom 如何仅注释缩放的图形

r - 如何在不引用列号的情况下选择数据框中的最后 x 列?

r - 使用 ggplot 组合条形图和折线图