R语言 如何分开两次堆叠酒吧在对方的顶部?

zvokhttg  于 2023-01-10  发布在  其他
关注(0)|答案(3)|浏览(146)

我试着用板条箱做一个堆叠条形图,但是前两个条形图是在对方的顶部而不是在对方的旁边。其他条形图是正确的。

我想把它们分开,这样我就可以把12个月作为当前11个月的条形图(在x轴上)。
我检查了是否有相同的值,但事实并非如此。

ggplot(rawdata4.1, aes(x= Monat, y= Ankünfte, fill= Jahreszahl)) +
  facet_wrap(~ Herkunft, scale = "fixed") +
  geom_bar(position = "stack",
           colour = "black",
            lwd = 0.9,
           linetype = 1,
           stat = "identity",) +
  scale_y_continuous(name= NULL, labels = label_number(suffix = " Mio", scale = 1e-6)) +
  scale_x_binned(name = NULL) +
  ggtitle("Monatliche Ankünfte vor und nach Corona") +
  theme_pander() +
  theme(axis.title.y = element_text(vjust = +1.75), legend.position = "right") +
  scale_fill_economist()

下面是两个图片的整个和最终的数据集和我目前的代码和图片我的数据。

nqwrtyyt

nqwrtyyt1#

您可以使用将中断设置为1:12的scale_x_continuous

library(ggplot2)
library(ggthemes)
library(scales)

ggplot(rawdata4.1, aes( x= Monat, y = Ankünfte, fill = Jahreszahl)) +
  facet_wrap(~ Herkunft, scale = "fixed") +
  geom_col(position = "stack", colour = "black", lwd = 0.9, linetype = 1) +
  scale_y_continuous(labels = label_number(suffix = " Mio", scale = 1e-6),
                     name = NULL) +
  scale_x_continuous(NULL, breaks = 1:12) +
  scale_fill_economist() +
  ggtitle("Monatliche Ankünfte vor und nach Corona") +
  theme_pander() +
  theme(axis.title.y = element_text(vjust = +1.75), 
        legend.position = "right")

以可复制格式从问题中转录的数据

rawdata4.1 <- data.frame(Jahreszahl = factor(rep(c(2019, 2020), each = 24)),
                 Monat = rep(rep(1:12, each = 2), 2),
                 Herkunft = rep(c("Schweiz", "Alle Länder"), 24),
                 Ankünfte = c(647315, 1226739, 698093, 1332892, 774956, 1526592, 
                              623656, 1384180, 697684, 1624902, 858811, 2006805, 
                              987515, 2284423, 1011069, 2270490, 926890, 
                              1958831,  78940, 1612160, 589729, 1154630, 678702,
                              1381913, 707576, 1339067, 742885, 1356936, 296702, 
                              498459, 43310, 53920, 289738, 318331, 606987, 
                              725924, 1229947, 1574105, 1106300, 1545573, 
                              1021787, 1304570, 811214, 957811, 353547, 424907, 
                              484612, 603825))
4ktjp1zp

4ktjp1zp2#

问题是您使用了scale_x_binned。删除它并将Monat列转换为factor应该可以解决您的问题。
使用一些虚假的示例数据,让我们首先重现您的问题:

set.seed(123)

rawdata4.1 <- data.frame(
  Jahreszahl = rep(2019:2020, each = 24), 
  Monat = rep(1:12, each = 2),
  Herkunft = c("Schweiz", "Alle Länder"), 
  Ankünfte = runif(48, 10000, 999999)
)
rawdata4.1$Jahreszahl <- factor(rawdata4.1$Jahreszahl)

library(ggplot2)
library(ggthemes)

ggplot(rawdata4.1, aes(x = Monat, y= Ankünfte, fill= Jahreszahl)) +
  facet_wrap(~ Herkunft, scale = "fixed") +
  geom_bar(position = "stack",
           colour = "black",
           lwd = 0.9,
           linetype = 1,
           stat = "identity",) +
  scale_y_continuous(name= NULL, labels = scales::label_number(suffix = " Mio", scale = 1e-6)) +
  scale_x_binned(name = NULL) +
  ggtitle("Monatliche Ankünfte vor und nach Corona") +
  theme_pander() +
  theme(axis.title.y = element_text(vjust = +1.75), legend.position = "right") +
  scale_fill_economist()
#> Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
#> ℹ Please use `linewidth` instead.

现在,删除scale_x_binned并转换为factor将得到所需的结果:

ggplot(rawdata4.1, aes(x = factor(Monat), y= Ankünfte, fill= Jahreszahl)) +
  facet_wrap(~ Herkunft, scale = "fixed") +
  geom_bar(position = "stack",
           colour = "black",
           lwd = 0.9,
           linetype = 1,
           stat = "identity",) +
  scale_y_continuous(name= NULL, labels = scales::label_number(suffix = " Mio", scale = 1e-6)) +
  ggtitle("Monatliche Ankünfte vor und nach Corona") +
  theme_pander() +
  theme(axis.title.y = element_text(vjust = +1.75), legend.position = "right") +
  scale_fill_economist()

vtwuwzda

vtwuwzda3#

像这样?

df <- tibble(
  year = rep(2019:2020, each = 24) %>% factor, 
  month = rep(rep(1:12,each = 2), 2) %>% factor,
  group = rep(c("Schweiz", "Alle lander"), 24), 
  value = sample(10000:999999, replace = TRUE, 48)
)

df %>% 
  ggplot() + 
  aes(x = month, y = value, fill = year) + 
  geom_col(position = "dodge") + 
  facet_wrap(~ group) + 
  scale_fill_economist() + 
  theme_pander()

相关问题