1

我正在 ggplot2 中制作 geom_col。x 轴是时间点的数值向量(0、6、18、24、32、44)。每列之间的差异对应于每个时间点之间的数值差异。但我希望所有列之间的距离相等。我在这里搜索了答案,但我没有找到类似的问题。

这是我的代码:

ggplot(data = ny_dataframe_scratch, aes(x=timepoint, y = relative_wound_healing, fill = Condition)) +
  geom_col(width = 5, position = position_dodge()) + 
  scale_x_continuous(breaks=c(0, 6, 18, 24, 32, 44), name = "Time point, hours") + 
  scale_y_continuous(name = "Relative scratch area") + 
  scale_fill_manual(values=c("palevioletred4", "slategray")) +
  geom_point(data = ny_dataframe_scratch, position = position_dodge(width = 5), aes(x=timepoint, y=relative_wound_healing, fill = Condition))

This is the output of dput():

structure(list(timepoint = c(0, 0, 0, 0, 0, 0, 6, 6, 6, 6, 6, 
6, 18, 18, 18, 18, 18, 18, 24, 24, 24, 24, 24, 24, 32, 32, 32, 
32, 32, 32, 44, 44, 44, 44, 44, 44), Condition = structure(c(2L, 
2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 
1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 
1L, 1L, 1L), .Label = c("Control", "Knockout"), class = "factor"), 
    relative_wound_healing = c(1, 1, 1, 1, 1, 1, 0.819981, 0.78227, 
    0.811902, 0.873852, 0.893572, 0.910596, 0.39819, 0.436948, 
    0.559486, 0.534719, 0.591295, 0.612154, 0.222731, 0.2592, 
    0.453575, 0.37238, 0.477891, 0.505393, 0.05243246, 0.0809449, 
    0.2108063, 0.261122, 0.3750218, 0.4129873, 0, 0.0240122, 
    0.0778219, 0.0806758, 0.2495444, 0.3203724)), class = "data.frame", row.names = c(NA, 
-36L))

图表的外观图片:

4

2 回答 2

1

x 尺度具有比例差距,因为 'ggplot2' 认为这些值是连续的而不是分类的。

为了使其分类,您可以例如使用因子:

aes(x = factor(timepoint, ordered = TRUE), …

(没有ordered = TRUE, 'ggplot2' 假定按字母顺序排列,所以它会放在11before 5,这可能不是你想要的。)

要修复条形高度,您需要计算并绘制一个汇总统计量 - 'ggplot2' 允许您使用stat_summary(而不是geom_col)执行此操作:

stat_summary(fun.y = mean, geom = "col", position = position_dodge())

综合起来:

ggplot(ny_dataframe_scratch) +
    aes(x = factor(timepoint, ordered = TRUE), y = relative_wound_healing, fill = Condition) +
    scale_fill_manual(values = c("palevioletred4", "slategray")) +
    stat_summary(fun.y = mean, geom = "col", position = position_dodge()) +
    geom_point(position = position_dodge(width = 1)) +
    labs(x = "Time point, hours", y = "Relative scratch area")
于 2021-04-11T14:18:07.493 回答
0

您的时间点是“数字”。尝试强迫他们考虑因素。此时, ggplot 应该将它们绘制成彼此等距的图。

xy$timepoint <- as.factor(xy$timepoint)
于 2021-04-11T14:00:16.647 回答