【问题标题】:How to make a histogram from a matrix in R如何从R中的矩阵制作直方图
【发布时间】:2015-11-03 14:48:53
【问题描述】:

我在从 R 中的矩阵构造直方图时遇到问题 该矩阵包含 3 个处理(lamda0.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 基本)并不重要。 非常感谢!

【问题讨论】:

  • 我不认为您要创建的实际上是直方图(在 y 轴上显示频率计数)。也许你的意思是条形图?
  • 你明白了。我的新手错误我看不到!我从这里得到它,谢谢! :)

标签: r matrix ggplot2 histogram


【解决方案1】:

同意 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)

【讨论】:

    猜你喜欢
    • 2014-05-17
    • 1970-01-01
    • 2016-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-22
    相关资源
    最近更新 更多