rggplot2geomggsave

How to avoid overlapp in geom_line


I have a really long and extended code, which I have created with graphs that shows lines overlapping.

enter image description here

Could you please suggest something to disentangle this and make the reproduction clearer? Maybe some other options to modify the size of graph itself?

For example when save graphs with ggsave. Thanks

REPREX

Here the datset I am working on

df = structure(list(ANT = c("MEDIUM", "MEDIUM", "MEDIUM", "LOW", "LOW", 
"LOW", "HIGH", "HIGH", "HIGH"), BUT = c("flat", "tighted", "curved", 
"flat", "tighted", "curved", "flat", "tighted", "curved"), var = c("sighted", 
"sighted", "sighted", "sighted", "sighted", "sighted", "sighted", 
"sighted", "sighted"), mean = c(24.9795700007983, 22.5476640156841, 
24.4568444611505, 24.4608952261011, 22.179843452558, 25.1211893108135, 
24.1807279709165, 21.9700173259632, 25.5690587972475), low = c(24.1929993862564, 
22.0173613402898, 23.7793134256169, 23.5748441551253, 21.2709153846706, 
24.2111126105412, 23.376952383782, 21.3272260871708, 24.7053231240633
), up = c(25.7661406153403, 23.0779666910784, 25.1343754966842, 
25.3469462970768, 23.0887715204454, 26.0312660110858, 24.984503558051, 
22.6128085647556, 26.4327944704316)), row.names = c(NA, -9L), class = c("tbl_df", 
"tbl", "data.frame"))

the table where to get the statistics label from

val = structure(list(group1 = c("flat", "flat", "tighted", "flat", 
"flat", "tighted", "flat", "flat", "tighted"), group2 = c("tighted", 
"curved", "curved", "tighted", "curved", "curved", "tighted", 
"curved", "curved"), y.position = c(45.985, 48.4375, 50.89, 53.985, 
56.4375, 58.89, 58.493, 60.9455, 63.398), lab = c("0.0132  -  *", 
"0.585  -  ns", "0.0494  -  ns", "0.0826  -  ns", "0.618  -  ns", 
"0.0214  -  ns", "0.0458  -  ns", "0.211  -  ns", "0.00171  -  **"
)), row.names = c(NA, -9L), class = c("tbl_df", "tbl", "data.frame"
))

and the code that I have used

library(ggplot2) 
library(ggpubr)

ggplot(df, 
       aes(x= BUT, y= mean, 
           colour= ANT, group = ANT)) +
  
  geom_errorbar(aes(ymin= low, 
                    ymax= up), 
                colour="black", width=.10) +
  geom_line() + 
  geom_point(size= 3, shape= 21, fill="white") + 
  stat_pvalue_manual(
    val, 
    step.increase=0.05,label = "lab")

