如何用 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

如何在我有 2 个变量 Nr_individualsPovertydecile 的设置中按 regiondate 绘制直方图?

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

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

这是我从你那里得到的数据 table:

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")

在没有 ggplot 的情况下试试这个:

'''set a starting point to find the same result because we use a simulation of the random normal law'''

set.seed(10)

'here our data'

x1 = rnorm(1000, mean=0.5, sd=0.1)

x2 = rnorm(1000, mean=0.3, sd=0.1)

'here for plot two histograms in same graph'

hist(x1, col='blue')

hist(x2, col='green', add=TRUE)