简体   繁体   中英

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

# 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.

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 .

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, 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, 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, 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, 2L, 3L, 
4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L), .Label = c("4", 
"8", "12.5", "16", "20", "25", "31.5"), 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, 
10L, 10L, 20L, 25L, 55L, 20L, 10L, 10L, 15L, 25L, 35L, 50L, 20L, 
10L, 10L, 15L, 25L, 30L, 45L, 20L, 15L, 10L, 10L, 20L, 30L, 50L, 
20L, 15L, 15L, 15L, 20L, 30L, 60L, 35L, 15L, 10L, 15L, 25L, 30L, 
60L, 35L, 15L, 15L, 15L, 30L, 35L, 55L, 25L, 10L, 15L, 15L, 25L, 
35L, 50L, 30L, 10L, 15L, 15L, 25L, 35L, 55L, 25L, 20L, 15L, 15L, 
25L, 30L, 55L, 25L, 15L, 15L, 15L, 30L, 35L), time = structure(c(1L, 
1L, 1L, 1L, 1L, 1L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, 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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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