如何设置切面时显示的y轴值的数量

问题描述 投票:1回答:1

我有一个多面条形图。

 dat <- data.frame(ID = c("A", "A", "B", "B", "C", "C"),
                   A = c("Type 1", "Type 2", "Type 1", "Type 2", "Type 1", "Type 2"),
                   B = c(1, 2, 53, 87, 200, 250))

 ggplot(data = dat, aes(x = A, y = B)) + 
   geom_bar(stat = "identity") +
   facet_wrap(~ID, scales= "free_y")

如何编码,每个图形仅显示3个y轴值?

我已经尝试过

   +scale_y_continuous(breaks=3)
ggplot2 scale facet axis-labels yaxis
1个回答
0
投票

为了更好地控制休息,您可以编写自己的休息功能。以下代码为您提供了3个中断。但是,这种非常基本的方法不一定会导致“漂亮的”中断:

library(ggplot2)

my_breaks <- function(x) { 
  seq(0, round(max(x), 0), length.out = 3) 
} 

my_limits <- function(x) { 
  c(x[1], ceiling(x[2]))
}

# Dataset 2
dat <- data.frame(
  ID = c("A", "A", "A","B", "B", "B", "C", "C", "C"), 
  A = c("Type 1", "Type 2", "Type 3", "Type 1", "Type 2", "Type 3","Type 1", "Type 2", "Type 3"), 
  B = c(1.7388, 4.2059, .7751, .9489, 2.23405, .666589, 0.024459, 1.76190, 0.066678)) 

ggplot(data = dat, aes(x = A, y = B)) + 
  geom_bar(stat = "identity") + 
  facet_wrap(~ID, scales= "free_y") + 
  scale_y_continuous(breaks = my_breaks, limits = my_limits)

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLmltZ3VyLmNvbS9TelNHSnhJLnBuZyJ9” alt =“”>

# Dataset 1
dat <- data.frame(ID = c("A", "A", "B", "B", "C", "C"),
                  A = c("Type 1", "Type 2", "Type 1", "Type 2", "Type 1", "Type 2"),
                  B = c(1, 2, 53, 87, 200, 250))

ggplot(data = dat, aes(x = A, y = B)) + 
  geom_bar(stat = "identity") + 
  facet_wrap(~ID, scales= "free_y") + 
  scale_y_continuous(breaks = my_breaks, limits = my_limits)

“”

reprex package(v0.3.0)在2020-04-10创建

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