rggplot2ggforcescatterpie

multiple pies with scatterpie or ggforce


I am trying to plot multiple piecharts on top of an image. I want to use custom_annotation to plot the rastered image. But right now I can't even get the multiple piecharts.

Ultimately I want 6 pies plotting in differnet spots over top of an image. imX and imY give the coordinates of where the pies should be on the image.

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

Now here is my attempt with 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()
  )  

Now my error is:

Error: Only strings can be converted to symbols

Here is my attempt with dplyr and ggforce:

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)) +

Here my error is :

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

Any help with either of these methods would be great. thanks


Solution

  • In your first attempt you should use:

    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