简体   繁体   English

ggplot2 中折线图的多个 X 轴值

[英]Multiple X-axis values for a line graph in ggplot2

Let's say I have this dataframe假设我有这个 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.我想绘制一个具有多个 X 轴值的折线图,类似于闪避条形图,但带有折线图。 The graph should look something like this:该图应如下所示: 图形图像

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:此代码当前正在输出: 输出错误

I'd greatly appreciate any help, thanks我将不胜感激任何帮助,谢谢

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")

在此处输入图像描述

You could use an interaction with your A and R variable and annotate the respective labels.您可以使用与 A 和 R 变量的interactionannotate相应的标签。 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创建于 2022-11-19,使用reprex v2.0.2

Specify the axis breaks, minor_breaks (separators) and labels in scale_x_continuous .scale_x_continuous中指定轴中断、 minor_breaks (分隔符)和标签。 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 .使用 ggprism,我们可以在theme中使用prism.ticks.length.x=指定次要中断的刻度长度,以使用 scale_x_continuous 中的scale_x_continuous minor_breaks=获得分隔符及其沿次要中断 X 轴的位置的效果。

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)
 

截屏

Note笔记

The input df reformatted from the question to make it easier to copy:输入df从问题中重新格式化以使其更容易复制:

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
))

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))

线图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM