在 R 中并排打印直方图

Print histograms next to each other in R

我正在使用 R 和包 "ggplot2"。我正在尝试打印几个并排的直方图:

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

发送错误"No layers in plot"。

这里是一个数据示例:

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) 来执行此操作。 感谢您对此的帮助。

swiMa

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

中的 x 不同

你的问题很尴尬,但这里是答案的开始。编辑您的问题以合并可重现的数据,扩展此答案,然后查看您还需要什么。

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