0

Q: Is there any efficient way to plot selected points only after all the remaining points?

Data used for plotting is generated as:

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

Plotting the selected values valsToKeep as colored and the rest as hollow circles.

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

However, it is very difficult to find the selected values valsToKeep. They are masked by Other points. To find the selected values valsToKeep, the size of the points are increased as suggested here:

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

Still the selected points are masked by Other and difficult to identify the patterns behind. Alternatively, tried to plot the plots in multiple layers as below:

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

Now, the plot looks better and easy to identify the patterns of the selected points.

The question is: Is there any other efficient way to plot the selected points (as a second layer) only after plotting the Other points (as a first layer)?

Update:

Here, I have already shown how to plot the selected points in the second layer(using geom_point) on top of already existing first layer (using ggplot() + geom_point). I am looking for alternate approaches available to do that?

The solutions proposed for a similar question are different in this case. Usage of order or alpha do not help in this scenario. So, any alternatives?

Prradep
  • 5,506
  • 5
  • 43
  • 84
  • Possible duplicate of [controlling order of points in ggplot2 in R?](https://stackoverflow.com/questions/15706281/controlling-order-of-points-in-ggplot2-in-r) – Z.Lin Sep 07 '17 at 10:19

1 Answers1

0

Control the alpha and color values manually

# 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()
CPak
  • 13,260
  • 3
  • 30
  • 48