【问题标题】:How to discard the x-axis and place manually factor axis in specified position ggplot2 in R如何丢弃x轴并手动将因子轴放置在R中的指定位置ggplot2
【发布时间】:2022-10-13 19:33:16
【问题描述】:

我在Rggplot2 中创建了以下情节

KL_tot_RM = c(19.95814, 19.82527, 19.59050, 19.54873, 19.52221, 19.51316, 20.69058,
  20.40126, 19.85393, 19.70881, 19.59630, 19.55224, 21.07645, 20.75875,
  20.09839, 19.88498, 19.69226, 19.60695, 21.87141, 21.53600, 20.77132,
  20.46374, 20.10521, 19.89015, 22.15567, 21.82590, 21.05127, 20.72808,
  20.33150, 20.07257, 22.43661, 22.12112, 21.33892, 21.00751, 20.58678,
  20.29487, 22.66943, 22.37502, 21.66856, 21.33899, 20.90636, 20.59057,
  22.58487, 22.11511, 21.80200, 21.36871, 21.03423, 22.33055, 22.06044,
  21.75075, 22.45573, 22.17089, 20.63523)
KL_tot_BM = c(19.39840, 19.44582, 19.50268, 19.50384, 19.50405, 19.50406, 19.39609,
  19.51611, 19.51505, 19.51001, 19.50653, 19.50530, 19.73257, 19.69439,
 19.55838, 19.53254, 19.51575, 19.50994, 21.00110, 20.71141, 20.07691,
 19.87026, 19.68416, 19.60225, 21.48362, 21.18844, 20.47549, 20.19863,
 19.90049, 19.74088, 21.92624, 21.65443, 20.92355, 20.61059, 20.23113,
 19.99025, 22.26873, 22.11128, 21.42642, 21.10224, 20.68051, 20.38133,
 22.31634, 22.00143, 21.69530, 21.26701, 20.94143, 22.37260, 22.04097,
 21.73265, 19.70630, 22.23376, 19.32922)
df_plot = data.frame(KL = c(KL_tot_RM,KL_tot_BM), x= 1:53, 
                     data_set = c(rep('RM',53),rep('BM',53)) )
ggplot(df_plot, aes(x = x, y = KL, colour = data_set)) +
  geom_point(size = 1.8)

我想做什么来丢弃 x 轴,即删除值 0、20、40 并将其留空。然后以某种方式创建以下情节

垂直虚线表示组。我想做的是在每个组的中间放置一个因素p_{1}, p_{2}, p_{3},....,p_{11} 有没有办法做到这一点?

【问题讨论】:

  • 你能把这个因素添加到你的data.frame中吗?

标签: r


【解决方案1】:

改进的解决方案可让您轻松更改间隔,并将标签放置在每个箱的中心。

my_interval <- 5
breaks <- seq(0, max(df_plot$x), my_interval)
labels <- breaks + my_interval/2

ggplot(df_plot, aes(x = x, y = KL, colour = data_set)) +
  geom_point(size = 1.8) +
  geom_vline(xintercept = breaks, linetype = "dashed") +
  scale_x_continuous(breaks = labels,
                     labels = paste0("P", seq_along(labels))) + 
  theme(axis.ticks.x = element_blank(),
        axis.title.x = element_blank())

【讨论】:

    【解决方案2】:

    如果您只想删除下面的轴刻度和文本,只需将 element_blank 参数添加到主题:

    ggplot(df_plot, aes(x = x, 
                        y = KL, 
                        colour = data_set)) +
      geom_point(size = 1.8)+
      theme(axis.text.x = element_blank(),
            axis.ticks.x = element_blank())
    

    这给了你这个:

    要添加任意中断,您可以使用此函数执行此操作:

    ggplot(df_plot,
           aes(x = x,
               y = KL, 
               colour = data_set)) +
      geom_point(size = 1.8)+
      scale_x_continuous(breaks = c(0,10,20,30,40,50),
                         labels=c("P1",
                                "P2",
                                "P3",
                                "P4",
                                "P5",
                                "P6"))
    

    可能还有一种更好的编码方式,但您也可以为所有这些中断添加geom_vline。如果你想改变美学,你可以在 geom_vline 函数中这样做:

    ggplot(df_plot,
           aes(x = x,
               y = KL, 
               colour = data_set)) +
      geom_point(size = 1.8)+
      scale_x_continuous(breaks = c(0,10,20,30,40,50),
                         labels=c("P1",
                                "P2",
                                "P3",
                                "P4",
                                "P5",
                                "P6"))+
      geom_vline(xintercept = 0)+
      geom_vline(xintercept = 10)+
      geom_vline(xintercept = 20)+
      geom_vline(xintercept = 30)+
      geom_vline(xintercept = 40)+
      geom_vline(xintercept = 50)
    

    像这样:

    编辑

    只需在此处添加最后一个参数即可丢弃 x 轴标签:

    ggplot(df_plot,
           aes(x = x,
               y = KL, 
               colour = data_set)) +
      geom_point(size = 1.8)+
      scale_x_continuous(breaks = c(0,10,20,30,40,50),
                         labels=c("P1",
                                  "P2",
                                  "P3",
                                  "P4",
                                  "P5",
                                  "P6"))+
      geom_vline(xintercept = 0)+
      geom_vline(xintercept = 10)+
      geom_vline(xintercept = 20)+
      geom_vline(xintercept = 30)+
      geom_vline(xintercept = 40)+
      geom_vline(xintercept = 50)+
      labs(x="")
    

    【讨论】:

    • 这是一个很棒的答案,但是我如何删除 xlab 的 x 呢?
    • 刚刚编辑了我的答案:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 2019-04-12
    • 2012-09-06
    • 1970-01-01
    • 2015-09-29
    相关资源
    最近更新 更多