hopping around

~ヘタレ研究者は今日も逝く~

multiplot

Rのcoefplotパッケージに,multiplotというのがあるんだけど,普通に出力すると,カラー出力されてしまう:

旬刊商事法務に原稿を提出するので,カラーじゃダメで,白黒にしなきゃいけないので,shapeとかlinestyleとかいじってみたら,legendが2つ出力されてしまったorz:

legendの消し方が分からないので,もうこうなったらbrute forceで行くしかないのん,と腹をくくって,position_dodgeとpointとerrorbarを駆使して,気合いで作ってみたの図:

ggplot2マンセーだけど,疲れた...orz

 

基本的なアイデアは,フィッティングしたモデルとrobust SE計算したところから必要な数値を取り出すfunctionつくって

get_estimates_for_coefplot<- function(mod, se, modname){
    test2 <- data.frame(summary(mod)$coefficients)
    test2['term'] <- names(coef(mod))
    test2['model'] <- modname
    test2['estimate'] <- test2$Estimate
    test2['std.error'] <- se
    test2['ub'] <- test2$Estimate + test2$std.error * 1.959964
    test2['lb'] <- test2$Estimate - test2$std.error * 1.959964
    return(test2)
}

モデルごとにつくったdfをrbindして,必要な変数名をfilterして日本語化して(場合によってはfactorで並べ替えして),最後にggplotに放り込んでlinetype/shapeをmodelで区分してposition_dodgeで並べる。