Solution

  • This is not how I would approach the problem - I would exclude non-significant interactions from the plot to 'tidy' things up - but here is an answer to your question:

    library(tidyverse)
    library(ggpubr)
    
    df = structure(list(ANT = c("MEDIUM", "MEDIUM", "MEDIUM", "LOW", "LOW", 
                                "LOW", "HIGH", "HIGH", "HIGH"), BUT = c("flat", "tighted", "curved", 
                                                                        "flat", "tighted", "curved", "flat", "tighted", "curved"), var = c("sighted", 
                                                                                                                                           "sighted", "sighted", "sighted", "sighted", "sighted", "sighted", 
                                                                                                                                           "sighted", "sighted"), mean = c(24.9795700007983, 22.5476640156841, 
                                                                                                                                                                           24.4568444611505, 24.4608952261011, 22.179843452558, 25.1211893108135, 
                                                                                                                                                                           24.1807279709165, 21.9700173259632, 25.5690587972475), low = c(24.1929993862564, 
                                                                                                                                                                                                                                          22.0173613402898, 23.7793134256169, 23.5748441551253, 21.2709153846706, 
                                                                                                                                                                                                                                          24.2111126105412, 23.376952383782, 21.3272260871708, 24.7053231240633
                                                                                                                                                                           ), up = c(25.7661406153403, 23.0779666910784, 25.1343754966842, 
                                                                                                                                                                                     25.3469462970768, 23.0887715204454, 26.0312660110858, 24.984503558051, 
                                                                                                                                                                                     22.6128085647556, 26.4327944704316)), row.names = c(NA, -9L), class = c("tbl_df", 
                                                                                                                                                                                                                                                             "tbl", "data.frame"))
    val = structure(list(group1 = c("flat", "flat", "tighted", "flat", 
                                    "flat", "tighted", "flat", "flat", "tighted"), group2 = c("tighted", 
                                                                                              "curved", "curved", "tighted", "curved", "curved", "tighted", 
                                                                                              "curved", "curved"), y.position = c(45.985, 48.4375, 50.89, 53.985, 
                                                                                                                                  56.4375, 58.89, 58.493, 60.9455, 63.398), lab = c("0.0132  -  *", 
                                                                                                                                                                                    "0.585  -  ns", "0.0494  -  ns", "0.0826  -  ns", "0.618  -  ns", 
                                                                                                                                                                                    "0.0214  -  ns", "0.0458  -  ns", "0.211  -  ns", "0.00171  -  **"
                                                                                                                                  )), row.names = c(NA, -9L), class = c("tbl_df", "tbl", "data.frame"
                                                                                                                                  ))
    val <- val %>%
      mutate(lab = paste0(group1, " vs ", group2, ": ", lab))
      
    ggplot(df, 
           aes(x= BUT, y= mean, 
               colour= ANT, group = ANT)) +
      
      geom_errorbar(aes(ymin= low, 
                        ymax= up), 
                    colour="black", width=.10) +
      geom_line() + 
      geom_point(size= 3, shape= 21, fill="white") + 
      stat_pvalue_manual(val, step.increase=0.075,
                         label = "lab", size = 2.5,
                         y.position = 27)
    

    Created on 2023-04-28 with reprex v2.0.2


    If you wanted to only plot the significant terms on the plot, you could do something like:

    library(tidyverse)
    library(ggpubr)
    
    df = structure(list(ANT = c("MEDIUM", "MEDIUM", "MEDIUM", "LOW", "LOW", 
                                "LOW", "HIGH", "HIGH", "HIGH"), BUT = c("flat", "tighted", "curved", 
                                                                        "flat", "tighted", "curved", "flat", "tighted", "curved"), var = c("sighted", 
                                                                                                                                           "sighted", "sighted", "sighted", "sighted", "sighted", "sighted", 
                                                                                                                                           "sighted", "sighted"), mean = c(24.9795700007983, 22.5476640156841, 
                                                                                                                                                                           24.4568444611505, 24.4608952261011, 22.179843452558, 25.1211893108135, 
                                                                                                                                                                           24.1807279709165, 21.9700173259632, 25.5690587972475), low = c(24.1929993862564, 
                                                                                                                                                                                                                                          22.0173613402898, 23.7793134256169, 23.5748441551253, 21.2709153846706, 
                                                                                                                                                                                                                                          24.2111126105412, 23.376952383782, 21.3272260871708, 24.7053231240633
                                                                                                                                                                           ), up = c(25.7661406153403, 23.0779666910784, 25.1343754966842, 
                                                                                                                                                                                     25.3469462970768, 23.0887715204454, 26.0312660110858, 24.984503558051, 
                                                                                                                                                                                     22.6128085647556, 26.4327944704316)), row.names = c(NA, -9L), class = c("tbl_df", 
                                                                                                                                                                                                                                                             "tbl", "data.frame"))
    val = structure(list(group1 = c("flat", "flat", "tighted", "flat", 
                                    "flat", "tighted", "flat", "flat", "tighted"), group2 = c("tighted", 
                                                                                              "curved", "curved", "tighted", "curved", "curved", "tighted", 
                                                                                              "curved", "curved"), y.position = c(45.985, 48.4375, 50.89, 53.985, 
                                                                                                                                  56.4375, 58.89, 58.493, 60.9455, 63.398), lab = c("0.0132  -  *", 
                                                                                                                                                                                    "0.585  -  ns", "0.0494  -  ns", "0.0826  -  ns", "0.618  -  ns", 
                                                                                                                                                                                    "0.0214  -  ns", "0.0458  -  ns", "0.211  -  ns", "0.00171  -  **"
                                                                                                                                  )), row.names = c(NA, -9L), class = c("tbl_df", "tbl", "data.frame"
                                                                                                                                  ))
    val <- val %>%
      mutate(lab = paste0(group1, " vs ", group2, ": p=", lab)) %>%
      filter(str_detect(lab, "ns", negate = TRUE))
      
    jitter_seed <- 123
    
    ggplot(df, 
           aes(x = BUT, y = mean, 
               colour = ANT, group = ANT)) +
      geom_errorbar(aes(ymin = low, 
                        ymax = up),
                    position = position_jitterdodge(jitter.width = 0.005, jitter.height = 0, seed = jitter_seed)) +
      geom_line(position = position_jitterdodge(jitter.width = 0.005, jitter.height = 0, seed = jitter_seed)) + 
      geom_point(size = 3, shape = 21, fill = "white",
                 position = position_jitterdodge(jitter.width = 0.005, jitter.height = 0, seed = jitter_seed)) + 
      stat_pvalue_manual(val, step.increase = 0.075,
                         label = "lab", size = 3.5,
                         y.position = 27) +
      theme_bw(base_size = 16)
    

    Created on 2023-04-28 with reprex v2.0.2

    Would this approach work for your use-case?