GGPLOT分组带有刻面的条形图:每个条形图有多个对齐的标签?

时间:2017-07-07 10:49:22

标签: r ggplot2 label facet grouped-table

(编辑:本文中提出的问题在下面解决)

我试图让我的条形图看起来有某种方式。我希望你能跟着我,而不是真正解释这个情节的目的是什么或数据的背景,所以我会直接进入。

我正在使用此代码

ggplot(ds, aes(x=fraction, y=AUC)) + 
    geom_bar(aes(fill=factor(demographics, c("adjusted", "not adjusted"))), position=position_dodge(width=0.9), stat="identity") + 
    facet_grid(~FN, switch="x") + 
    geom_text(aes(label=round(AUC, 2), fontface="bold", vjust=-0.4), position=position_dodge(width=0.9), size=2.75) +
    theme(legend.title=element_blank(), legend.position="bottom", 
            axis.text.y=element_blank(),
            axis.title.x=element_blank(), axis.ticks.x=element_blank(), axis.ticks.y=element_blank(), 
            panel.background=element_blank())

制作以下情节

enter image description here

然而,我想看到的是

  1. 条形图顶部的AUC值与每个条形图正确对齐(按“人口统计学”[已调整/未调整]分组),而不是与“分数”组中间对齐(血清/血浆)就像现在一样

  2. 最后一个方面“FN = 5”中的两个条形每个只有它们当前宽度的一半(以匹配方面“FN = 1”到“FN = 4”的其他条形的宽度< / p>

  3. 每个栏内都有垂直打印的另一个标签。此标签存储在用于打印图表的数据集“ds”中包含的字符变量“features”中

  4. enter image description here

    ds <- structure(list(ds = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
                                2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 2L, 2L, 
                                2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L), .Label = c("1", 
                                "2", "3", "4"), class = "factor"), FN = structure(c(1L, 2L, 3L, 
                                4L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 5L, 1L, 
                                2L, 3L, 4L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 
                                5L), .Label = c("FN=1", "FN=2", "FN=3", "FN=4", "FN=5"), class = "factor"), 
                fraction = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("serum", 
                                "plasma"), class = "factor"), demographics = structure(c(1L, 
                                1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 
                                2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 
                                2L, 2L, 2L, 2L, 2L), .Label = c("not adjusted", "adjusted (age, sex, education, ethnicity, ApoE4)"
                        ), class = "factor"), AUC = c(0.741264171455195, 0.749961174095356, 
                        0.797639384997671, 0.763006678055599, 0.894704146606616, 
                        0.9038670601025, 0.90169280944246, 0.912564062742662, 0.912564062742662, 
                        0.672077922077922, 0.715367965367965, 0.746753246753247, 
                        0.791125541125541, 0.804112554112554, 0.827922077922078, 
                        0.829004329004329, 0.83982683982684, 0.83982683982684, 0.741264171455195, 
                        0.749961174095356, 0.797639384997671, 0.763006678055599, 
                        0.894704146606616, 0.9038670601025, 0.90169280944246, 0.912564062742662, 
                        0.912564062742662, 0.672077922077922, 0.715367965367965, 
                        0.746753246753247, 0.791125541125541, 0.804112554112554, 
                        0.827922077922078, 0.829004329004329, 0.83982683982684, 0.83982683982684
                ), features = c("p21", "p3 + p21", "p3 + p7 + p19", "p1 + p8 + p21 + p23", 
                        "p8", "p11 + p24", "p14 + p17 + p24", "p11 + p13 + p14 + p17", 
                        "p11 + p13 + p14 + p17", "p13", "p9 + p17", "p9 + p14 + p17", 
                        "p7 + p9 + p17 + p19", "p20", "p9 + p19", "p9 + p17 + p19", 
                        "p4 + p8 + p9 + p17", "p4 + p8 + p9 + p17", "p21", "p3 + p21", 
                        "p3 + p7 + p19", "p1 + p8 + p21 + p23", "p8", "p11 + p24", 
                        "p14 + p17 + p24", "p11 + p13 + p14 + p17", "p11 + p13 + p14 + p17", 
                        "p13", "p9 + p17", "p9 + p14 + p17", "p7 + p9 + p17 + p19", 
                        "p20", "p9 + p19", "p9 + p17 + p19", "p4 + p8 + p9 + p17", 
                        "p4 + p8 + p9 + p17")), .Names = c("ds", "FN", "fraction", 
                "demographics", "AUC", "features"), row.names = c(NA, -36L), class = "data.frame")
    

    编辑:最后,使用评论部分的帮助,我使用了这段代码

                ggplot(ds, aes(x=fraction, y=AUC, fill=factor(demographics, c("adjusted (age, sex, education, ethnicity, ApoE4)", "not adjusted")))) + 
                    geom_bar(position=position_dodge(width=0.9), stat="identity") + 
                    facet_grid(~FN) + 
                    geom_text(aes(label=round(AUC, 2), fontface="bold", vjust=1), position=position_dodge(width=0.9), size=2.75) + 
                    geom_text(aes(y=0.3, label=features, fontface="bold"), color="white", position=position_dodge(width=0.9), angle=90, size=3) + 
                    theme_bw() +
                    theme(legend.title=element_blank(), legend.position="bottom", 
                            axis.text.y=element_blank(),
                            axis.title.x=element_blank(), axis.title.y=element_text(face="bold"), axis.ticks.x=element_blank(), axis.ticks.y=element_blank(), 
                            panel.background=element_blank(), panel.grid.minor=element_blank(), panel.grid.major=element_blank())
    

    生成我最初想要的结果(我改变了主意,将facet的宽度减半“FN = 5”),看起来像这样

    enter image description here

    为了提高可读性,我决定使用此代码翻转图表

                ggplot(ds, aes(x=fraction, y=AUC, fill=factor(demographics, c("not adjusted", "adjusted (age, sex, education, ethnicity, ApoE4)")))) + 
                    geom_bar(position=position_dodge(width=0.9), stat="identity") + 
                    facet_grid(FN~.) + 
                    geom_text(aes(y=AUC, label=round(AUC, 2), fontface="bold"), position=position_dodge(width=0.9),  hjust=1.15, size=3.25) + 
                    geom_text(aes(y=0.4, label=features, fontface="bold"), position=position_dodge(width=0.9), color="white", size=3) + 
                    theme_bw() +
                    theme(legend.title=element_blank(), legend.position="bottom", 
                            axis.title.x=element_text(face="bold"), axis.title.y=element_blank(), 
                            axis.ticks.x=element_blank(), axis.ticks.y=element_blank(), 
                            panel.background=element_blank(), panel.grid.minor=element_blank(), panel.grid.major=element_blank(),   
                            strip.text.y = element_text(angle=0)) + coord_flip() 
    

    导致水平条形图现在看起来像这样(图例键的颜色和顺序还不是很正确)

    enter image description here

0 个答案:

没有答案