以有效的方式分层绘制选定的点?

时间:2017-09-07 10:11:24

标签: r ggplot2

问:是否有任何有效的方法仅在所有剩余点之后绘制选定点?

用于绘图的数据生成为:

# Loading required libraries
library(ggplot2)
library(forcats)
library(tidyverse)
library(RColorBrewer)

# Data generation process
df <- data.frame(X=rnorm(10000,0,1), Y=rnorm(10000,0,1), 
                 ID=paste(rep("ID", 10000), 1:10000, sep="_"),
                 Type=rep("ID",10000),
                 Val=c(rep(c('Type1','Type2'),3000),
                       rep(c('Type3','Type4'),2000)))

dat1 <- data.frame(Type=rep('CT',80),
                   Val=paste(rep("CT", 80), 
                             sample(1:6,80,replace=T), sep="_"))
dat1 <- cbind(df[sample(1:100,80),1:3],dat1)

dat2 <- data.frame(Type=rep('D',80),
                   Val=paste(rep("D", 80), 
                             sample(1:6,80,replace=T), sep="_"))
dat2 <- cbind(df[sample(1:100,80),1:3],dat2)

dat3 <- data.frame(Type=rep('OP',80),
                   Val=paste(rep("OP", 80), 
                             sample(1:6,80,replace=T), sep="_"))
dat3 <- cbind(df[sample(1:100,80),1:3],dat3)

# Final data
df <- rbind(df, dat1, dat2, dat3)

将选定的值valsToKeep绘制为彩色,其余值绘制为空心圆。

valsToKeep <- c("D_1","D_4")
n <- length(valsToKeep)
getPaletteDark = brewer.pal(8,"Dark2")
colSel <- sample(getPaletteDark,n)

ggplot(df %>% 
         mutate(Val=fct_other(Val,keep=valsToKeep)) %>% 
         arrange(Val) %>% 
         filter(!duplicated(.[,c("X","Y")])), 
       aes(X,Y,col=Val,alpha=Val,shape=Val)) + 
  geom_point(alpha=1) +
  scale_colour_manual(values=c(colSel,"grey70")) +
  scale_alpha_manual(values = c(rep(1,n),0.2)) +
  scale_shape_manual(values = c(rep(16,n),1)) +
  theme_bw() 

enter image description here

但是,很难找到所选的值valsToKeep。它们被Other点掩盖。要查找选定的值valsToKeep,点数的大小会增加为suggested

ggplot(df %>% 
         mutate(Val=fct_other(Val,keep=valsToKeep)) %>% 
         arrange(Val) %>% 
         filter(!duplicated(.[,c("X","Y")])), 
       aes(X,Y,col=Val,alpha=Val,shape=Val,size=Val)) + 
  geom_point(alpha=1) +
  scale_colour_manual(values=c(colSel,"grey70")) +
  scale_alpha_manual(values = c(rep(1,n),0.2)) +
  scale_shape_manual(values = c(rep(16,n),1)) +
  scale_size_manual(values = c(rep(6,n),1)) +
  theme_bw() 

enter image description here

选定的点仍被Other掩盖,难以识别背后的模式。或者,尝试将图形绘制在多个图层中,如下所示:

dfPlot <- df %>% mutate(Val=fct_other(Val,keep=valsToKeep)) %>% 
  arrange(Val) %>% filter(!duplicated(.[,c("X","Y")]))

p1 <- ggplot(dfPlot %>% filter(Val=='Other'), aes(X,Y)) + 
  geom_point(alpha=0.2,col="grey70",shape=1) + theme_bw() 

p1 + geom_point(data=dfPlot %>% filter(!Val=='Other'),aes(X,Y,col=Val)) + 
  scale_colour_manual(values=colSel)

enter image description here

现在,情节看起来更好,更容易识别所选点的模式。

问题是:在绘制其他点(作为第一层)之后,是否还有其他有效方法可以绘制所选点(作为第二层)?

更新:

在这里,我已经展示了如何在已存在的第一层(使用geom_point)之上绘制第二层中的选定点(使用ggplot() + geom_point)。我正在寻找可用的替代方法吗?

在这种情况下,为类似question提出的解决方案是不同的。在此方案中,orderalpha的使用无效。那么,还有其他选择吗?

1 个答案:

答案 0 :(得分:0)

手动控制alpha和颜色值

# Your data
df <- rbind(df, dat1, dat2, dat3)

Lvls <- sort(unique(df$Val))
Lngth <- length(Lvls)
valstokeep <- c("D_2", "D_4")

# color levels
colormap <- rep("grey", Lngth)
colormap[Lvls %in% valstokeep] <- c("red","blue")

# alpha levels
alphamap <- rep(0.01, Lngth)   # change to lower or higher alpha levels
alphamap[Lvls %in% valstokeep] <- c(1,1)

ggplot(data=df, aes(x=X, y=Y, alpha=Val, color=Val, fill=Val)) +
  geom_point() +
  scale_alpha_manual(values=alphamap) +
  scale_color_manual(values=colormap) + 
  scale_fill_manual(values=colormap) +
  theme_classic()