【问题标题】:Print histograms next to each other in R在R中彼此相邻打印直方图
【发布时间】:2015-02-25 10:17:05
【问题描述】:

我正在使用 R 和包“ggplot2”。我正在尝试打印几个相邻的直方图:

library(ggplot2) h=ggplot(data, aes(x=Return1)) h+ geom_histogram(binwidth = 0.1) h + facet_grid(Return1 + Return2)

它发送错误“绘图中没有图层”。

这里是数据示例:

data=matrix(rnorm(50, 0, 1), ncol=5) colnames(data)=c("Return1", "Return2", "Return3", "Return4", "Return5") rownames(data)=c("1995", "1996", "1997", "1998", "1999", "2000", "2001", "2002", "2003", "2004")

目的是得到一个直方图,其中包含我的第一个变量“Return1”的值,另一个直方图包含我的第二个变量“Return2”的值,...

我不确定我是否使用了正确的函数 (facet_grid) 来做到这一点。 感谢您对此的帮助。

游泳

【问题讨论】:

  • 提供示例数据
  • 这里是数据示例。不过谢谢你的帮助。 data=matrix(rnorm(50, 0, 1), ncol=5) colnames(data)=c("Return1", "Return2", "Return3", "Return4", "Return5") rownames(data)=c("1995", "1996", "1997", "1998", "1999", "2000", "2001", "2002", "2003", "2004")
  • 我在下面发布了一个完整的答案!

标签: r ggplot2 histogram


【解决方案1】:
data <- structure(c(-0.447315711911355, 0.670646511357067, 0.28805337765816, 
0.210323243582978, 0.558951367403988, 0.607248748494035, -1.16412611819213, 
0.0915424491269807, 0.469191549286902, -0.619038988584179, -0.659390830669932, 
-0.924810741449363, -1.42269762267215, -0.13593956988495, -1.92882493234572, 
-1.27638233136087, 1.3816213467106, 0.365757310517179, -0.479538532782686, 
0.70769126786196, -0.326429694012458, -1.01751602957572, 0.555459246627799, 
-0.355015029145993, -0.065915904785214, 0.576685372310354, 1.08050319208107, 
-0.697995949639672, 0.661562203593662, 0.0968130184654234, -0.672212026424006, 
0.0269233940788362, 0.661236459007207, -0.507557327616088, -0.800274398837844, 
1.93302333485735, -1.28135392022731, 1.33095400120017, -0.377753506417346, 
0.700663669871313, -1.08566228220391, -1.08906574084574, -1.04577335310861, 
0.956870855865283, -2.21389083133313, 0.299118475920725, 0.523434618906021, 
0.0428254530914905, 0.443157396704438, 2.00841231202171), .Dim = c(10L, 
5L), .Dimnames = list(NULL, c("Return1", "Return2", "Return3", 
"Return4", "Return5")))

您可以使用简单的 hist 函数将所有内容组合在一起

par(mfrow=c(1,5))
hist(data[,1])
hist(data[,2])
hist(data[,3])
hist(data[,4])
hist(data[,5])

你可以使用 gplot

你需要按如下方式安装一个包

install.packages("gridExtra") 

注意: 不要忘记加载库和 添加任何需要的包或库

p1<- qplot(data[,1], binwidth=.5)
p2<- qplot(data[,2], binwidth=.5)
p3<- qplot(data[,3], binwidth=.5)
p4<- qplot(data[,4], binwidth=.5)
p5<- qplot(data[,5], binwidth=.5)
grid.arrange(p1, p2, p3, p4,p5, ncol=2)

或者你可以使用ggplot如下:

row.names (data) <- NULL
df<- as.data.frame(data)
p1<- ggplot(df, aes(df[,1])) + geom_histogram(binwidth=.5)
p2<- ggplot(df, aes(df[,2])) + geom_histogram(binwidth=.5)
p3<- ggplot(df, aes(df[,3])) + geom_histogram(binwidth=.5)
p4<- ggplot(df, aes(df[,4])) + geom_histogram(binwidth=.5)
p5<- ggplot(df, aes(df[,5])) + geom_histogram(binwidth=.5)
grid.arrange(p1, p2, p3, p4,p5, ncol=2)

【讨论】:

    【解决方案2】:

    要使用 facet_grid,您需要在包含以下内容的数据中创建一列(例如命名为返回):Return1、Return2、... 然后:h + facet_grid(.~returns) 但我认为你在 facet_grid 中使用的变量必须与 aes() 中的 x 不同

    【讨论】:

    • 感谢您的回复。这是我从网上的例子中看到的。使用虹膜数据,它使用“物种”,并根据该变量制作图表,但在我的示例中,我只是一个打印图表,就像par(mfrow=c(1,2)) #two histograms on one line 一样。
    【解决方案3】:

    您的问题很尴尬,但这是答案的开始。编辑您的问题以包含可重复的数据,扩展此答案,看看您还需要什么。

    data <- matrix(rnorm(50, 0, 1), ncol=5) 
    colnames(data) <- c("Return1", "Return2", "Return3", "Return4", "Return5")
    rownames(data) <- c("1995", "1996", "1997", "1998", "1999", "2000", "2001", "2002", "2003", "2004") <- 
    
    df <-  as.data.frame(data) # since ggplot2 requires a data frame
    df.m <- 
    
    ggplot(df, aes(x=Return1, y=Return2)) + #added the second variable as y
      geom_histogram(stat = "identity") + # need to set the stat
      facet_grid(Return1 ~ Return2) # note the tilde operator
    

    【讨论】:

    • 我的代码有一些错误。 Stacking not well defined when ymin !=0 Nemo 可以在下面找到我正在寻找的解决方案。也感谢您的帮助!
    猜你喜欢
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-04
    • 2014-09-03
    • 2013-04-22
    相关资源
    最近更新 更多