如何更正 geom_col 中条形之间的不同距离
How to correct a different distance between bars in geom_col
我正在 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))
图表的图片:
您的时间点是“数字”。尝试强迫他们考虑因素。那时,ggplot 应该将它们绘制成彼此等距。
xy$timepoint <- as.factor(xy$timepoint)
x 尺度具有比例间隙,因为“ggplot2”将值视为连续值而不是分类值。
要使其明确化,您可以使用因子:
aes(x = factor(timepoint, ordered = TRUE), …
(没有 ordered = TRUE
,'ggplot2' 假定按字母顺序排列,因此它会将 11
放在 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")
我正在 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))
图表的图片:
您的时间点是“数字”。尝试强迫他们考虑因素。那时,ggplot 应该将它们绘制成彼此等距。
xy$timepoint <- as.factor(xy$timepoint)
x 尺度具有比例间隙,因为“ggplot2”将值视为连续值而不是分类值。
要使其明确化,您可以使用因子:
aes(x = factor(timepoint, ordered = TRUE), …
(没有 ordered = TRUE
,'ggplot2' 假定按字母顺序排列,因此它会将 11
放在 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")