在堆叠的条形图中更改颜色

时间:2019-05-17 19:20:45

标签: r ggplot2 bar-chart

此条形图我需要将三种颜色设置为特定颜色。 RET应该是“ chartreuse3”, FiR / FG应该是海蓝宝石4, RHA应该是“ goldenrod1”。

我已经找到了解决此问题的一些方法,但是当我尝试实施该方法时,它将无法工作。

fall3 <- structure(list(Community = c("First-Year", "First-Year", "First-Year", 
"First-Year", "Upper-Class", "Upper-Class", "First-Year", "First-Year", 
"Upper-Class", "Upper-Class"), Division = c("FiR/FG", "RHA", 
"RHA", "RET", "FiR/FG", "RET", "RET", "RHA", "RHA", 
"FiR/FG"), Count = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1)), row.names = c(NA, 
-10L), class = c("tbl_df", "tbl", "data.frame"))



gg_color_hue <- function(n) {
  hues = seq(15, 375, length=n+1)
  hcl(h=hues, l=65, c=100)[1:n]
}

#make custom palette

mycols <- gg_color_hue(length(unique(fall3$Division)))
names(mycols) <- unique(fall3$Division)
mycols["RET"] <- 'sienna'
mycols["FiR/FG"] <- 'darkgoldenrod1'
mycols["RHA"] <- 'darkolivegreen3'


stick <- ggplot(fall3, aes(Community))+
  geom_bar(aes(fill = Division))+
 scale_color_manual(values=mycols)

我希望有一个带有适当颜色的条形图。此外,有些事情告诉我我做错了。因为我的图表显示,一年级和上层阶级的活动数量是相等的,而事实并非如此。

1 个答案:

答案 0 :(得分:0)

mycols<- c("RET" = "sienna", "FiR/FG" = "darkgoldenrod1", "RHA" = "darkolivegreen3")
ggplot(fall3, aes(Community, fill = Division))+
  geom_bar() +
  scale_fill_manual(values=mycols)

enter image description here