library(tidyverse)
mpg2 <- mpg %>% mutate(hwy = hwy - 30)
ggplot(mpg2, aes(cty, hwy)) +
geom_point() +
facet_grid(year ~ fl, scales = "free") +
scale_y_continuous(expand = expand_scale(mult = 2))
随着代码块以上,我想同时做三件事情:
(-)
负y轴标签(在我的例子中你需要删除-40
,-30
和-60
标签)。我只想零和积极的标签显示。scales = "free"
我该怎么做?
我们可以通过一个函数来在scale_y_continuous
的断裂参数,返回长度为二的数值向量,在这种情况下。
library(ggplot2); library(dplyr)
mpg2 <- mpg %>% mutate(hwy = hwy - 30)
my_breaks <- function(x) c(0, (((max(x) / 2) %/% 10) + 1) * 10)
该函数输出0
和(((max(x) / 2) %/% 10) + 1) * 10
这给OP的希望的输出。上断裂是最大y
通过2
划分和“向上舍入”到10
的下一个更大的倍数。
例
my_breaks(67)
# [1] 0 40
情节
ggplot(mpg2, aes(cty, hwy)) +
geom_point() +
facet_grid(year ~ fl, scales = "free") +
scale_y_continuous(expand = expand_scale(mult = 2),
breaks = my_breaks)