【问题标题】:Multiple X-axis values for a line graph in ggplot2ggplot2 中折线图的多个 X 轴值
【发布时间】:2022-11-20 02:35:20
【问题描述】:

假设我有这个数据框

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

此代码当前正在输出:

我将不胜感激任何帮助,谢谢

【问题讨论】:

    标签: r ggplot2 plot graph


    【解决方案1】:

    我不确定,但也许这可能是一个开始:

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

    【讨论】:

      【解决方案2】:

      您可以将 interaction 与您的 A 和 R 变量一起使用,并将 annotate 与各自的标签一起使用。这是一个可重现的例子:

      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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-01
        • 1970-01-01
        • 2023-03-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多