【问题标题】:R: ggplot better gradient colorR: ggplot 更好的渐变色
【发布时间】:2013-04-24 02:39:12
【问题描述】:

我正在使用 ggplot 绘制比例堆积条形图。我得到的情节是这样的:

这是我正在使用的自写函数:

df <- data.frame(id=letters[1:3],val0=1:3,val1=4:6,val2=7:9, val3=2:4, val4=1:3, val5=4:6, val6=10:12, val7=12:14)

PropBarPlot<-function(df, mytitle=""){
   melteddf<-melt(df, id=names(df)[1], na.rm=T)
   ggplot(melteddf, aes_string(x=names(df)[1], y="value", fill="variable")) + 
     geom_bar(position="fill") + 
     theme(axis.text.x = element_text(angle=90, vjust=1)) + 
     labs(title=mytitle)
}

print(PropBarPlot(df))

这里val4val5差别不大。

但由于颜色的原因,其中一些无法区分。有人能告诉我如何选择更好的颜色以便区分吗?

谢谢。

【问题讨论】:

  • 您能否提供一个示例数据框,以便更轻松地测试绘图?无论如何,我的想法是查看 ggplot 中的 scale_colour_brewer,它允许您使用来自 colorbrewer2.org 的调色板
  • @zelite 添加。这里基本上val4val5 并没有明显不同。

标签: r ggplot2 dataframe


【解决方案1】:

如何使用scale_fill_brewer,它使用来自ColorBrewer 网站的调色板,由包RColorBrewer 实现?

ggplot(diamonds, aes(clarity, fill=cut) ) +
geom_bar( ) +
scale_fill_brewer( type = "div" , palette = "RdBu" )

您可以选择多种不同的调色板。

require(RColorBrewer)
?brewer.pal

如果您需要更多颜色,可以使用colorRampPalette 功能在某些颜色之间进行插值(为此我会使用brewer.pal 调色板)。你可以这样做:

# Create a function to interpolate between some colours
mypal <- colorRampPalette( brewer.pal( 6 , "RdBu" ) )
# Run function asking for 19 colours
mypal(19)
 [1] "#B2182B" "#C2373A" "#D35749" "#E47658" "#F0936D" "#F4A989" "#F8BFA5"
 [8] "#FCD6C1" "#F3DDD0" "#E7E0DB" "#DAE2E6" "#CBE1EE" "#ADD1E5" "#90C0DB"
 [15] "#72AFD2" "#5B9DC9" "#478BBF" "#3478B5" "#2166AC"

在需要 8 种颜色的示例中,您可以像这样使用 scale_fill_manual()

PropBarPlot<-function(df, mytitle=""){
   melteddf<-melt(df, id=names(df)[1], na.rm=T)
   ggplot(melteddf, aes_string(x=names(df)[1], y="value", fill="variable")) + 
     geom_bar(position="fill") + 
     theme(axis.text.x = element_text(angle=90, vjust=1)) + 
     labs(title=mytitle)+
    scale_fill_manual( values = mypal(8) )
}

print(PropBarPlot(df))

【讨论】:

  • 我检查了不同的调色板,我得到的最好的是最多 12 种变化,而在我的情况下,我至少需要 19 种颜色。所有的调色板都有问题。
  • @RachitAgrawal 好的,我更新了一些可能对你有用的东西。检查编辑。
  • 遇到上述同样的问题。其中一些无法区分。
  • 那么你在一个情节中有太多的颜色类别!调色板有最大颜色数量是有原因的。考虑使用 facet_wrap 或 facet_grid 分割你的情节。
  • 如果情节中的相邻颜色来自调色板的相对部分,也许效果会更好?在示例中,您的蓝色接近蓝色,红色接近红色。如果他们交替,那么它会更好地区分吗?但我同意 SimonO101:如果区分这么多颜色有问题,也许你在一个情节中绘制了太多。
【解决方案2】:

从@SimonO101 借一些代码

library(ggplot2)
library(reshape2)
library(RColorBrewer)
mypal <- colorRampPalette( brewer.pal( 9 , "Set1" ) ) #you can try using different palete instead
#of "Set1" until it looks good to you

intercalate <- function(n){ #my crude attempt to shuffle the colors
  c(rbind(1:(n/2), n:(n/2+1))) #it will only work for even numbers
}

PropBarPlot<-function(df, mytitle=""){
  melteddf<-melt(df, id=names(df)[1], na.rm=T)
  ggplot(melteddf, aes_string(x=names(df)[1], y="value", fill="variable")) + 
    geom_bar(position="fill") + 
    theme(axis.text.x = element_text(angle=90, vjust=1)) + 
    labs(title=mytitle)+
    scale_fill_manual( values = mypal(8)[intercalate(8)] )
  #better would be to calculate the different number of categories
  #you have and put that instead of the number 8
}

df <- data.frame(id=letters[1:3],
                 val0=1:3,
                 val1=4:6,
                 val2=7:9, 
                 val3=2:4, 
                 val4=1:3, 
                 val5=4:6, 
                 val6=10:12, 
                 val7=12:14)

print(PropBarPlot(df))

看看这是否更适合您的需求。

【讨论】:

  • 我想出了一个更简单的解决方案来混合颜色。在下面添加。感谢您的帮助。
【解决方案3】:

感谢@zelite 和@SimonO101 的帮助。这是你们两个提议的更简单的版本。为了完整起见,在此处添加。

library(ggplot2)
library(reshape2)
library(RColorBrewer)

getColors<-function(n){
   mypal<-colorRampPalette(brewer.pal(12, "Paired"))
   sample(mypal(n), n, replace=FALSE)
}

PropBarPlot<-function(df, mytitle=""){
   melteddf<-melt(df, id=names(df)[1], na.rm=T)
   n<-length(levels(factor(melteddf$variable)))

   ggplot(melteddf, aes_string(x=names(df)[1], y="value", fill="variable")) + 
      geom_bar(position="fill") + 
      scale_fill_manual(values=getColors(n)) + 
      theme(axis.text.x = element_text(angle=90, vjust=1)) + 
      labs(title=mytitle)
}

df <- data.frame(id=letters[1:3],
             val0=1:3,
             val1=4:6,
             val2=7:9, 
             val3=2:4, 
             val4=1:3, 
             val5=4:6, 
             val6=10:12, 
             val7=12:14)

print(PropBarPlot(df))

谢谢。

【讨论】:

    【解决方案4】:

    我会使用 scale_fill_manual=c("red","green"),如果您愿意,可以添加更多颜色 要么 scale_fill_brewer(palette="Reds"),我喜欢这个。你可以在这里使用调色板

    【讨论】:

      猜你喜欢
      • 2020-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-10
      • 2019-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多