如何绘制矩阵中每行2列之间的最小值?

时间:2013-06-20 14:48:31

标签: r plot histogram minimum

我有下表:

[1,]    434    359
[2,]   8012   8217
[3,]   1254   1360
[4,]     39    112
[5,]   4322   4199
[6,]    595   2737
[7,]  12984  13112
[8,]   5597   4287

我想绘制每行最小值的直方图。我知道 R 中的hist()函数,但我不知道如何只绘制2列之间的最小值。

另外,我尝试安装ggp​​lot2但它没有用,所以这对我来说不是一个选择。

3 个答案:

答案 0 :(得分:3)

?pmin

hist(pmin(x[,1], x[,2]))

答案 1 :(得分:1)

some.table <- cbind(c(434,8012,1254,39,4322,595,12984,5597),c(359,8217,1360,112,4199,2737,13112,4287))
hist(apply(some.table,1,min),breaks=10)

答案 2 :(得分:1)

如果要绘制每行的最小值,则不需要使用hist而只需要使用条形图。你有两个解决方案,标准解决方案和ggplot解决方案。

标准的:

df <- data.frame(v1 = c(434,8012,1254,39,4322,595,12984,5597), v2 = c(359,8217,1360,112,4199,2737,13112,4287))
barplot(apply(df,1,min))

ggplot one:

library("ggplot2")
df$min <- apply(df, 1, min)
ggplot(data = df, aes(x = 1:8, y = min)) + geom_bar(stat="identity")

apply()函数将统计信息应用于数据帧的行或列。