我在从R中的矩阵构造直方图时遇到了麻烦 该基质包含3种处理(对于4个群体rec1,rec2,rec3,con1,lamda0.001,lambda0.002,lambda0.005)。矩阵是:
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基本),这无关紧要。 非常感谢!
答案 0 :(得分: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)