rggplot2scatterpie

correct use of colors in scatterpie


I want to make 6 different pies using scatterpie. There are 101 different categories making up the pies (not all pies have 101), so I want to be able to differentiate the colors.

This does not give me enough colors (I can tell just by looking at the pies)

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=colnames(wholebody_cutLH_wide_t[1:101]),color=NA) +
#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()
)  

enter image description here

Then if I try to set the colors manually as below I get a blank screen. If I try and set the colors in the scatterpie (color=sample(allcolors,101)), then I get the error

Error: Aesthetics must be either length 1 or the same as the data (2864): colour

allcolors = grDevices::colors()[grep('gr(a|e)y', grDevices::colors(), invert = T)]
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=colnames(wholebody_cutLH_wide_t[1:101]),color=NA) +
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()
 )  

Solution

  • Here is the final working code. I had to switch the scale_color_manual to scale_fill_manual.

    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=colnames(wholebody_cutLH_wide_t)[1:101],color=NA) +
    scale_fill_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()
    )  
    

    And here is the plot

    enter image description here