如何使使用ggplot2在R中创建的图中所有文本大小相同?
为了使使用ggplot2在R中创建的图中所有文本大小相同,我们可以按照以下步骤操作:
- 首先,创建一个数据框。
- 然后,使用ggplot2创建图表。
- 之后,使用theme函数创建相同的图表,并使用text参数更改文本大小。
创建数据框
让我们创建一个如下所示的数据框:
x<-sample(1:50,25) y<-sample(1:50,25) df<-data.frame(x,y) df
执行上述脚本后,将生成以下输出(由于随机化,此输出将在您的系统上有所不同):
x y 1 25 50 2 46 28 3 29 10 4 16 8 5 37 45 6 41 6 7 27 40 8 28 34 9 33 4 10 10 21 11 40 7 12 36 42 13 18 33 14 11 23 15 5 19 16 24 24 17 23 12 18 48 22 19 7 39 20 4 49 21 19 43 22 15 5 23 2 36 24 44 20 25 34 15
使用ggplot2创建图形
使用geom_point在x和y之间创建散点图:
x<-sample(1:50,25) y<-sample(1:50,25) df<-data.frame(x,y) library(ggplot2) ggplot(df,aes(x,y))+geom_point()
输出
Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.
创建具有相同文本大小的上述图形
将theme函数添加到上述脚本中,并将图中显示的所有文本的文本大小设置为20:
x<-sample(1:50,25) y<-sample(1:50,25) df<-data.frame(x,y) library(ggplot2) ggplot(df,aes(x,y))+geom_point()+theme(text=element_text(size=20))
输出
广告