如何创建 R 中的阶梯图?


可以使用 ggplot2 包的 geom_tile 函数创建简单的阶梯图。我们只需要使用向量或我们想要在 x 和 y 的位置创建阶梯图的列。例如,如果我们有一个 R 数据框 df 的 x 列,那么可以按以下方式创建阶梯图:ggplot(df,aes(x,x))+geom_tile()。

示例

考虑以下数据框

在线演示

> x<-1:10
> df<-data.frame(x)
> df

输出

x
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10

加载 ggplot2 包并创建阶梯图

> library(ggplot2)
> ggplot(df,aes(x,x))+geom_tile()

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

输出

使用 x 的 log 函数创建阶梯图

> ggplot(df,aes(x,log(x)))+geom_tile()

输出

更新于: 07-11-2020

525 次浏览

职业启动

通过完成课程取得认证

开始
广告