如何在 R 中在宽度更大的 ggplot2 图形中创建水平线?
要在 R 的 ggplot2 图形中创建宽度更大的水平线,我们可以按照以下步骤进行操作
- 首先,创建一个数据框。
- 然后,使用 ggplot2 创建一个绘图。
- 之后,使用 geom_hline 函数创建一个相同的绘图,并使用 yintercept 来定义水平线,使用 size 参数来定义宽度
创建数据框
让我们创建一个如下所示的数据框 −
> x<-sample(1:50,20) > y<-sample(1:100,20) > df<-data.frame(x,y) > df
执行上述脚本后,会生成以下输出(由于随机化,此输出在您的系统上可能会有所不同) −
x y 1 1 43 2 21 17 3 36 9 4 43 40 5 8 19 6 34 75 7 23 29 8 44 84 9 33 8 10 24 87 11 45 20 12 17 86 13 9 60 14 50 35 15 46 3 16 49 14 17 47 18 18 19 1 19 25 16 20 32 5
使用 ggplot2 创建绘图
让我们在 x 和 y 之间创建一个散点图 −
> x<-sample(1:50,20) > y<-sample(1:100,20) > df<-data.frame(x,y) > library(ggplot2) > ggplot(df,aes(x,y))+geom_point()
输出

创建宽度更大的水平线绘图
使用 geom_hline 创建上述绘图中的水平线,yintercept = 60 且 size = 2 −
> x<-sample(1:50,20) > y<-sample(1:100,20) > df<-data.frame(x,y) > library(ggplot2) > ggplot(df,aes(x,y))+geom_point()+geom_hline(yintercept=60,size=2)
输出

广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP