ggplot仅显示在y轴(刻面积)的正值

问题描述 投票:1回答:1
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))

随着代码块以上,我想同时做三件事情:

  1. 不显示任何(-)负y轴标签(在我的例子中你需要删除-40-30-60标签)。我只想零和积极的标签显示。
  2. 保持scales = "free"
  3. 保持规模的不断扩大,以及

我该怎么做?

facet delete negative

r ggplot2
1个回答
2
投票

我们可以通过一个函数来在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)

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.