【问题标题】:Discrete axis ticks and labels shifted compared to actual values与实际值相比,离散轴刻度和标签偏移
【发布时间】:2015-10-21 17:11:07
【问题描述】:

我有一个如下数据集:我有一个这样的数据框,称为data_frame_test

        Value    time   group
   3.96655960       0     184
  -8.71308460       0     184
 -11.11638947       0     184
  -6.84213562      11     184
  -1.25926609      11     184
  -4.60649529      11     184
   0.27577858      11     184
  11.85394249      20     184
  -0.27114563      20     184
   1.73081284      20     184
   1.78209915      20     184
  11.34305840      20     184
  13.49688263      20     184
  -7.54752045      20     184
 -13.63673286      25     184
  -5.75711517      25     184
   0.35823669      25     184
  -2.45237694      25     184
   0.49313087       0      66
  -9.04148674       0      66
 -15.50337906       0      66
 -17.51445351       0      66
 -10.66807098       0      66
  -2.24337845       5      66
 -13.79929533       5      66
   1.33287125       5      66
   2.22143402       5      66
  11.46484833      10      66
  23.26805916      10      66
   9.07377968      10      66
   4.28664665      10      66

data_frame_test <- structure(list(Value = c(3.9665596, -8.7130846, -11.11638947, 
-6.84213562, -1.25926609, -4.60649529, 0.27577858, 11.85394249, 
-0.27114563, 1.73081284, 1.78209915, 11.3430584, 13.49688263, 
-7.54752045, -13.63673286, -5.75711517, 0.35823669, -2.45237694, 
0.49313087, -9.04148674, -15.50337906, -17.51445351, -10.66807098, 
-2.24337845, -13.79929533, 1.33287125, 2.22143402, 11.46484833, 
23.26805916, 9.07377968, 4.28664665), time = c(0L, 0L, 0L, 11L, 
11L, 11L, 11L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 25L, 25L, 25L, 
25L, 0L, 0L, 0L, 0L, 0L, 5L, 5L, 5L, 5L, 10L, 10L, 10L, 10L), 
    group = c(184L, 184L, 184L, 184L, 184L, 184L, 184L, 184L, 
    184L, 184L, 184L, 184L, 184L, 184L, 184L, 184L, 184L, 184L, 
    66L, 66L, 66L, 66L, 66L, 66L, 66L, 66L, 66L, 66L, 66L, 66L, 
    66L)), .Names = c("Value", "time", "group"), class = "data.frame", row.names = c(NA, 
-31L))

我想为每个时间点和组绘制一个值的箱线图。

ggplot(data_frame_test, aes(x=factor(time), y=Value, colour = factor(group)))  + 
  geom_boxplot(outlier.size=0, fill = "white", position="identity", alpha=.5) +
  scale_x_discrete(limits = seq(-1,26), breaks = seq(-1,26), labels = seq(-1,26))

结果如下图,几乎是对的: 但是,x 轴标签和刻度会移动。我如何把它放在它所属的地方?

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    您试图将一个因子视为数字,但事实并非如此。这是一个更好的解决方案:

    ggplot(data_frame_test, aes(x=factor(time, levels = seq(-1,26), ordered = TRUE), 
                                y=Value, colour = factor(group)))  + 
      geom_boxplot(outlier.size=0, fill = "white", position="identity", alpha=.5) +
      scale_x_discrete(drop = FALSE)
    

    【讨论】:

      【解决方案2】:

      我不太确定为什么会这样,但我可能会制作这样的情节,因为将时间转换为因子对我来说很直观:

      ggplot(data_frame_test, 
             aes(x = time, y=Value, colour = factor(group), group = interaction(time, group))) + 
        geom_boxplot(outlier.size=0, fill = "white", position="identity", alpha=.5)
      

      这给出了:

      您可以使用scale_x_continuous 更改休息时间等。

      【讨论】:

      • 是的,这实际上比我的回答要好,如果你没有一个因素开始。最后加上scale_x_continuous(breaks = -1:26)即可。
      • 哦,我看到了使它起作用的技巧:group = interaction(time, group)。这实际上是问题的后续,我不得不再分成两个问题:stackoverflow.com/questions/25033007/…
      猜你喜欢
      • 2011-09-28
      • 1970-01-01
      • 2015-12-18
      • 2019-05-11
      • 2013-06-18
      • 2019-10-26
      • 1970-01-01
      • 2012-03-14
      • 1970-01-01
      相关资源
      最近更新 更多