以有效的方式分层绘制选定的点? [英] Layered plotting of selected points in an efficient way?

查看:115
本文介绍了以有效的方式分层绘制选定的点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



用于绘图的数据生成为:

 #加载所需的库
库(ggplot2)
库(forcats)
库(tidyverse)
数据库(RColorBrewer)

#数据生成过程
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)

$最终数据
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()

这里:

$ pre $ g $ p $ 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()



仍然所选的点被其他和难以识别的模式后。或者,试图在多个图层中绘制如下图:

  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)



现在,情节看起来更好,并且易于识别选定点的模式。



现在的问题是:是否有其他有效的方法来绘制其他点(作为第一层)



更新:

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



为类似的问题在这种情况下是不同的。在这种情况下,订单 alpha 的用法不起作用。所以,任何替代方案?

解决方案

手动控制alpha和color值

< pre $ #您的数据
df < - rbind(df,dat1,dat2,dat3)

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

#颜色等级
色彩映射< ; - rep(gray,Lngth)
colormap [Lvls%in%valstokeep]< - c(red,blue)

#alpha level
alphamap< - rep(0.01,Lngth)#变为较低或较高的alpha水平
alphamap [%valstokeep中的水平%] <-c(1,1)

ggplot = 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()


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?

解决方案

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

这篇关于以有效的方式分层绘制选定的点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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