gpt4 book ai didi

r - ggplot : limit axis limits/breaks of individual facet

转载 作者:行者123 更新时间:2023-12-05 06:20:10 25 4
gpt4 key购买 nike

我经常制作条形图,并将条形图的值额外包含为注释 (geom_text)。通常,我更喜欢这些值右对齐(与将标签放在条形顶部相反)。在绘制多面条形图时,我将这些值放在每个组中的最大值(我之前计算过)加上我通过乘以 x 值添加的一点额外空间(我不使用 nudge_x 因为它的绝对值可能适合一些方面,但不是其他方面)。

这种方法让我烦恼的是注释下的剩余轴标签。请参见下图(轴标签 15、100 和 2.5)。我想将 x 轴标签限制为(接近于)每个方面的最大值,而不是一直延伸到注释。

我想知道是否有更好的方法可用。

(我知道我可以使用 group_split 和例如 patchwork 生成所需的图形。我感兴趣的是是否有直接的方法来限制每个单独方面的轴限制/标签)。

非常感谢。

library(tidyverse)
#> Warning: package 'dplyr' was built under R version 3.6.2
#> Warning: package 'forcats' was built under R version 3.6.3

mtcars %>%
group_by(cyl, gear) %>%
summarise(n_obs=n()) %>%
mutate(n_obs=case_when(gear==4 ~ n_obs*100,
TRUE ~ as.numeric(n_obs))) %>%
group_by(gear) %>%
mutate(n_obs_max=max(n_obs, na.rm=T)) %>%
ggplot()+
geom_bar(aes(y=cyl,
x=n_obs),
stat="identity")+
geom_text(aes(y=cyl,
x=n_obs_max*1.20,
label=n_obs))+
facet_wrap(vars(gear),
scales="free_x")

reprex package 创建于 2020-03-08 (v0.3.0)

更新

继下面@stafan 的有用回答之后,这里对我的问题进行了修改和部分回答。

传递给 breaks 参数的函数

  my_breaks <- function(x) {

#calculates the max value on the x axis for each facet
new_x=max(x)

#adjusts this max value for a) the extension of the x axis by the
#expand=expansion(mult=c(0, 0.3)) which was needed to have enough space
#for the annotation; and the factor added to the position of the
#annotations with x=max_n_obs*1.10; the result is the maximum value
#of the bars in each facet;
old_max <- new_x/1.3/1.1

#create 5 labels; the maximum is the highest bar in each facet
my_pretty=labeling::extended(0, old_max, m=5)

#round these values
my_pretty=signif(my_pretty, digits=-2)

#remove the highest label(s)
my_pretty=head(unique(my_pretty), -1)

#combine the remaining labels and the maximum value of the highest bar
my_pretty=c(my_pretty, old_max)
my_pretty
}

应用于我的(修改后的)示例,这产生了我一直在寻找的东西(见下图)。

library(tidyverse)
#> Warning: package 'dplyr' was built under R version 3.6.2
#> Warning: package 'forcats' was built under R version 3.6.3

my_breaks <- function(x) {
new_x=max(x)
old_max <- new_x/1.2/1.05
#old_max
my_pretty=labeling::extended(0, old_max, m=5)
my_pretty=signif(my_pretty, digits=-2)
my_pretty=head(unique(my_pretty), -1)
my_pretty=c(my_pretty, old_max)
my_pretty

}

mtcars %>%
group_by(cyl, gear) %>%
summarise(n_obs=n()) %>%
mutate(n_obs=case_when(gear==4 ~ n_obs*100,
TRUE ~ as.numeric(n_obs))) %>%
group_by(gear) %>%
mutate(n_obs_max=max(n_obs, na.rm=T)) %>%
ggplot()+
geom_bar(aes(y=cyl,
x=n_obs),
stat="identity")+
geom_text(aes(y=cyl,
x=n_obs_max*1.20,
label=n_obs))+
scale_x_continuous(breaks=my_breaks1,
expand=expansion(mult=c(0, 0.05)))+
facet_wrap(vars(gear),
scales="free_x")

此函数的缺点是比例扩展值 (1.3) 和标签定位因子 (1.1) 被“硬编码”到函数中。方便的是在 ggplot scale 命令中传递函数时指定这些值,例如

scale_x_continuous(breaks=my_breaks(expansion=1.3, pos.factor=1.1))

不幸的是,我还没有弄清楚这是如何工作的。

reprex package 创建于 2020-03-09 (v0.3.0)

最佳答案

试试这个。

  1. 我扩展了 y 轴。
  2. 我调整了休息时间。我借用了here的总体思路.函数 my_breaks 返回 pretty_breaks 但删除最后一个值。

(注意:我也切换了美学,y = nobs 和 x = cyl 并使用了 coord_flip,因为在我的机器上运行你的代码没有重现你的情节(ggplot 3.3.0)):

library(tidyverse)
#> Warning: package 'forcats' was built under R version 3.6.3

my_breaks <- function(x, n = 5, drop = 2) {
breaks <- seq(x[[1]], x[[2]], length.out = n)
breaks <- scales::pretty_breaks()(breaks)
breaks <- breaks[1:(length(breaks) - drop)]
breaks
}

mtcars %>%
group_by(cyl, gear) %>%
summarise(n_obs = n()) %>%
mutate(n_obs = case_when(
gear == 4 ~ n_obs * 100,
TRUE ~ as.numeric(n_obs))) %>%
group_by(gear) %>%
mutate(n_obs_max = max(n_obs, na.rm=T)) %>%
ggplot(aes(x = cyl))+
geom_bar(aes(y = n_obs), stat="identity")+
geom_text(aes(y = n_obs_max * 1.2, label = n_obs))+
facet_wrap(vars(gear), scales = "free_x") +
scale_y_continuous(breaks = function(x) my_breaks(x, 5, 2),
expand = expand_scale(mult = c(0.05, .2))) +
coord_flip()
#> Warning: `expand_scale()` is deprecated; use `expansion()` instead.

reprex package 创建于 2020-03-09 (v0.3.0)

关于r - ggplot : limit axis limits/breaks of individual facet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60585888/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com