如何在ggplot2中设置y轴和x轴的原点/截距?
x 轴的线应该正好在y=Z。
具有Z=0或其他给定值。
xlimylim不要在这里剪。您需要使用expand_limits、scale_x_continuous和scale_y_continuous。尝试:
df <- data.frame(x = 1:5, y = 1:5)
p <- ggplot(df, aes(x, y)) + geom_point()
p <- p + expand_limits(x = 0, y = 0)
p # not what you are looking for

p + scale_x_continuous(expand = c(0, 0)) + scale_y_continuous(expand = c(0, 0))

您可能需要稍作调整以确保点不会被截断(例如,请参见 和 处的x = 5点y = 5。
只需将这些添加到您的 ggplot 中:
+ scale_x_continuous(expand = c(0, 0), limits = c(0, NA)) +
scale_y_continuous(expand = c(0, 0), limits = c(0, NA))
df <- data.frame(x = 1:5, y = 1:5)
p <- ggplot(df, aes(x, y)) + geom_point()
p <- p + expand_limits(x = 0, y = 0)
p # not what you are looking for
p + scale_x_continuous(expand = c(0, 0), limits = c(0,NA)) +
scale_y_continuous(expand = c(0, 0), limits = c(0, NA))
最后,请注意不要无意中将数据排除在图表之外。例如,aposition = 'dodge'可能会导致条形图完全离开图表(例如,如果它的值为零并且您从零开始轴),因此您可能看不到它,甚至可能不知道它在那里。我建议先完整绘制数据,检查,然后使用上面的提示来提高绘图的美感。