尽管已经使用过几次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
运行它,但仅限于分类列?但我不知道该怎么做!
答案 0 :(得分:1)
几点
hyp
或有问题的变量作为一个因素。删除最容易
执行此操作之前的NA
值。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))
现在您希望facet_grid
与scales = 'free_y'
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))