繁体   English   中英

如何用 2 个变量绘制直方图

[英]How to plot histogram with 2 variables

我有以下数据集

    date        region     Nr_individuals     Povertydecile
  01-01-2019      1              80                2
  01-01-2019      1              23                3
  01-01-2019      1              2                 4
  01-01-2019      2              100               1
  01-01-2019      2              60                2
  01-01-2019      3              20                8 
  01-01-2019      3              50                10
  01-04-2019      1              77                1
  01-04-2019      1              20                2
  01-04-2019      1              5                 3
  01-04-2019      2              89                1
  01-04-2019      2              78                3
  01-04-2019      3              16                8 
  01-04-2019      3              55                9

如何在此设置中绘制每个regiondate的直方图,其中我有 2 个变量Nr_individualsPovertydecile

不太确定是否有正确的答案,以下是一个建议:

ggplot(df,aes(x=factor(Povertydecile),y=Nr_individuals)) + 
geom_col() + facet_grid(region ~ date)

在此处输入图片说明

这是我从您的表中获取的数据:

df = structure(list(date = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("01-01-2019", "01-04-2019"
), class = "factor"), region = c(1L, 1L, 1L, 2L, 2L, 3L, 3L, 
1L, 1L, 1L, 2L, 2L, 3L, 3L), Nr_individuals = c(80L, 23L, 2L, 
100L, 60L, 20L, 50L, 77L, 20L, 5L, 89L, 78L, 16L, 55L), Povertydecile = c(2L, 
3L, 4L, 1L, 2L, 8L, 10L, 1L, 2L, 3L, 1L, 3L, 8L, 9L)), class = "data.frame", row.names = c(NA, 
-14L))

另一种方法是躲避一个变量并刻画另一个变量:

ggplot(df, aes(x = as.factor(Povertydecile), y = Nr_individuals)) +
  geom_col(aes(fill = factor(region)), 
           position = position_dodge(preserve = "single")) + 
  scale_fill_manual(values = c("indianred3", "deepskyblue3", "gold")) +
  facet_grid(date ~ .) +
  labs(fill = "Region", x = "Poverty decile", y = "Individuals") +
  theme_bw() +
  theme(strip.background = element_blank(),
        strip.text = element_text(size = 16),
        panel.border = element_blank(),
        legend.position = "bottom")

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM