2012-09-11 14 views
0

尽管已经使用了ggplot几次,但我很努力地解决这个问题。我想这将是最简单的解释是什么我想没有我的恶劣企图这样做,在使用ggplot,以下是相当难看太多,但它是最好的,我可以在此刻:(管理gidplot from`mids` object

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中创建一个漂亮的图表,显示这些类型的barlot图 - 例如,6行中的一行,所有的图表都在y轴上具有相同的比例尺,以便可以轻松地进行比较。 ldt <-complete(impute,"long", include=TRUE)然后melt(ldt, c(".imp",".id","hyp"))但我只是不能工作后如何调用ggplot :(

请请注意,我的真实数据中有许多不同的变量,这仅适用于分类变量。我想我可以做出一个功能,然后运行,使用sapply,但只能在分类列?但我不知道该怎么做!

回答

1

几个点

  1. 你需要有hyp或作为一个因素考虑的变量。在执行此操作之前,最简单的方法是删除 ,NA值。
  2. 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)) 

enter image description here

以长格式

现在你要facet_gridscales = '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)) 

enter image description here

相关问题