ggplot2data-visualizationpie-chartcolor-schemecolorbrewer

How to use colorRamp in ggplot for a pie chart?


I am trying to make a pie chart using ggplot2.

So far I have been successful, except for the color component. I am trying to use colorRamp::primary.colors because the items in my chart are categorical, so gradient colors blurred the boundaries between each item. I tried R brewer, but it doesn't have enough color.

pie chart

https://www.nceas.ucsb.edu/~frazier/RSpatialGuides/colorPaletteCheatsheet.pdf

I am not sure how to add colorRamp to my code. Should create a list of color first and then add it to ggplot?

winter <- structure(list(site = c("CI", "CI", "CI", "CI", "CI", "CI", "CI", 
"CI", "PI", "PI", "PI", "PI", "MF", "MF", "MF", "MF", "MF", "MF", 
"MF", "LK", "LK", "LK", "LK", "LK", "LK", "LK", "MF", "MF", "MF", 
"MF", "MF", "BL", "BL", "BL", "BL", "BL", "BL", "BL", "BL", "BL", 
"BL", "BL", "BL", "LP", "LP", "LP", "LP", "LP", "LP", "LP", "LP", 
"NP", "NP", "NP", "NP", "NP", "NP", "NP", "TC", "TC", "TC", "TC", 
"TC", "TC", "SO", "BI", "WI", "WI", "WI"), alga = c("Plocamium", 
"Colpomenia", "Hypnea", "Ulva", "Colpomenia", "Dictyota", "S. hemiphyllum", 
"Ulva", "Colpomenia", "Dictyota (small)", "S. hemiphyllum", "Ulva", 
"Colpomenia", "Colpomenia", "Corallina", "Dictyota (small)", 
"Gelidium", "S. hemiphyllum", "Ulva", "Colpomenia", "Dictyota (big)", 
"Dictyota (small)", "Plocamium", "Pockiela", "S. hemiphyllum", 
"S.tenerrimum", "Colpomenia", "Corallina", "Padina", "S. hemiphyllum", 
"Ulva", "Colpomenia", "Dictyota (big)", "Dictyota (small)", "Dictosphaesia", 
"Plocamium", "Pockiela", "Cladophora", "Corallina", "Gelidium", 
"Gigartina", "S. hemiphyllum", "Ulva", "Codium", "Colpomenia", 
"Ulva", "Colpomenia", "Gelidium", "Gigartina", "S. hemiphyllum", 
"Ulva", "Colpomenia", "Colpomenia", "Gelidium", "Gigartina", 
"S. hemiphyllum", "S.tenerrimum", "Ulva", "Colpomenia", "Ulva", 
"Colpomenia", "Dictyota (big)", "S. hemiphyllum", "Ulva", "Ulva", 
"Colpomenia", "Colpomenia", "S. hemiphyllum", "Ulva"), kg = c(0.00209, 
0.84109, 0.00746, 0.05966, 0.01617, 0.00189, 27.37882, 1.61209, 
1.5998, 0.00969, 6.89784, 0.00049, 0.23829, 2.4064, 0.00608, 
0.01422, 0.01735, 2.55, 0.11174, 0.68972, 1.22544, 0.01607, 0.00719, 
0.00044, 0.35, 0.00401, 0.23829, 0.83879, 0.2129, 1.9627, 0.00157, 
0.02963, 0.03386, 0.49223, 5e-04, 0.04417, 1.61504, 0.00121, 
0.06069, 0.00944, 0.4431, 5.35, 0.3, 0.00963, 0.35527, 0.00257, 
0.74142, 0.06269, 0.20287, 30.7, 0.02773, 0.02052, 0.01596, 0.0467, 
0.01082, 21.92626, 0.09068, 0.24969, 0.09909, 0.00143, 0.1968, 
1.10294, 4.85, 0.10844, 4.1, 1.97947, 0.00358, 13.52087, 0.01678
)), row.names = c(NA, -69L), class = c("tbl_df", "tbl", "data.frame"
))

library(ggplot2)
library(colorRamps)
weight <- ggplot(winter, aes(x = "", y = kg, fill = alga)) +
  geom_bar(width = 1, stat = "identity")

pie <- weight + 
  coord_polar("y", start = 0) +
  colorRampPalette(primary.colors(17), no.white = TRUE) #this is where I am stuck
pie

Solution

  • First things first: Don't do pie charts!!


    [w/o looking at your data i did not even realize in the first place that there are more points per alga, which is nicely visible int eh point chart below and what was not visible in the bar/pie chart)

    To solve the problem of having more categories than brewer colors, you can easily fall back on colorRampPalette:

    library(RColorBrewer)
    
    my_pal <- colorRampPalette(brewer.pal(9, "Set1"))
    
    weights <- ggplot(winter, 
                      aes(x = reorder(alga, kg, max), 
                          y = kg, 
                          color = reorder(alga, kg, max))) +
               geom_point() + coord_flip()
    
    
    weights + scale_color_manual("", values = my_pal(nlevels(as.factor(winter$alga))))
    

    enter image description here