ggplot

时间:2016-09-27 21:06:09

标签: r ggplot2 colors scale

我有数据,我想使用ggplot' s geom_point绘图:

  set.seed(1)
  df <- data.frame(x=rnorm(100),y=rnorm(100),val=c(rnorm(90),rep(NA,10)))

我根据df$val

的间隔添加颜色
  intervals.df <- data.frame(interval=c("(-3,-2]","(-2,-0.999]","(-0.999,0]","(0,1.96]","(1.96,3.91]","(3.91,5.87]","not expressed"),
                             start=c(-3,-2,-0.999,0,1.96,3.91,NA),end=c(-2,-0.999,0,1.96,3.91,5.87,NA),
                             col=c("#2f3b61","#436CE8","#E0E0FF","#7d4343","#C74747","#EBCCD6","#D3D3D3"),stringsAsFactors=F)
  df <- cbind(df,do.call(rbind,lapply(df$val,function(x){
    if(is.na(x)){
      return(data.frame(col=intervals.df$col[nrow(intervals.df)],interval=intervals.df$interval[nrow(intervals.df)]))
    } else{
      idx <- which(intervals.df$start <= x & intervals.df$end >= x)
      return(data.frame(col=intervals.df$col[idx],interval=intervals.df$interval[idx]))
    }
  })))

在此我将df$col设置为factor并将标签设置为区间,以便我可以在图例中绘制它们:

  df$col <- factor(df$col,levels=intervals.df$col,labels=intervals.df$interval)

这也将显示所有间隔,包括df$val可能未覆盖的间隔,但我想要这样。

以下是我试图绘制它的方法:

library(ggplot2)
ggplot(df,aes(x=x,y=y,colour=col))+geom_point(cex=2,shape=1,stroke=1)+labs(x="X",y="Y")+theme_bw()+theme(legend.key=element_blank(),panel.border=element_blank(),strip.background=element_blank())+scale_shape(solid=T)+scale_fill_manual(drop=FALSE,values=levels(df$col),name="DE")

哪个让我接近但颜色不对: enter image description here

所以我认为这个plot命令会纠正(添加scale_color_manual):

ggplot(df,aes(x=x,y=y,colour=col))+geom_point(cex=2,shape=1,stroke=1)+labs(x="X",y="Y")+theme_bw()+theme(legend.key=element_blank(),panel.border=element_blank(),strip.background=element_blank())+scale_shape(solid=T)+scale_fill_manual(drop=FALSE,values=levels(df$col),name="DE")+scale_color_manual(drop=FALSE,values=levels(df$col),name="DE")

但是这会引发错误:

Error in grDevices::col2rgb(colour, TRUE) : invalid color name '(0,1.96]'

那么,如何才能正确显示颜色(以及图例name)?

1 个答案:

答案 0 :(得分:3)

一个选项是在通过interval设置级别后将颜色映射到intervals.df,因此级别的顺序和级别的数量是正确的。使用intervals.df中的颜色,将颜色的命名向量传递给scale_color_manual

# Set levels of interval via intervals.df
df$interval = factor(df$interval, levels=intervals.df$interval)

# Named vector of the colors based on intervals.df
colors = intervals.df$col
names(colors) = intervals.df$interval

ggplot(df, aes(x=x, y=y, colour=interval))+
    geom_point(cex=2, shape=1, stroke=1) +
    labs(x="X", y="Y")+
    theme_bw()+
    theme(legend.key=element_blank(),
         panel.border=element_blank(), strip.background=element_blank())+
    scale_color_manual(values = colors, name = "DE", drop = FALSE)