簡體   English   中英

ggplot2 中折線圖的多個 X 軸值

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

假設我有這個 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))

我想繪制一個具有多個 X 軸值的折線圖,類似於閃避條形圖,但帶有折線圖。 該圖應如下所示: 圖形圖像

到目前為止,我的嘗試是這樣的:


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

此代碼當前正在輸出: 輸出錯誤

我將不勝感激任何幫助,謝謝

我不確定,但也許這可能是一個開始:

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

在此處輸入圖像描述

您可以使用與 A 和 R 變量的interactionannotate相應的標簽。 這是一個可重現的例子:

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

創建於 2022-11-19,使用reprex v2.0.2

scale_x_continuous中指定軸中斷、 minor_breaks (分隔符)和標簽。 使用 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)
 

截屏

筆記

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

這可能接近您的預期

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