如何在 R 中使用 ggplot2 创建更宽线条的折线图?


可以通过使用 ggplot2 的 geom_line 美学中的 size 参数来增加折线图的线条宽度。例如,如果我们有一个数据框 df,它包含两个数值列 x 和 y,并且我们想要在两者之间创建一个线条更宽的折线图,则可以按以下方式进行操作 −

ggplot(df)+geom_line(aes(x,y,size=2))

示例

考虑以下数据框 −

 在线演示

x<-rnorm(20,1,0.38)
y<-rnorm(20,5,1.47)
df<-data.frame(x,y)
df

输出

      x       y
1 1.0049532 4.790329
2 1.2701198 6.013440
3 0.6092557 5.308750
4 0.3055946 5.450709
5 1.1332979 3.970237
6 1.0800608 7.902160
7 0.8698150 5.691266
8 1.1752127 3.384137
9 0.6616497 2.724296
10 0.9367864 2.639873
11 1.5382767 4.217684
12 1.4085143 3.368657
13 1.2959630 4.251973
14 1.2404002 9.024287
15 0.8245613 4.157773
16 1.3915653 4.271487
17 1.3249538 6.792369
18 0.9989782 4.639817
19 1.7328304 5.421390
20 0.4621207 3.987451

加载 ggplot2 包并在 x 和 y 之间创建折线图 −

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

示例

> library(ggplot2)
> ggplot(df)+geom_line(aes(x,y))

输出

创建 x 和 y 之间线条更宽的折线图 −

示例

ggplot(df)+geom_line(aes(x,y,size=2))

输出

更新于: 07-Dec-2020

97 次查看

开启你的职业生涯

通过完成课程获得认证

开始
广告