繁体   English   中英

R:ggplot更好的渐变色

[英]R: ggplot better gradient color

我正在使用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差别不大。

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

谢谢。

如何使用scale_fill_brewer ,它使用ColorBrewer网站上的调色板,由ColorBrewerRColorBrewer

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))

借用@ 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))

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

感谢@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))

谢谢。

我会使用scale_fill_manual = c(“red”,“green”),你可以根据需要添加更多颜色或者scale_fill_brewer(palette =“Reds”),我喜欢这个。 你可以在这里使用调色板

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM