简体   繁体   English

如何根据两个因素为每种独特的水平组合创建直方图?

[英]How to create histograms for each unique combination of levels from two factors?

I cannot figure out how to use a loop to plot one histogram for each unique combination of levels from TWO factors. 我无法弄清楚如何使用循环为两个因素的每种独特水平组合绘制一个直方图。

Here is my data: https://www.dropbox.com/sh/exsjhu23fnpwf4r/AABvitLBN1nRMpXcyYMVIOIDa?dl=0 这是我的数据: https : //www.dropbox.com/sh/exsjhu23fnpwf4r/AABvitLBN1nRMpXcyYMVIOIDa?dl=0

# perhaps need to have factors
df$freq <- as.factor(df$freq)
df$time <- as.factor(df$time)

I learned how to use a loop to plot histograms for ONE factor levels: 我学习了如何使用循环绘制一个因子水平的直方图:

# space for plots  
windows(width=19, height=10) 
par(las=1, cex.lab=0.75, cex.axis=0.6, bty="n", mgp=c(1, 0.6, 0),
    oma=c(2, 4, 2, 0) + 0.1, mar=c(4, 0, 3, 3) + 0.1)
a <- layout(matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
                     18, 19, 20, 21), nrow=3, ncol=7, byrow=T))
layout.show(a)

# loop 
for (i in 1:length(unique(df$freq))) {
  value <- subset(df, freq == unique (df$freq)[i])
  hist(value$thr, main=paste0("freq: ", unique(df$freq)[i]))
}

I tried variations of this loop for TWO factors but that unfortunately does not work: 我针对两个因素尝试了此循环的变体,但不幸的是,这不起作用:

for (i in 1:length(unique(df[c("freq", "time")]))) {
  value <- subset(df, freq == unique (df$freq)[i] & time == unique(df$time)[i])
  hist(value$thr, main=paste0("freq: ", unique(df$freq)[i]))
}

I would also like to learn how to label each histogram based on the levels of TWO factors (not just one)... 我还想学习如何基于两个因素(不仅是一个因素)的水平来标记每个直方图...

It's more convenient to use by here. 在这里使用by更方便。

For the titles we prefer characters to factors. 对于标题,我们更喜欢字符而不是因素。

df1[c("freq", "time")] <- lapply(df1[c("freq", "time")], as.character)

Then open windows, 然后打开窗户

windows(width=19, height=10) 
par(las=1, cex.lab=0.75, cex.axis=0.6, bty="n", mgp=c(1, 0.6, 0), 
    oma=c(2, 4, 2, 0) + 0.1, mar=c(4, 0, 3, 3) + 0.1)
a <- layout(matrix(1:21, 3, 7))
layout.show(a)

and plot. 和情节。

by(df1, df1[c("freq", "time")], function(x) 
  hist(x$thr, main=paste("freq:", paste(x[1, c(1, 3)], collapse=","))))

Result 结果

在此处输入图片说明

Edit 编辑

To get the specific order we probably have to do some more stuff. 为了获得特定的订单,我们可能需要做更多的事情。

df1[c("freq", "time")] <- lapply(df1[c("freq", "time")], as.character)

windows(width=19, height=10) 
par(las=1, cex.lab=0.75, cex.axis=0.6, bty="n", mgp=c(1, 0.6, 0), 
    oma=c(2, 4, 2, 0) + 0.1, mar=c(4, 0, 3, 3) + 0.1)
a <- layout(matrix(1:21, 3, 7, byrow=TRUE))   # with byrow
layout.show(a)

l <- split(df1, df1[c("freq", "time")])
m <- t(sapply(l, function(x) x[1, c(1, 3)]))  # matrix of first rows of each subset
m[, 2] <- sub("m", "", m[, 2])                # use the values...
m <- apply(m, 1:2, as.numeric)                # ... make numeric

Now we obtain the histograms within a lapply over the list ordered by m . 现在,我们获得m排序的列表上的lapply内的直方图。

lapply(l[order(m[, 2], m[, 1])], function(x)
  hist(x$thr, main=paste("freq:", paste(x[1, c(1, 3)], collapse=","))))

New Result 新结果

在此处输入图片说明

Data 数据

df1 <- structure(list(freq = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 
1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 
3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 
5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 
7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 
2L, 3L, 4L, 5L, 6L, 7L, 1L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), class = "factor"), thr = c(60L, 
25L, 20L, 15L, 15L, 30L, 35L, 60L, 25L, 10L, 15L, 15L, 30L, 35L, 
55L, 30L, 15L, 15L, 10L, 25L, 40L, 50L, 25L, 15L, 10L, 15L, 20L, 
40L, 50L, 30L, 10L, 15L, 15L, 20L, 25L, 50L, 25L, 10L, 10L, 10L, 
20L, 25L, 45L, 20L, 10L, 10L, 10L, 20L, 25L, 45L, 15L, 10L, 10L, 
10L, 20L, 30L, 60L, 30L, 10L, 10L, 10L, 15L, 30L, 50L, 25L, 10L, 
10L, 10L, 20L, 30L, 45L, 25L, 15L, 10L, 15L, 30L, 35L, 50L, 25L, 
15L, 10L, 15L, 25L, 35L, 60L, 25L, 10L, 10L, 15L, 20L, 30L, 60L, 
25L, 5L, 5L, 10L, 20L, 30L, 45L, 20L, 5L, 10L, 10L, 20L, 30L, 
45L, 20L, 10L, 10L, 10L, 20L, 30L, 60L, 30L, 15L, 10L, 15L, 25L, 
30L, 55L, 25L, 10L, 10L, 10L, 20L, 30L, 55L, 35L, 10L, 10L, 10L, 
20L, 30L, 60L, 35L, 15L, 10L, 10L, 15L, 25L, 50L, 30L, 10L, 10L, 
10L, 20L, 25L, 55L, 25L, 10L, 10L, 15L, 25L, 25L, 65L, 30L, 10L, 
10L, 15L, 20L, 30L, 60L, 30L, 15L, 15L, 15L, 15L, 30L, 55L, 35L, 
15L, 15L, 15L, 25L, 35L, 55L, 35L, 15L, 15L, 15L, 25L, 35L, 60L, 
35L, 15L, 15L, 15L, 25L, 35L, 60L, 30L, 10L, 10L, 15L, 25L, 35L, 
55L, 30L, 15L, 10L, 10L, 25L, 30L, 50L, 25L, 10L, 10L, 10L, 20L, 
30L, 55L, 30L, 10L, 10L, 15L, 20L, 30L, 55L, 30L, 10L, 15L, 20L, 
25L, 35L, 55L, 25L, 15L, 15L, 15L, 25L, 40L, 50L, 20L, 10L, 10L, 
20L, 30L, 40L, 45L, 25L, 10L, 10L, 10L, 20L, 30L, 50L, 25L, 10L, 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L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L), .Label = c("3m", "6m", "9m"), class = "factor")), row.names = c(NA, 
-322L), class = "data.frame")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM