删除 ggplot 轴中的尾随零

Mat*_*att 2 r ggplot2

肯定有另一种方法可以去除 y 轴刻度标签中的尾随零,而不是用labels=c("0",...).

我在网格中绘制了许多图形,y 轴上都有尾随零,特别是零值(见图)。

在此处输入图片说明 如果我必须手动设置每个图中的所有标签,那将非常麻烦。

nei*_*fws 5

尝试将此添加到您的ggplot

+ scale_y_continuous(labels = function(x) ifelse(x == 0, "0", x))
Run Code Online (Sandbox Code Playgroud)

示例数据:

data.frame(x = 1:6, 
           y = seq(0, 0.05, 0.01)) %>% 
  ggplot(aes(x, y)) + 
  geom_point() + 
  scale_y_continuous(labels = function(x) ifelse(x == 0, "0", x))
Run Code Online (Sandbox Code Playgroud)

结果:

在此处输入图片说明