简体   繁体   English

如何在y轴上绘制每行的最小值和最大值?

[英]How to plot the min and max of each row in y-axis?

I'm new and learning R and got a problem plotting the min and max value of a matrix. 我是新手,正在学习R,但在绘制矩阵的最小值和最大值时遇到问题。 The matrix is something like this: 矩阵是这样的:

X Y1 Y2 Y3 Y4 Y5

1 0.5 0.6 0.3 0.3 0.2

2 0.3 0.4 0.1 0.7 0.4

3 0.4 0.3 0.5 0.6 0.3

Now I would like to plot the first column(X) as x-axis, and pick out the min and max values of each row (eg X=1 , Ymin=0.2 , Ymax=0.6 in the first row), and plot them as the y-axis. 现在,我想将第一列(X)绘制为x轴,并挑选出每一行的最小值和最大值(例如,第一行中的X=1Ymin=0.2Ymax=0.6 ),并绘制它们作为y轴。

Could someone help me to figure it out? 有人可以帮我弄清楚吗?

A solution with rbind and 2 apply functions (for min and max) (surely not the best tho) : 具有rbind和2个应用函数的函数(最小和最大)(肯定不是最好的方法):

mat <- as.matrix(read.table(header = T, text = "X Y1 Y2 Y3 Y4 Y5

1 0.5 0.6 0.3 0.3 0.2

2 0.3 0.4 0.1 0.7 0.4

3 0.4 0.3 0.5 0.6 0.3"))

mat2 <- t(rbind(X = mat[ ,1], Ymin = apply(mat[ ,-1], 1, min), Ymax = apply(mat[ ,-1], 1, max)))

matplot(mat2[ ,1], mat2[ ,-1], pch = 20, cex = 1.5)

For example using pmin and pmax : 例如,使用pminpmax

mn = Reduce(pmin,as.list(dat[,-1]))
mx = Reduce(pmax,as.list(dat[,-1]))
library(lattice)
xyplot(mn+mx~x,data.frame(x= dat[,1],mn=mn,mx=mx),
   type='l',auto.key=T,
   ylab=list(label="max and min"))

在此处输入图片说明

Where dat is : dat在哪里:

dat <- 
  read.table(text='
X Y1 Y2 Y3 Y4 Y5
1 0.5 0.6 0.3 0.3 0.2
2 0.3 0.4 0.1 0.7 0.4
3 0.4 0.3 0.5 0.6 0.3',header=TRUE)

Here is one possibility, considering you want a scatterplot. 考虑到您需要散点图,这是一种可能性。

#reading your data
table =  read.table(header=TRUE, text="
             X Y1 Y2 Y3 Y4 Y5
             1 0.5 0.6 0.3 0.3 0.2
             2 0.3 0.4 0.1 0.7 0.4
             3 0.4 0.3 0.5 0.6 0.3", sep= " ")

#using a for loop to filter only data to be used in the plot (X, Min_Y, Max_Y)
df = data.frame(X=NA,min_Y=NA,max_Y=NA)

for (i in c(1:length(df))) {

X = table[i,1] #X values from table
min_Y = c(min(table[i,c(2:6)])) #minimum values inside table columns 2 to 6
max_Y = c(max(table[i,c(2:6)])) #maximum values inside table columns 2 to 6

df = rbind(df,c(X,min_Y,max_Y)) #new df with X, Min_Y, Max_Y

}

df = df[-1,]
df #df results

  X min_Y max_Y
2 1   0.2   0.6
3 2   0.1   0.7
4 3   0.3   0.6

#produce scatterplot with R package ggplot2
library(ggplot2)

ggplot(df) + 
  geom_point(aes(x=X,y=min_Y),colour="red") + 
  geom_point(aes(x=X,y=max_Y),colour="blue") +
  ylab("Y") +
  theme_bw()

在此处输入图片说明

So here is (another...) way to get the column-wise min and max (using m as your matrix). 因此,这是(另一种...)获取列级最小值和最大值(使用m作为矩阵)的方法。

z <- t(apply(m,1,
             function(x)return(c(x[1],min=min(x[2:length(x)]),max=max(x[2:length(x)])))))
z <- data.frame(z)
z
#      X min max
# [1,] 1 0.2 0.6
# [2,] 2 0.1 0.7
# [3,] 3 0.3 0.6

From here, plotting is straightforward. 从这里开始,绘制很简单。

plot(z$X, z$max, ylim=c(min(z$min),max(z$max)),col="blue")
points(z$X, z$min, col="red")

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

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