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