具有不同颜色变量和平均线的绘图

发布于 2025-02-05 01:38:56 字数 833 浏览 3 评论 0原文

我在r中有一个中的数据集[ - 1,1] 5个国家,即(a,b,b,c,d,e)代码>如下:

data <- matrix(runif(n=50, min = -1, max = 1),ncol = 5)
data <- round(data,2)
colnames(data) <-
  c("A", "B", "C", "D", "E")

然后,我计算所有国家/地区的每行的平均值:

aver <- as.matrix(rowMeans(data))
rownames(aver) <- c("X1","X2","X3","X4","X5","X6","X7","X8","X9","X10")

我想创建一个带有每个国家/国家(列)的观测值的图,以具有不同的颜色,aver 要包括的行。我正在使用以下内容,但无法正常工作。

plot(c(data),col=c("red","yellow","green","blue","black"))
lines(aver)

最终结果必须看起来像这样,但包括彩色观测值: ”在此处输入图像描述

我该怎么做?

I have a dataset in R of values in [-1,1] for 5 countries, namely (A,B,C,D,E) as follows:

data <- matrix(runif(n=50, min = -1, max = 1),ncol = 5)
data <- round(data,2)
colnames(data) <-
  c("A", "B", "C", "D", "E")

Then I calculate the average value of each row for all countries with the following:

aver <- as.matrix(rowMeans(data))
rownames(aver) <- c("X1","X2","X3","X4","X5","X6","X7","X8","X9","X10")

I want to create a plot with the observations with each country (column) to have different colors and the aver line to be included. I am using the following but can not make it properly work.

plot(c(data),col=c("red","yellow","green","blue","black"))
lines(aver)

The final result must look like this but with colored observations included:enter image description here

How can I do that??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷清清 2025-02-12 01:38:57

也许您想要这样的东西:

matplot(as.data.frame(data),col=c("red","yellow","green","blue","black"), pch=16, ylab = "")
lines(aver)

输出:

”在此处输入映像说明“

为传奇添加此信息:

legend("bottomright", legend = colnames(data)[1:5], col=c("red","yellow","green","blue","black"), pch = 16)

输出:

“在此处输入图像说明”

Maybe you want something like this:

matplot(as.data.frame(data),col=c("red","yellow","green","blue","black"), pch=16, ylab = "")
lines(aver)

Output:

enter image description here

Add this for legend:

legend("bottomright", legend = colnames(data)[1:5], col=c("red","yellow","green","blue","black"), pch = 16)

Output:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文