【问题标题】:Manually coloring dodged multiple bars in ggplot2 [duplicate]在ggplot2中手动着色躲避多个条[重复]
【发布时间】:2023-03-31 06:49:01
【问题描述】:

我想在 ggplot2 中手动为闪避的多个条形着色。我正在使用 scale_fill_manual 指定颜色,但它仍使用默认颜色,如屏幕截图所示。

   library(ggplot2)
   library(dplyr)
   library(tidyr)

  # Some fake data

  by_state<- data.frame(
   state=c("Florida","New York","Nebraska","Nevada","Texas"),
   healthy=c(19,16,15,20,22),
   danger=c(2,4,6,2,1),
   warning=c(4,5,8,3,3),
   stringsAsFactors = FALSE
       )


   by_state%>%gather(Condition,value,-state)%>%
  ggplot(aes(x=state,y=value,fill=Condition))+
  geom_bar(stat = "identity", position = "dodge")+ 
  scale_colour_manual(values = c("red","blue", "orange"))

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    您需要scale_fill_manual 来修改填充。颜色只修改颜色美感,不修改填充美感。

    如果同时包含颜色和填充(增大尺寸以突出显示条形周围的颜色),您可以更清楚地看到这一点:

    by_state%>%gather(Condition,value,-state)%>%
      ggplot(aes(x=state,y=value,fill=Condition, color =Condition))+
      geom_bar(stat = "identity", position = "dodge", size = 2)+ 
      scale_fill_manual(values = c("red","blue", "orange"))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多