使用 ggplot2 更改图形 X 轴线的颜色。

要使用 ggplot2 更改图形的 X 轴线颜色,我们可以使用主题函数,我们可以使用 element_line 将 axis.line.x.bottom 参数颜色设置为所需的颜色。

查看下面的示例以了解如何完成。当我们想要为查看者突出显示 X 轴时,可能需要这样做。

示例

以下代码段创建了一个示例数据框 -

x<-sample(0:9,20,replace=TRUE)
y<-sample(0:9,20,replace=TRUE)
df<-data.frame(x,y)
df

创建以下数据框

   x y
1  4 5
2  5 0
3  5 3
4  7 4
5  1 9
6  0 6
7  6 0
8  9 7
9  6 5
10 5 3
11 2 8
12 3 2
13 5 1
14 1 2
15 8 2
16 6 5
17 5 2
18 1 2
19 9 4
20 9 2

要创建 ggplot2 包并在上面创建的数据框中创建 x 和 y 之间的散点图,请将以下代码添加到上面的代码片段中 -

x<-sample(0:9,20,replace=TRUE)
y<-sample(0:9,20,replace=TRUE)
df<-data.frame(x,y)
library(ggplot2)
ggplot(df,aes(x,y))+geom_point()
输出结果

如果您将上述所有片段作为单个程序执行,它会生成以下输出 -

要在上面创建的数据框中使用蓝色 X 轴线在 x 和 y 之间创建散点图,请将以下代码添加到上面的代码段 -

x<-sample(0:9,20,replace=TRUE)
y<-sample(0:9,20,replace=TRUE)
df<-data.frame(x,y)
library(ggplot2)
ggplot(df,aes(x,y))+geom_point()+theme(axis.line.x.bottom=element_line(color="blue"))
输出结果

如果您将上述所有片段作为单个程序执行,它会生成以下输出 -

猜你喜欢