0

(EDIT: The issues raised in this post are solved below)

I am trying to get my bar plot to look a certain way. I hope you can follow me without really explaining what the purpose of this plot is or the context of the data, so I'll dive right in.

I'm using this code

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())

to produce the following plot

enter image description here

What I want to see, however, is

  1. that the AUC-values on top of the bars are correctly aligned with every single bar (grouped by "demographics" [adjusted/not adjusted]) and not with the middle of the group "fraction" (serum/plasma) as it is right now

  2. that the two bars in the last facet "FN=5" each have only half of their current width (to match the width of the other bars in facets "FN=1" to "FN=4"

  3. that there is another label printed vertically within every bar. This label is stored in the character variable "features" contained in the data set "ds" used to print the plot

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")

EDIT: Eventually, using the help in the comments section, I used this code

            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())

to produce the results I initially wanted (I changed my mind on halving the width of the columns in facet "FN=5" though), looking like this

enter image description here

To increase readability, I decided to flip the plot using this code

            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() 

which results in a horizontal bar plot now looking like this (colors and order of legend keys are not quite right yet)

enter image description here

s.erhardt
  • 77
  • 1
  • 3
  • 9
  • Pls make you question [reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Use `dput(d)` instead of an image of the data – GGamba Jul 07 '17 at 10:53
  • 2
    Get the dev version of ggplot, then use `position_dodge(preserve = "single")` for both geoms. You also need to set the `group` for `geom_text` appropriately to allow for dodging. This is probably automatic is you move the fill aesthetic to `ggplot` instead of `geom_bar`. Images of data are not useful. Please ask one question per question. – Axeman Jul 07 '17 at 11:10
  • @GGamba Sorry for the inconvenience and thanks for the info. – s.erhardt Jul 07 '17 at 13:27
  • @Axeman Thanks for your tips. I fixed the problem with the labels above the bars not aligning with the middle of the respective bar by moving `fill=factor(demographics, c("adjusted", "not adjusted"))` to `ggplot` instead of `geom_bar` - just as you advised me to do. – s.erhardt Jul 07 '17 at 14:28

0 Answers0