[英]Plot with different colour variables and average line in R
I have a dataset in R
of values in [-1,1]
for 5 countries, namely (A,B,C,D,E)
as follows:我在
R
中有一个数据集,其中包含 5 个国家的[-1,1]
值,即(A,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")
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.我想创建一个带有每个国家(列)的观察结果的图,以具有不同的颜色和要包括的
aver
线。 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:最终结果必须如下所示,但包括彩色观察:
How can I do that??我怎样才能做到这一点??
Maybe you want something like this:也许你想要这样的东西:
matplot(as.data.frame(data),col=c("red","yellow","green","blue","black"), pch=16, ylab = "")
lines(aver)
Output:输出:
Add this for legend:添加这个作为图例:
legend("bottomright", legend = colnames(data)[1:5], col=c("red","yellow","green","blue","black"), pch = 16)
Output:输出:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.