Home > other >  Multiple X-axis values for a line graph in ggplot2
Multiple X-axis values for a line graph in ggplot2

Time:11-20

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

CodePudding user response:

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

CodePudding user response:

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 screenshot

CodePudding user response:

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

  • Related