[英]ggplot only show positive values on y-axis (faceted plot)
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
中的breaks参数,该函数返回长度为2的数字矢量。
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.