如何将 Boxplot 上的数据点与 R 中的线条连接起来?
在本文中,我们将讨论如何使用 R 编程语言在 ggplot2 中连接箱线图中的配对点。
带有数据点的箱线图帮助我们可视化分布之间的摘要信息。例如,我们可能有对应于两个不同类别的两个定量变量,并希望通过线连接这些数据点。所以要做到这一点,我们必须加载 tidyverse 库
句法:
library(tidyverse)
示例 1:使用连接成对点的线创建箱线图
这是一个基本的箱线图,其中线连接成对点。
R
# load tidyverse
library(tidyverse)
# create dataframe
sample_data < - data.frame(value=c(1, 2, 3, 4, 4, 5, 6,
7, 9, 11, 1.5, 2.3, 2.5, 3.4,
4.5, 5.5, 6.5, 7.5, 9.5, 12.5),
category=c('A', 'B', 'A', 'B', 'A',
'B', 'A', 'B', 'A', 'B',
'A', 'B', 'A', 'B', 'A',
'B', 'A', 'B', 'A', 'B'),
paired=c(0, 0, 1, 1, 2, 2, 3, 3, 4, 4,
5, 5, 6, 6, 7, 7, 8, 8, 9, 9))
# create plot using ggplot() and geom_boxplot() functions
ggplot(sample_data, aes(category, value, fill=category)) +
geom_boxplot()+
# geom_point() is used to make points at data values
geom_point()+
# geom_line() joins the paired datapoints
geom_line(aes(group=paired))
R
# load tidyverse
library(tidyverse)
# create dataframe
sample_data <- data.frame( value = c(1,2,3,4,4,5,6,
7,9,11,1.5,2.3,2.5,3.4,
4.5,5.5,6.5,7.5,9.5,12.5),
category = c('A','B','A','B','A',
'B','A','B','A','B',
'A','B','A','B','A',
'B','A','B','A','B'),
paired = c(0,0,1,1,2,2,3,3,4,4,
5,5,6,6,7,7,8,8,9,9))
# create plot using ggplot() and geom_boxplot() functions
ggplot(sample_data, aes(category,value, fill=category)) +
geom_boxplot()+
# geom_line() joins the paired datapoints
# color and size parameters are used to customize line
geom_line(aes(group = paired), size=2, color='gray', alpha=0.6)+
# geom_point() is used to make points at data values
# fill and size parameters are used to customize point
geom_point(aes(fill=category,group=paired),size=5,shape=21)
R
# load tidyverse
library(tidyverse)
# create dataframe
sample_data <- data.frame( value = c(1,2,3,4,4,5,6,
7,9,11,1.5,2.3,2.5,3.4,
4.5,5.5,6.5,7.5,9.5,12.5),
category = c('A','B','A','B','A',
'B','A','B','A','B',
'A','B','A','B','A',
'B','A','B','A','B'),
paired = c(0,0,1,1,2,2,3,3,4,4,
5,5,6,6,7,7,8,8,9,9))
# create plot using ggplot() and geom_boxplot() function
ggplot(sample_data, aes(category,value, fill=category)) +
geom_boxplot()+
# linetype parameter is used to customize the joining line
geom_line(aes(group = paired), linetype=2, size=1.3)+
# geom_point() is used to plot data points on boxplot
geom_point(aes(fill=category,group=paired),size=5,shape=21)
输出:
颜色定制:
我们可以使用 geom_line() 的 color 和 size 属性来更改连接段的颜色和宽度。
句法:
ggplot(aes( x, y )) + geom_boxplot()+geom_point( aes( fill ), size, shape ) + geom_line( aes( group ), size, color, alpha )
参数:
- 组:具有要加入的对的变量。
- 颜色:对点进行分类的变量
- alpha:决定透明度
- size:决定大小
- 形状:确定形状
- 填充:确定填充颜色
例子:
电阻
# load tidyverse
library(tidyverse)
# create dataframe
sample_data <- data.frame( value = c(1,2,3,4,4,5,6,
7,9,11,1.5,2.3,2.5,3.4,
4.5,5.5,6.5,7.5,9.5,12.5),
category = c('A','B','A','B','A',
'B','A','B','A','B',
'A','B','A','B','A',
'B','A','B','A','B'),
paired = c(0,0,1,1,2,2,3,3,4,4,
5,5,6,6,7,7,8,8,9,9))
# create plot using ggplot() and geom_boxplot() functions
ggplot(sample_data, aes(category,value, fill=category)) +
geom_boxplot()+
# geom_line() joins the paired datapoints
# color and size parameters are used to customize line
geom_line(aes(group = paired), size=2, color='gray', alpha=0.6)+
# geom_point() is used to make points at data values
# fill and size parameters are used to customize point
geom_point(aes(fill=category,group=paired),size=5,shape=21)
输出:
线路定制
我们可以使用 linetype 属性将连接线转换为脊线。适用于线图的每个属性也适用于这条线。
句法:
ggplot(aes( x, y )) + geom_boxplot()+geom_point() + geom_line( aes( group ), linetype )
参数
- 组:具有要加入的对的变量。
- linetype:确定线的类型
例子:
电阻
# load tidyverse
library(tidyverse)
# create dataframe
sample_data <- data.frame( value = c(1,2,3,4,4,5,6,
7,9,11,1.5,2.3,2.5,3.4,
4.5,5.5,6.5,7.5,9.5,12.5),
category = c('A','B','A','B','A',
'B','A','B','A','B',
'A','B','A','B','A',
'B','A','B','A','B'),
paired = c(0,0,1,1,2,2,3,3,4,4,
5,5,6,6,7,7,8,8,9,9))
# create plot using ggplot() and geom_boxplot() function
ggplot(sample_data, aes(category,value, fill=category)) +
geom_boxplot()+
# linetype parameter is used to customize the joining line
geom_line(aes(group = paired), linetype=2, size=1.3)+
# geom_point() is used to plot data points on boxplot
geom_point(aes(fill=category,group=paired),size=5,shape=21)
输出: