R:ggplot更好的渐变色 [英] R: ggplot better gradient color

查看:686
本文介绍了R:ggplot更好的渐变色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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 =){
meltingdf< -melt(df,id = names(df) ],na.rm = T)
ggplot(meltdf,aes_string(x = names(df)[1],y =value,fill =variable))+
geom_bar(position = fill)+
主题(axis.text.x = element_text(angle = 90,vjust = 1))+
labs(title = mytitle)
}

print(PropBarPlot(df))

这里 val4 val5 没有很大的不同。



但由于颜色有些他们是不可区分的。有人可以告诉我如何选择更好的颜色,以便它们是可以区分的?



谢谢。

解决方案

感谢@zelite和@ SimonO101的帮助。这是您提出的两个简单的版本。

$ b pre $ 库(ggplot2)
库(reshape2)
库(RColorBrewer)

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

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

ggplot(meltdf,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,
v al7 = 12:14)

print(PropBarPlot(df))

谢谢。


I am using ggplot to plot Proportional Stacked Bar plot. And the Plot I am getting is something like this:

And this is the self written function I am using:

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

Here val4 and val5 are not very different.

But due to colors some of them are not distinguishable. Can someone tell me how to choose better colors so that they are differentiable?

Thanks.

解决方案

Thanks @zelite and @SimonO101 for your help. This is simpler version of what both of you proposed. Adding here for the completeness.

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

Thanks.

这篇关于R:ggplot更好的渐变色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