如何在R中使用ggplot2创建没有灰色背景的散点图?


为了在R中使用ggplot2创建没有灰色背景的散点图,我们可以按照以下步骤操作:

  • 首先,创建一个数据框。
  • 然后,使用ggplot2包的geom_point函数创建散点图。
  • 之后,将theme_minimal()函数添加到ggplot函数中。

创建数据框

让我们创建一个如下所示的数据框:

 实时演示

x<-rnorm(25)
y<-rnorm(25)
df<-data.frame(x,y)
df

执行上述脚本后,会生成以下输出(由于随机化,此输出在您的系统上可能会有所不同):

        x         y
1  -0.12641811  1.95757659
2  -1.38633565  0.77338055
3  -1.22762385 -0.44237134
4  -0.64563725  0.89688293
5   0.26524186 -0.70475151
6   0.17533808 -0.94449945
7  -0.49610310  0.39499452
8   0.63024372 -2.10619250
9  -0.26097072  0.72478045
10 -0.25907688  0.06150708
11  0.43858635  0.98054086
12 -0.47139236  1.27132622
13 -1.30498718 -0.20353174
14  0.15307884 -2.02789784
15  0.30174693 -1.15737060
16 -0.79224642 -1.20908515
17 -0.69133131 -1.07662121
18  0.84688970  0.47372022
19 -0.71300947 -0.42851191
20  0.04015489  0.41890481
21 -0.19846102  0.86615792
22 -0.37124762  0.16788345
23 -0.70334925  1.74406602
24  0.66638451  0.55980663
25  0.78728771  0.49338160

创建散点图

加载ggplot2包并在x和y之间创建散点图:

x<-rnorm(25)
y<-rnorm(25)
df<-data.frame(x,y)
library(ggplot2)
ggplot(df,aes(x,y))+geom_point()

输出

创建没有灰色背景的散点图

使用theme_minimal()创建没有灰色背景的散点图:

x<-rnorm(25)
y<-rnorm(25)
df<-data.frame(x,y)
library(ggplot2)
ggplot(df,aes(x,y))+geom_point()+theme_minimal()

输出

更新于: 2021年8月14日

193 次查看

启动你的职业生涯

通过完成课程获得认证

开始
广告

© . All rights reserved.