3

I edited this plot with Inkscape, however I wonder if is there a way to do it within ggplot environment?

enter image description here

I did a quick search in the web without any success. Can someone give me a workaround?

I would really appreciate because I consider it a very common type of plot.

The code for this plot is:

var3 %>% 
  gather(-(exp:rep), key = "variable", value ="value") %>%
  mutate(variable = recode(variable, 
                           diam = "Lesion diameter",
                           dles = "Lesion density",
                           sev = "Disease severity")) %>% 
  ggplot(aes(x = Iso, y = value)) + 
  geom_boxplot(aes(fill=factor(exp)),
               position = position_dodge(width = 0), alpha = 0.5, 
               width =0.3) +
  facet_grid(variable~Cv, 
             labeller = labeller(Cv = label_both, variable = label_value),
             scales = "free")+
  scale_y_continuous(labels=scaleFUN)+
  theme_juan(9, "top", "center")+
  scale_fill_manual(values = c("grey70","grey30"),
                    labels = c("Exp. 1","Exp. 2"), name = "")

Being var3a data frame looking like:

# A tibble: 72 x 7
     exp           Cv    Iso   rep        sev      dles      diam
   <dbl>       <fctr> <fctr> <dbl>      <dbl>     <dbl>     <dbl>
 1     1 BMX_Potencia     MT     1 0.17262317 1.8924050 0.2400000
 2     1 BMX_Potencia     MT     2 0.14700000 2.1483445 0.3000000
 3     1 BMX_Potencia     MT     3 0.01404289 1.3623602 0.2133333
 4     1 BMX_Potencia     MT     4 0.11933333 1.3160049 0.2900000
Juanchi
  • 1,147
  • 2
  • 18
  • 36
  • 3
    You should provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample data and the code you are using to make the plot. – MrFlick Nov 08 '17 at 21:05
  • This is not really the use case for facets. They are designed for comparing the same variable across groups, _i.e._ the y-axes should have the same units and preferably, the same scale. You could create your plot using a tool such as [cowplot](https://cran.r-project.org/web/packages/cowplot/vignettes/introduction.html). – neilfws Nov 08 '17 at 21:34
  • Another option would be to use grobs (look up `ggplotGrob()`, `arrangeGrob()` and `grid.arrange()`) to combine 3 plots into a multipanel figure. – Jan Boyer Nov 08 '17 at 21:46
  • I agree with @neilfws, however I found this function a handly way to align the plots and avoid using multiple x-axis when it is a common variable as time, for instance. – Juanchi Nov 08 '17 at 21:54
  • for meteorological data as, precipitations, max, min temperatures it would be also suitable, as https://stackoverflow.com/questions/15999304/plotting-continuous-and-discrete-series-in-ggplot-with-facet – Juanchi Nov 08 '17 at 22:53

1 Answers1

2

you could try this workaround

library(ggplot2)
library(grid)

d <- data.frame(x=1:4, f1=gl(2,2, labels=c("A","B")), f2=rep(letters[1:2],each=2))

p1 <- ggplot(d, aes(x,x)) + geom_blank() + 
  facet_grid(f1~f2)

p2 <- p1 + facet_grid(f1~f2, switch = 'y', 
                      labeller = labeller(f1=c(A="this", B="that")))+ 
  theme(strip.placement.y = "outside",
        strip.text.y = element_text(angle = 90),
        strip.background = element_blank()) 

g1 <- ggplotGrob(p1)
g2 <- ggplotGrob(p2)

grid.newpage()
grid.draw(gridExtra::gtable_cbind(g2[,-ncol(g2)], g1[,ncol(g1)-3]))

enter image description here

baptiste
  • 75,767
  • 19
  • 198
  • 294