0

Let's say I have this dataframe

df <- structure(list(A = c(25, 25, 25, 50, 50, 50, 100, 100, 100, 250, 250, 250),
                     R = c("R1", "R2", "R3", "R1", "R2", "R3", "R1", "R2", "R3", "R1", "R2", "R3"), 
                     ACI = c(2.75769,
                             3.59868,
                             3.00425,
                             1.90415,
                             2.19912,
                             2.01439,
                             1.34013,
                             1.45594,
                             1.3738,
                             0.84241,
                             0.87391,
                             0.85184
                     ), 
                     PB = c(3.06259,
                            4.10288,
                            3.40414,
                            2.00337,
                            2.32796,
                            2.13138,
                            1.37404,
                            1.49467,
                            1.40867,
                            0.84817,
                            0.88002,
                            0.85838
                     ), 
                     NB = c(3.13425,
                            4.22754,
                            3.49041,
                            2.03281,
                            2.36812,
                            2.16289,
                            1.3858,
                            1.5086,
                            1.42187,
                            0.85346,
                            0.88572,
                            0.86346
                     ), 
                     Bca = c(2.65087,
                             3.3918,
                             2.86767,
                             1.89719,
                             2.20208,
                             2.00181,
                             1.35534,
                             1.49656,
                             1.38895,
                             0.85497,
                             0.9015,
                             0.86487
                     ), 
                     SB = c(3.33211,
                            4.42798,
                            3.73011,
                            2.12197,
                            2.48144,
                            2.266,
                            1.41635,
                            1.54522,
                            1.45326,
                            0.85775,
                            0.89055,
                            0.86863
                    ), 
                     `round(2)` = c(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2)),
                     class = "data.frame", row.names = c(NA, -12L))

I would like to draw a line graph with multiple X-axis values, something like a dodged bar graph, but with a line graph. The graph should look something like this: Graph Image

My attempt until now is this:


df %>% 
  pivot_longer(ACI:SB) %>% 
  mutate(across(where(is.character), as.factor)) %>% 
  ggplot(aes(x = R, y = value, group=name)) +
  geom_line()+
  facet_wrap(~A, nrow=1, strip.position="bottom")

This code is currently outputting this: Wrong Output

I'd greatly appreciate any help, thanks

Dave2e
  • 22,192
  • 18
  • 42
  • 50
Ahmet-Salman
  • 194
  • 8

4 Answers4

2

I am not sure but maybe this could be a start:

library(tidyverse)

df %>% 
  pivot_longer(ACI:SB) %>% 
  mutate(across(where(is.character), as.factor)) %>% 
  ggplot(aes(x = A, y = value, group = name, color = name)) +
  geom_point()+
  geom_line()+
  facet_wrap(.~R, nrow = 1, strip.position = "bottom")+
  theme_classic()
  labs(x="Test/Train", y="Score", fill="Segment Length") +
  theme(panel.spacing = unit(0, "lines"), strip.placement = "outside")

enter image description here

TarJae
  • 72,363
  • 6
  • 19
  • 66
2

You could use an interaction with your A and R variable and annotate the respective labels. Here is a reproducible example:

library(dplyr)
library(ggplot2)
library(ggthemes)
library(tidyr)

df %>% 
  pivot_longer(ACI:SB) %>% 
  mutate(across(where(is.character), as.factor)) %>% 
  ggplot(aes(x = interaction(A, R), y = value, group=name)) +
  geom_line(aes(color = name)) +
  geom_point(aes(color = name)) +
  coord_cartesian(ylim = c(0, 5), expand = FALSE, clip = "off") +
  annotate(geom = "text", x = seq_len(nrow(df)), y = -0.1, label = df$R, size = 3) +
  annotate(geom = "text", x = 2 + 3 * (0:3), y = -0.3, label = unique(df$A), size = 3) +
  theme_excel_new() +
  theme(plot.margin = unit(c(1, 1, 4, 1), "lines"),
        axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        panel.grid.major.x = element_blank(),
        panel.grid.minor.x = element_blank(),
        legend.position = c(0.5, -0.15), legend.direction = 'horizontal')

Created on 2022-11-19 with reprex v2.0.2

Quinten
  • 35,235
  • 5
  • 20
  • 53
1

Specify the axis breaks, minor_breaks (separators) and labels in scale_x_continuous. With the ggprism we can specify t he tick lengths for the minor breaks using prism.ticks.length.x= in theme to get the effect of separators and their locations along the X axis of the minor breaks using minor_breaks= in scale_x_continuous.

library(dplyr)
library(tidyr)
library(ggplot2)
library(ggprism)

xlabs <- with(df, ifelse(R == "R2", paste(R, A, sep = "\n"), R))
breaks <- seq_along(xlabs)

df %>% 
  mutate(x = 1:n()) %>%
  pivot_longer(ACI:SB) %>% 
  ggplot(aes(x, value, col = name)) +
    geom_line(size = 2) +
    xlab("") +
    ylab("") +
    theme(prism.ticks.length.x = unit(25, "pt"),
          legend.position = "bottom") +
    scale_x_continuous(guide = "prism_minor", 
                       limits = range(breaks), 
                       breaks = breaks, 
                       labels = xlabs,
                       minor_breaks = breaks[xlabs == "R3"]  + .5)
 

screenshot

Note

The input df reformatted from the question to make it easier to copy:

df <-
structure(list(A = c(25, 25, 25, 50, 50, 50, 100, 100, 100, 250, 
250, 250), R = c("R1", "R2", "R3", "R1", "R2", "R3", "R1", "R2", 
"R3", "R1", "R2", "R3"), ACI = c(2.75769, 3.59868, 3.00425, 1.90415, 
2.19912, 2.01439, 1.34013, 1.45594, 1.3738, 0.84241, 0.87391, 
0.85184), PB = c(3.06259, 4.10288, 3.40414, 2.00337, 2.32796, 
2.13138, 1.37404, 1.49467, 1.40867, 0.84817, 0.88002, 0.85838
), NB = c(3.13425, 4.22754, 3.49041, 2.03281, 2.36812, 2.16289, 
1.3858, 1.5086, 1.42187, 0.85346, 0.88572, 0.86346), Bca = c(2.65087, 
3.3918, 2.86767, 1.89719, 2.20208, 2.00181, 1.35534, 1.49656, 
1.38895, 0.85497, 0.9015, 0.86487), SB = c(3.33211, 4.42798, 
3.73011, 2.12197, 2.48144, 2.266, 1.41635, 1.54522, 1.45326, 
0.85775, 0.89055, 0.86863), `round(2)` = c(2, 2, 2, 2, 2, 2, 
2, 2, 2, 2, 2, 2)), class = "data.frame", row.names = c(NA, -12L
))
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341
0

This may come close to what you expect

df %>% 
  pivot_longer(ACI:SB) %>% 
  ggplot(aes(rep(1:nrow(df), each = length(value)/nrow(df)), 
      value, col = name)) + 
    geom_line() + 
    geom_point() + 
    xlab("") + 
    scale_x_continuous(breaks = c(1:nrow(df)), labels = paste(df$R, df$A))

line plot

Andre Wildberg
  • 12,344
  • 3
  • 12
  • 29