R 在ggplot散点图中以特定方式覆盖点
颜色根据两个变量的交互作用添加到ggplot散点图中:choice和flag各有两个值,因此总共有四种组合。我使用了基于z值的刻面R 在ggplot散点图中以特定方式覆盖点,r,ggplot2,R,Ggplot2,颜色根据两个变量的交互作用添加到ggplot散点图中:choice和flag各有两个值,因此总共有四种组合。我使用了基于z值的刻面 library(tidyverse) x <- runif(10000) y <- runif(10000) z <- c(rep(0, 5000), rep(1, 5000)) flag <- c(rep(0, 500), rep(1, 4500), rep(0, 4500), rep(1, 500)) choice <- rep(
library(tidyverse)
x <- runif(10000)
y <- runif(10000)
z <- c(rep(0, 5000), rep(1, 5000))
flag <- c(rep(0, 500), rep(1, 4500), rep(0, 4500), rep(1, 500))
choice <- rep(c(0, 1), 5000)
tbl <- tibble(x, y, z, flag, choice)
scatterplot <- ggplot(tbl,
aes(x = x,
y = y,
color = factor(interaction(choice, flag)))
) +
geom_point(alpha = 0.7,
size = 2) +
scale_color_manual(values = c("blue3", "cyan1", "red3", "orange")) +
facet_grid(z ~ .) +
theme_bw() +
theme(legend.position = "right") +
theme(aspect.ratio = 1) +
ggtitle("Scatter plot")
scatterplot
但我有以下要求-
z用于刻面。对于z=i,我希望flag=i的点位于上方,即下图中,
对于z=0,蓝色点标志=0应位于红色/橙色点上方
对于z=1,红色/橙色点标志=1应位于蓝色点上方,如图所示
如果我理解正确,您对下部面板很满意,但您需要将顶部面板中的蓝色圆点覆盖在橙色圆点上,此时橙色圆点覆盖在两个面板中的蓝色圆点上 如果是这种情况,则使用子集合数据帧再次调用geom_点,其中z==0&flag==0将覆盖顶部面板上的相应蓝色点,而不会影响下部面板 待定% mutatecol=交互选择,标志 ggplottbl,aesx,y,color=col+ 几何点α=0.7,尺寸=2+ geom_pointdata=subsetbl,z==0&flag==0, α=0.7,尺寸=2+ 刻度\u颜色\u手动值=cblue3、青色1、红色3、橙色+ facet_gridz~。+ 主题_bw+ themelegend.position=右+ 测量光谱比率=1+ 标题散射图
如果我理解正确,您对下部面板很满意,但您需要将顶部面板中的蓝色圆点覆盖在橙色圆点上,此时橙色圆点覆盖在两个面板中的蓝色圆点上 如果是这种情况,则使用子集合数据帧再次调用geom_点,其中z==0&flag==0将覆盖顶部面板上的相应蓝色点,而不会影响下部面板 待定% mutatecol=交互选择,标志 ggplottbl,aesx,y,color=col+ 几何点α=0.7,尺寸=2+ geom_pointdata=subsetbl,z==0&flag==0, α=0.7,尺寸=2+ 刻度\u颜色\u手动值=cblue3、青色1、红色3、橙色+ facet_gridz~。+ 主题_bw+ themelegend.position=右+ 测量光谱比率=1+ 标题散射图
将此视为您的一种选择。对于facets,设置特定顺序很复杂,但您可以使用拼图进行相同的绘图: 绘图:
#Plot
G1 <- ggplot(subset(tbl,z==0),aes(x = x,y = y,
color = factor(interaction(choice, flag),
levels = rev(unique(interaction(choice, flag))),
ordered = T))) +
geom_point(alpha = 0.7,
size = 2) +
scale_color_manual(values = c("blue3", "cyan1", "red3", "orange")) +
facet_grid(z ~ .) +
theme_bw() +
theme(legend.position = "right") +
theme(aspect.ratio = 1) +
ggtitle("Scatter plot")+
labs(color='Color',x='')+theme(legend.position = 'none')
#Plot 2
G2 <- ggplot(subset(tbl,z==1),aes(x = x,y = y,
color = factor(interaction(choice, flag)))) +
geom_point(alpha = 0.7,
size = 2) +
scale_color_manual(values = c("blue3", "cyan1", "red3", "orange")) +
facet_grid(z ~ .) +
theme_bw() +
theme(legend.position = "right") +
theme(aspect.ratio = 1) +
labs(color='Color')
最终安排:
#Final plot
G <- G1/G2
G <- G+plot_layout(guides = 'collect')
输出:
将此视为您的一种选择。对于facets,设置特定顺序很复杂,但您可以使用拼图进行相同的绘图: 绘图:
#Plot
G1 <- ggplot(subset(tbl,z==0),aes(x = x,y = y,
color = factor(interaction(choice, flag),
levels = rev(unique(interaction(choice, flag))),
ordered = T))) +
geom_point(alpha = 0.7,
size = 2) +
scale_color_manual(values = c("blue3", "cyan1", "red3", "orange")) +
facet_grid(z ~ .) +
theme_bw() +
theme(legend.position = "right") +
theme(aspect.ratio = 1) +
ggtitle("Scatter plot")+
labs(color='Color',x='')+theme(legend.position = 'none')
#Plot 2
G2 <- ggplot(subset(tbl,z==1),aes(x = x,y = y,
color = factor(interaction(choice, flag)))) +
geom_point(alpha = 0.7,
size = 2) +
scale_color_manual(values = c("blue3", "cyan1", "red3", "orange")) +
facet_grid(z ~ .) +
theme_bw() +
theme(legend.position = "right") +
theme(aspect.ratio = 1) +
labs(color='Color')
最终安排:
#Final plot
G <- G1/G2
G <- G+plot_layout(guides = 'collect')
输出: