R语言 如何在ggplot2中用圆圈/椭圆注释群组的变量
在这篇文章中,我们将讨论如何在R编程语言中使用ggplot2包,通过分类变量对群组进行圆/椭圆注释。
为了在数据点群周围添加一个圆或椭圆,我们使用ggforce包的geom_mark_circle()和geom_mark_ellipse()函数。这个函数会自动计算出圆/椭圆的半径,以便通过分类数据在点群周围绘制。
首先,我们将使用ggplot2包的geom_point函数在散点图中绘制数据。我们将使用 aes()函数的颜色参数,按分类变量组为该图着色。
语法 。
ggplot(df, aes( x, y ) ) + geom_point( aes( color ))
参数 。
- df: 确定要使用的数据框架。
- x和y: 分别决定x轴和y轴的变量。
- color: 决定用于给数据点集群着色的分类变量。
例子 。
这里,是一个使用ggplot2包的geom_point()函数绘制的基本散点图。我们通过分类变量组对该图进行了着色。
# load library tidyverse
library(tidyverse)
# set theme
theme_set(theme_bw(16))
# create x and y vector
xAxis <- rnorm(1000)
yAxis <- rnorm(1000) + xAxis + 10
# create groups in variable using conditional
# statements
group <- rep(1, 1000)
group[xAxis > -1.5] <- 2
group[xAxis > -0.5] <- 3
group[xAxis > 0.5] <- 4
group[xAxis > 1.5] <- 5
# create sample data frame
sample_data <- data.frame(xAxis, yAxis, group)
# create a scatter plot with points colored by
# group
ggplot(sample_data, aes(x = xAxis,
y = yAxis))+
geom_point(aes(color = as.factor(group)))
输出 。
在群集周围标注圆圈
为了在群集的周围标注一个圆,我们使用ggforce包的geom_mark_circle()函数。为了使用这个函数,我们首先通过以下方式安装并导入ggforce包。
install. packages('ggforce')
library(ggforce)
现在,我们将通过使用geom_mark_circle()函数来注释一个数据点群周围的圆。
语法 。
ggplot(df, aes( x, y ))+ geom_point( aes( color ))+ geom_mark_circle( aes(color) )
例子 。
这里,是一个基本的散点图,在一簇数据点周围用圆圈标明分类变量组。
# load library tidyverse
library(tidyverse)
library(ggforce)
# set theme
theme_set(theme_bw(16))
# create x and y vector
xAxis <- rnorm(500)
yAxis <- rnorm(1000) + xAxis + 10
# create groups in variable using conditional
# statements
group <- rep(1, 500)
group[xAxis > -1.5] <- 2
group[xAxis > -0.5] <- 3
group[xAxis > 0.5] <- 4
group[xAxis > 1.5] <- 5
# create sample data frame
sample_data <- data.frame(xAxis, yAxis, group)
# create a scatter plot with points colored by group
# circles are annotated using geom_mark_circle() function
ggplot(sample_data, aes(x = xAxis,
y = yAxis))+
geom_point(aes(color = as.factor(group)))+
geom_mark_circle(aes(color = as.factor(group)), expand = unit(0.5,"mm"))+
theme(legend.position = "none")
输出 。
在群集周围标注椭圆
为了在组的点群周围标注一个椭圆,我们使用了ggforce包的geom_mark_ellipse()函数。这个函数会自动计算出椭圆的尺寸,并将其叠加在散点图的上面。
语法 。
ggplot(df, aes( x, y )+ geom_point( aes( color ))+ geom_mark_ellipse( aes(color) )
例子 。
这里,是一个基本的散点图,在一簇数据点周围有一个由分类变量组着色的椭圆形。
# load library tidyverse
library(tidyverse)
library(ggforce)
# set theme
theme_set(theme_bw(16))
# create x and y vector
xAxis <- rnorm(500)
yAxis <- rnorm(1000) + xAxis + 10
# create groups in variable using conditional
# statements
group <- rep(1, 500)
group[xAxis > -1.5] <- 2
group[xAxis > -0.5] <- 3
group[xAxis > 0.5] <- 4
group[xAxis > 1.5] <- 5
# create sample data frame
sample_data <- data.frame(xAxis, yAxis, group)
# create a scatter plot with points colored by group
# ellipses are annotated using geom_mark_ellipse() function
ggplot(sample_data, aes(x = xAxis,
y = yAxis))+
geom_point(aes(color = as.factor(group)))+
geom_mark_ellipse(aes(color = as.factor(group)), expand = unit(0.5,"mm"))+
theme(legend.position = "none")
输出 。
自定义美学
我们可以通过使用 aes() 函数的颜色、填充和 alpha 属性来定制 geom_mark_* 函数的美学效果。
语法 。
ggplot(df, aes( x, y )+ geom_point( aes( color ))+ geom_mark_ellipse( aes(color, fill, alpha) )
其中。
- color: 决定了圆或椭圆的边界颜色。
- fill: 决定圆或椭圆的背景颜色。
- alpha: 决定圆圈或椭圆的透明度。
例子 。
在这个例子中,我们将绘制一个由椭圆叠加的散点图,背景由组别分类变量着色。
# load library tidyverse
library(tidyverse)
library(ggforce)
# set theme
theme_set(theme_bw(16))
# create x and y vector
xAxis <- rnorm(500)
yAxis <- rnorm(1000) + xAxis + 10
# create groups in variable using conditional
# statements
group <- rep(1, 500)
group[xAxis > -1.5] <- 2
group[xAxis > -0.5] <- 3
group[xAxis > 0.5] <- 4
group[xAxis > 1.5] <- 5
# create sample data frame
sample_data <- data.frame(xAxis, yAxis, group)
# create a scatter plot with points colored by group
# ellipses are annotated using geom_mark_ellipse() function
ggplot(sample_data, aes(x = xAxis,
y = yAxis))+
geom_point(aes(color = as.factor(group)))+
geom_mark_ellipse(aes(fill = as.factor(group)), expand = unit(0.5,"mm"))+
theme(legend.position = "none")
输出 。