Search code examples
rggplot2facetfacet-grid

R how to flip facet_grid rows?


I have the following plot and want the older adult row on the bottom, younger on the top.

enter image description here

data:

structure(list( Gender = structure(c(1L, 
1L, 2L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 
1L, 2L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 
1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 2L, 1L, 1L, 1L, 
2L, 2L, 2L, 1L), .Label = c("Male", "Female"), class = "factor"), 
    Age = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 
    2L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 
    2L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 1L, 
    1L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 1L), .Label = c("Younger", 
    "Older"), class = "factor"), SleepDeprivation = structure(c(2L, 
    1L, 1L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 
    1L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 2L, 
    2L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 
    2L, 1L, 2L, 1L, 1L, 2L, 2L), .Label = c("No", "Yes"), class = "factor"), 
    ROI = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "Right Noradrenergic Bundle", class = "factor"), 
    FA = c(0.270336896, 0.239859328, 0.253058553, 0.239136323, 
    0.236547187, 0.250703156, 0.240607098, 0.256228983, 0.249909833, 
    0.25690344, 0.263866663, 0.240790352, 0.25867039, 0.295251846, 
    0.215813458, 0.252566397, 0.281900764, 0.23559688, 0.242475167, 
    0.282497048, 0.259372681, 0.294507146, 0.288260996, 0.239144236, 
    0.223323226, 0.253974199, 0.240887016, 0.253256589, 0.223256662, 
    0.235150456, 0.291969478, 0.305382729, 0.270458698, 0.232845142, 
    0.28080976, 0.264537901, 0.258622199, 0.271028847, 0.225935951, 
    0.250249714, 0.283011079, 0.278435558, 0.279990733, 0.233835503, 
    0.238910407, 0.300289392, 0.274696231, 0.288767546, 0.223124981, 
    0.256094754, 0.239295736, 0.270433217, 0.299104929)), row.names = c(NA, 
-53L), class = "data.frame")

My code:

library(tidyverse)
library(wesanderson)
library(ggplot2)

DF$Age <- factor(DF$Age,  levels  = c("Younger", "Older"))

I noticed the geom_text I have is causing the issue. Since I originally omitted that code and the answer below is able to produce the correct plot.

The geomtext label:

labelp4w <- data.frame(
  label = c(c(paste0('atop(~italic(t)[Student](15.95) == -0.93)'),
              paste0("italic(p) == 0.36")),
            c(paste0('atop(italic(t)[Student](30.15) == 0.51)'),
              paste0("italic(p) == 0.61"))),
  Age   = c(c("Younger", "Younger"),
            c("Older","Older")))
## plot
(p4 <- ggplot(data = DF, aes(x=SleepDeprivation, y=FA)) +
    theme_minimal() +
        facet_grid(cols = vars(ROI),  rows = vars(Age),
             labeller = as_labeller(c("Younger"  = "Younger Adults",
                                      "Older"    = "Older Adults",
                                        "title" ="title")))+
    geom_violin(lwd = 1, alpha = 0.5, trim=F, aes(fill = Age)) +
    geom_boxplot(width = 0.1) +
    scale_fill_manual(values=wes_palette(n=2, name="GrandBudapest2")) +
    geom_text(data = labelp4w,
              x = 1.5,
              y=seq(0.75,0.68, length = 4),
              label = labelp4w$label, 
              size = 7.5, parse = T) +
    scale_y_continuous(breaks = seq(0.2, 0.8, 0.2),limits = (c(0.2, 0.8))) +
    theme(plot.title    = element_text(hjust = 0.3, size=40, face="bold", margin=margin(0,0,0,0)),
        legend.position = "none",
        legend.text   = element_blank(),
        legend.title  = element_blank(),
        axis.title.y  = element_blank(),
        axis.title.x  = element_text(size = 25, face = "bold"),
        axis.text.x   = element_text(size = 25, face = "bold"),
        axis.text.y   = element_blank(),
        strip.text.x  = element_text(size  = 28, face = "bold"),
        strip.text.y  = element_text(size  = 20, face="bold"),
        plot.subtitle = element_text(hjust = 0.5, size = 20),
        plot.caption  = element_text(color = "black", size =20),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        strip.background = element_blank(),
        panel.border = element_blank()) + 
    labs(x = "")
)

The issue is occurring with the addition of geom_text. I'm not sure why?


Solution

  • This seems to be caused on your end.

    DF$Age <- factor(DF$Age,  levels  = c("Younger", "Older"))
    ggplot(data = DF, aes(x=SleepDeprivation, y=FA)) +
        facet_grid(cols = vars(ROI),  rows = vars(Age),
                   labeller = as_labeller(c("Younger"  = "Younger Adults",
                                            "Older"    = "Older Adults",
                                            "title" ="title")))+
        geom_violin(lwd = 1, alpha = 0.5, trim=F, aes(fill = Age)) +
        geom_boxplot(width = 0.1) +
        labs(x = "")
    

    In the above I removed all of the clutter code, that does not affect your positioning.

    enter image description here

    Update for geom_text

    With the geom_text inclusion this seems like a vague interaction between when the ordering is performed for 2 datasets. Notice that DF$Age is a factor while labelp4w$Age is a character vector. In this case the sorting is coming from the latter. Just ensure that both are factors.

    
    labelp4w <- data.frame(
      label = c(c(paste0('atop(~italic(t)[Student](15.95) == -0.93)'),
                  paste0("italic(p) == 0.36")),
                c(paste0('atop(italic(t)[Student](30.15) == 0.51)'),
                  paste0("italic(p) == 0.61"))),
      Age   = factor(c("Younger", "Younger", "Older", "Older"), levels = c("Younger", "Older")))