繁体   English   中英

有没有办法让我在一个图表上简单地 plot 所有这些向量?

[英]Is there a way for me to simply plot all of these vectors on a single graph?

这些是需要全部绘制在同一张图上的向量

我想 plot 将所有这些向量放在一组中。 我见过使用矩阵的方法,但我无法理解如何将其组织为矩阵,我也更愿意使用向量。 有没有一种方法可以让我把这些都放在一张图上?

x_axis <- c(0, 1, 2, 3, 4, 7)
mouse_r_veh <- c(6, 7, 5, 2, 3, 7)
mouse_r_cap <- c(27, 22, 21, 25, 21, 25)
mouse_rr_veh <- c(7, 3, 4, 6, 4, 17)
mouse_rr_cap <- c(24, 27, 29, 9, 10, 21)
mouse_l_veh <- c(10, 12, 11, 16, 13, 2)
mouse_l_cap <- c(26, 23, 23, 23, 24, 22)
mouse_ll_veh <- c(0, 2, 1, 3, 0, 0)

您可以将数据放入data.frame并使用pivot_longer创建一个带有每个系列名称的新变量:

library(tidyr)
library(ggplot2)

df <- data.frame(x_axis,      
                 mouse_r_veh, 
                 mouse_r_cap, 
                 mouse_rr_veh,
                 mouse_rr_cap,
                 mouse_l_veh, 
                 mouse_l_cap, 
                 mouse_ll_veh)

data <- df %>% pivot_longer(cols = contains('mouse'))

ggplot(data) + geom_line(aes(x = x_axis, y = value, color = name))

在此处输入图像描述

如果你不想使用 matplot,也不想使用 ggplot,你可以只做一个plot调用和几lines

plot(x_axis, ylim = c(0, 30))
lines(mouse_r_cap, col="red")
lines(mouse_r_veh, col = "green")
# ... et cetera

如果您不介意将 matplot 与矩阵一起使用,您可以这样做:

mx <- cbind(x_axis, mouse_r_veh, mouse_r_cap, 
        mouse_rr_veh, mouse_rr_cap, mouse_l_veh, 
        mouse_l_cap, mouse_ll_veh)
matplot(mx, type ="l")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM