尽管已经使用了几次ggplot,我还是很难在这个问题上取得任何进展。我想这将是最容易解释我想要做什么,而不是我可恶的尝试使用ggplot,下面也是相当丑陋的,但这是我目前所能做到的最好的:
require(mice)
impute <- mice(nhanes, seed = 101)
counts <- table(nhanes$hyp)
barplot(counts, main="hyp", xlab="observed")
x11()
counts <- table(complete(impute,1)$hyp)
barplot(counts, main="hyp", xlab="Imputation 1")
x11()
counts <- table(complete(impute,2)$hyp)
barplot(counts, main="hyp", xlab="Imputation 2")
x11()
counts <- table(complete(impute,3)$hyp)
barplot(counts, main="hyp", xlab="Imputation 3")
x11()
counts <- table(complete(impute,4)$hyp)
barplot(counts, main="hyp", xlab="Imputation 4")
x11()
counts <- table(complete(impute,5)$hyp)
barplot(counts, main="hyp", xlab="Imputation 5")我想创建一个漂亮的网格图,在ggplot中显示这些类型的柱状图-例如,1行6,在y轴上都有相同的比例,以便可以很容易地进行比较。
我想我应该先使用ldt <-complete(impute,"long", include=TRUE),然后再使用melt(ldt, c(".imp",".id","hyp")),但我就是想不出怎么调用ggplot :(
请注意,我的实际数据中有许多不同的变量,这只适用于分类变量。我在想,我可以创建一个函数,然后使用sapply运行它,但只在分类列上运行吗?但是我不知道该怎么做!
发布于 2012-09-12 08:01:32
有几点
,
hyp或有问题的变量作为一个因子。最简单的做法是在执行以下操作之前删除NA值: this.facet_wrap (一个变量)或facet_grid (一个或多个变量)将为您很好地排列绘图。例如
ldt <-complete(impute,"long", include=TRUE)
ggplot(ldt[!is.na(ldt$hyp),], aes(x= factor(hyp))) +
geom_bar() +
facet_wrap(~.imp, nrow = 1) +
xlab('Observed') +
scale_y_continuous(expand = c(0,0))

长文形式
现在,您希望使用带有scales = 'free_y'的facet_grid
all_long <- melt(ldt, c(".imp",".id","hyp"))
ggplot(all_long[!is.na(all_long$hyp),], aes(x= factor(hyp))) +
geom_bar() +
facet_grid(variable ~.imp, scales = 'free_y') +
xlab('Observed') +
scale_y_continuous(expand = c(0,0))

https://stackoverflow.com/questions/12375142
复制相似问题