1
variable <-     c("PM10","SO2","NO","NO2","PM10","SO2","NO","NO2","PM10","SO2","NO","NO2","PM10","SO2","NO","NO2","PM10","SO2","NO","NO2","PM10","SO2","NO","NO2")
sex <- c("male","male","male","male","female","female","female","female",
     "male","male","male","male","female","female","female","female",
     "male","male","male","male","female","female","female","female")
exposureperiod <- c("P1","P1","P1","P1","P1","P1","P1","P1",
                "P2","P2","P2","P2","P2","P2","P2","P2",
                "P3","P3","P3","P3","P3","P3","P3","P3")

set.seed(100)
coef <- runif(24, -2, 2)
coef_lb <- coef - 0.3
coef_ub <- coef + 0.3

df <- data.frame(variable,sex,exposureperiod,coef,coef_lb,coef_ub)

df$variable <- factor(df$variable,levels=c("PM10","SO2","NO","NO2"))
levels(df$variable) <- c("PM[10]","SO[2]", "NO", "NO[2]")

df$exposureperiod <- factor(df$exposureperiod,levels=c("P1","P2","P3"))
df$sex <- factor(df$sex,levels=c("male","female"))

df <- df[order(df$variable,df$sex),]

df$aux <- c(1,2,3,
         5,6,7,
         11,12,13,
         15,16,17,
         21,22,23,
         25,26,27,
         31,32,33,
         35,36,37)

library(ggplot2)

plot <- ggplot(data = df, aes(x = aux, y = coef)) +
  geom_pointrange(aes(ymin=coef_lb,ymax=coef_ub),shape="none") +
  geom_point(aes(shape = exposureperiod)) +
  scale_shape_discrete(name  ="Exposure period",
                   breaks=c("P1", "P2","P3"),
                   labels=c("P1","P2","P3")) +
  scale_x_continuous("Sex and Pollutant",breaks=c(2,6,12,16,22,26,32,36), 
                 labels=c("Boys","Girls","Boys","Girls","Boys","Girls","Boys","Girls")) +
  scale_y_continuous("Mean Difference in Tanner Stage",
                 limits=c(-3, 3),
                 breaks=round(seq(-3, 3, by = 0.5),1)) +
  geom_hline(yintercept=0,alpha=1,linetype="dashed") +
  theme(axis.text.x = element_text()) +
  theme_bw(base_size = 16,base_family="Arial") +
  theme(legend.text.align = 0,
    legend.title = element_text(face="plain"),
    legend.key = element_blank(),
    legend.position = "bottom") +
  guides(shape= guide_legend(nrow = 3,byrow = TRUE)) +
  theme(text = element_text(colour = "black",face="plain"),
    axis.title.y = element_text(face="plain"),
    axis.title.x = element_text(face="plain"),
    axis.text.x = element_text(face="plain",hjust = 0),
    axis.text.y = element_text(face="plain")) + 
  theme(panel.background = element_blank(),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.border = element_blank(),
    axis.line = element_line(colour = "black"))+
  theme(axis.ticks = element_line(size = 1))

plot

With the above script, I got the graph as below.

enter image description here

But I want to add another level of x-axis, which indicate the PM10, SO2, NO, and NO2, like the below graph. (To illustrate, I added those pollutants manually.) And of course, the x-axis title and legend should move down accordingly.

I used facet before, but I want to avoid the gap between pollutants generate by facet.

Thank you.

enter image description here

cyrusjan
  • 637
  • 1
  • 8
  • 23
  • 2
    I'd suggest to facet by pollutant (and adjust the theme and facets to a close approximation of this). Small gaps between the pollutants (as you'd get with facets) would improve the plot. – Roland Mar 30 '16 at 08:18
  • Actually I did used facet before, but the x-axis is separated into four line segments, and could not combine into one. Given that I want to avoid the small gaps between pollutants, I turn to this. – cyrusjan Mar 30 '16 at 08:21
  • Then you are out of luck with ggplot2. Not having the gaps gives a worse presentation of your data then having them. – Roland Mar 30 '16 at 08:24
  • I do agree with you, but this is unfortunately required. – cyrusjan Mar 30 '16 at 08:28
  • 2
    You might be able to take something from [this](http://stackoverflow.com/questions/28853786/how-do-i-plot-charts-with-nested-categories-axes/28868462#28868462) – Sandy Muspratt Mar 30 '16 at 08:28
  • 1
    You can get rid of spaces between facets via `panel.margin`. That combined with the newer `switch` argument of `facet_wrap` seems like a straightforward way to get the plot you want. – aosmith Mar 30 '16 at 18:13

1 Answers1

0

You could try faceting the plot

plot <- plot + facet_wrap(~variable)
Richard Telford
  • 9,558
  • 6
  • 38
  • 51