ggplot:如何在不同方面获得相同的条宽

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

我通常必须创建与以下相似的图。结果使我有些恼怒的是刻面B中条的位置和大小/(bin)宽度。

理想情况下,我想使小节与小平面A中的c的杆位于同一水平/高度,并且剩余的两个“空格”(a和b位于小面A中)在小平面B中保持空白。

library(tidyverse)

df <- tibble::tribble(
  ~group, ~unit, ~value,
     "A",   "a",    10L,
     "A",   "b",    15L,
     "A",   "c",    20L,
     "A",   "c",    25L,
     "B",   "d",    10L
  )

df %>% 
  ggplot()+
  geom_bar(aes(x=unit,
               y=value),
           stat="identity")+
  facet_wrap(vars(group),
             scales = "free_y")+
  coord_flip()

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

reprex package(v0.3.0)在2020-05-26创建

[我知道我可以使用scales="free"或使用patchworkcowplot包等。但是据我所知,这些方法都无法产生期望的结果。

有什么想法吗?非常感谢!

r ggplot2 facet-wrap
1个回答
2
投票

我唯一想到的方法是对条的宽度进行硬编码:

ggplot(df, aes(x = unit, y = value))+
  geom_col(width = c(0.9, 0.9, 0.9, 0.9, 0.35)) +
  facet_wrap(vars(group), scales = 'free_y')+
  coord_flip()

enter image description here

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