1

I would like to divide my x axis into two groups. I managed already to make one group using facet but can't make a second one. Here is a picture of what I already have and what I want additionally. Here is also my command:

ggplot(myk.p, aes(x=Group, y=Colonization, fill=MO)) +
    geom_bar(stat="identity", colour="black") +
    scale_fill_manual(values=c("#000000","#7a7a7a","#c0c0c0")) +
    theme(panel.margin = unit(0, "lines"), strip.background =
    element_blank()) + facet_wrap(~Water, switch = "x", scales = "free_x") +
    ggtitle("Root mycorrhization in % (Populus nigra)")

Chart

Example Data.csv:

"Group" "Colonization"  "MO"    "Water" "Fertilizer"
"P1"    69,2307692308   "C" "once"  "without"
"P2"    71,8232044199   "F" "once"  "without"
"P3"    82,5174825175   "I" "once"  "without"
"P4"    66,4122137405   "C" "once"  "with"
"P5"    86,013986014    "F" "once"  "with"
"P6"    98,7654320988   "I" "once"  "with"
"P7"    41,9540229885   "C" "twice" "without"
"P8"    65,7894736842   "F" "twice" "without"
"P9"    63,9705882353   "I" "twice" "without"
"P10"   62,5850340136   "C" "twice" "with"
"P11"   87,012987013    "F" "twice" "with"
"P12"   89,696969697    "I" "twice" "with"
Ttam
  • 63
  • 1
  • 7
  • If you would like, you could do `facet_grid` instead of `facet_wrap`, although that will give you a 2x2 plot – Dave Gruenewald Sep 14 '16 at 14:13
  • You need to include enough data to make your issue [reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Generally, you need to facet by a variable with two levels to get two facets. – alistaire Sep 14 '16 at 14:54
  • I added a sample csv file. – Ttam Sep 14 '16 at 19:21
  • How about `facet_wrap(~Water + Fertilizer, switch = "x", scales = "free_x", nrow = 1)`? It's not exactly what you have, but pretty close. Adding back some spaces between panels would make it clearer. – aosmith Sep 14 '16 at 20:21

1 Answers1

0

Thanks for the comments. aosmith: Works perfect. The only problem I now have is that the ordering is wrong. I want to start it from P1,P2,...P12 This is what I got now:

almost

Ttam
  • 63
  • 1
  • 7
  • The facets are ordered by the levels of the factors. So if you want `without` to come first you can set levels of the factor: `myk.p$Fertilizer = factor(myk.p$Fertilizer, levels = c("without", "with"))` and rerun the graph code. As this currently reads this should be a comment, not an answer. Add in your final code to make this an official answer – aosmith Sep 14 '16 at 21:29
  • Works fine. Thanks so much. – Ttam Sep 15 '16 at 00:56