如何在 ggplot 中使用 colorRamp 绘制饼图?

How to use colorRamp in ggplot for a pie chart?

我正在尝试使用 ggplot2 制作饼图。

到目前为止我已经成功了,除了颜色组件。我正在尝试使用 colorRamp::primary.colors 因为我图表中的项目是分类的,所以渐变颜色模糊了每个项目之间的界限。我试过 R brewer,但颜色不够。

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

我不确定如何将 colorRamp 添加到我的代码中。应该先创建一个颜色列表,然后将其添加到 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

要事第一:Don't do pie charts!!


[w/o 看着你的数据,我一开始甚至没有意识到每个 alga 有更多的点,这在下面的 eh 点图表中很明显,什么是不可见的在 bar/pie 图表中)

要解决类别多于 brewer 颜色的问题,您可以轻松地求助于 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))))