如何从R中的矩阵制作直方图

How to make a histogram from a matrix in R

我在从 R 中的矩阵构造直方图时遇到问题 该矩阵包含 3 个处理(lambda0.001、lambda0.002、lambda0.005,用于 4 个群体 rec1、rec2、rec3、con1)。矩阵是:

     lambda0.001 lambda0.002 lambda.003
rec1   1.0881688   1.1890554  1.3653264
rec2   1.0119031   1.0687678  1.1751051
rec3   0.9540271   0.9540271  0.9540271
con1   0.8053506   0.8086985  0.8272758

我的目标是绘制一个直方图,Y 轴为 lambda,X 轴为四组,每组三种处理。这四个小组应该彼此分开一小段时间。 我需要帮助,在 ggplot2 中是否只是常规绘图(R basic)并不重要。 非常感谢!

同意 docendo discimus 的观点,也许条形图就是您要找的。根据您的要求,尽管我会重塑您的数据以使其更容易使用,但您仍然可以使用 stat = "identity"

完成它
sapply(c("dplyr", "ggplot2"), require, character.only = T)

#  convert from matrix to data frame and preserve row names as column
b <- data.frame(population = row.names(b), as.data.frame(b), row.names = NULL)

# gather so in a tidy format for ease of use in ggplot2
b <- gather(as.data.frame(b), lambda, value, -1)

# plot 1 as described in question
ggplot(b, aes(x = population, y = value)) + geom_histogram(aes(fill = lambda), stat = "identity", position = "dodge")        

# plot 2 using facets to separate as an alternative
ggplot(b, aes(x = population, y = value)) + geom_histogram(stat = "identity") + facet_grid(. ~ lambda)