我从csv文件中读取数据:
| proband | stimulus | group |
|---------|----------|-------|
| 0 | A | X |
| 0 | B | Y |
| 0 | C | Z |
| 1 | A | X |
| 1 | B | Y |
| 1 | C | N |
我正在阅读csv文件,并为每个刺激绘制直方图,如pdf:
Data <- read.csv(file="./groups_data.csv", head=TRUE, sep=";")
library(lattice)
pdf("output/groups_single.pdf", width=8, height=4)
par(mar=c(1.5,0,1,2.5), xpd=TRUE)
bar_plot_single <- histogram(~ group | stimulus, data=Data, layout=c(1,1), xlab="Gruppe", ylab="Häufigkeit (%)", scales=list(x=list(rot=90)))
print(bar_plot_single)
dev.off()
答案 0 :(得分:1)
我认为它不适用于lattice
的分面,但您可以通过这种方式手动循环:
library(lattice)
dat <- data.frame(proband = c(0, 0, 0, 1, 1, 1),
stimulus = c("A", "B", "C", "A", "B", "C"),
group = c("X", "Y", "Z", "X", "Y", "N"))
labels <- rbind("", levels(dat$group))
by(dat, dat$stimulus, function(x) {
histogram(~ group,
data = x,
layout = c(1,1),
xlab = "Gruppe",
ylab = "Häufigkeit (%)",
ylim = c(-10, 110),
scales = list(x=list(rot=90,
labels = labels[cbind(levels(x[, 3]) %in% x[, 3] + 1,
1:length(levels(x[, 3])))])),
drop.unused.levels = FALSE) })
为每个图指定x轴刻度标签。
保存返回的列表对象,然后
do.call(gridExtra::grid.arrange, c(p, nrow = 1))
给出如下图:
然后pdf就像你的问题一样:
pdf("groups_single.pdf", width=8, height=4)
par(mar=c(1.5,0,1,2.5), xpd=TRUE)
by(dat, dat$stimulus, function(x) {
histogram(~ group,
data = x,
layout = c(1,1),
xlab = "Gruppe",
ylab = "Häufigkeit (%)",
ylim = c(-10, 110),
scales = list(x=list(rot=90,
labels = labels[cbind(levels(x[, 3]) %in% x[, 3] + 1,
1:length(levels(x[, 3])))])),
drop.unused.levels = FALSE) })
dev.off()
答案 1 :(得分:0)
我设法通过使用for循环来获得符合我需求的结果。
Data <- read.csv(file="./groups_data.csv", head=TRUE, sep=";")
stimuli <- factor(unique(Data$stimulus))
library(lattice)
pdf("output/groups_single.pdf", width=8, height=4)
par(mar=c(1.5,0,1,2.5), xpd=TRUE)
for ( i in stimuli ) {
data <- subset(Data, stimulus == i)
bar_plot_single <- histogram(~ group | stimulus,
data=data,
layout=c(1,1),
ylim=c(0,100),
xlab="Gruppe",
ylab="Häufigkeit (%)",
scales=list(x=list(rot=90)),
panel=function(...){
panel.abline(h=seq(0,150,20))
panel.abline(v=seq(0,150,1))
panel.histogram(...)
})
print(bar_plot_single)
}
dev.off()