如何在 R 中以折线图的形式绘制数据框的行?
若要以折线图的形式绘制数据框的行,则可以使用 matplot 函数,但我们需要先转置数据框,因为转置的数据框值会被视为列,而 matplot 函数绘制的是列,而不是行。例如,如果我们有一个名为 df 的数据框,则可以通过使用以下命令创建以折线图形式绘制各行的图形:
matplot(t(df),type="l")
示例 1
请考虑以下数据框:
> x1<-rpois(5,2) > x2<-rpois(5,5) > x3<-rpois(5,3) > df1<-data.frame(x1,x2,x3) > df1
输出
x1 x2 x3 1 0 9 5 2 3 4 2 3 0 2 1 4 3 7 3 5 5 10 3
将 df1 中的行创建为折线图:
> matplot(t(df1),type="l")
Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.
输出
示例 2
> y1<-rnorm(3) > y2<-rnorm(3) > y3<-rnorm(3) > df2<-data.frame(y1,y2,y3) > df2
输出
y1 y2 y3 1 -0.9992381 -1.3802480 -0.7228096 2 1.3677936 0.1813761 1.3711921 3 0.8905198 -1.0607813 0.3895616 1 -0.9992381 -1.3802480 -0.7228096 2 1.3677936 0.1813761 1.3711921 3 0.8905198 -1.0607813 0.3895616
将 df2 中的行创建为折线图:
> matplot(t(df2),type="l")
输出
广告