'How to plot histogram with 2 variables
I have the following dataset
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
How to plot an histogram per region
and date
in this setting where I have 2 variables Nr_individuals
and Povertydecile
?
Solution 1:[1]
Not very sure if there's a correct answer to this, below is a suggestion:
ggplot(df,aes(x=factor(Povertydecile),y=Nr_individuals)) +
geom_col() + facet_grid(region ~ date)
This is the data I took from your 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))
Solution 2:[2]
An alternative would be to dodge one variable and facet another:
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")
Solution 3:[3]
Try this without 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)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | StupidWolf |
Solution 2 | Allan Cameron |
Solution 3 | Martin Gal |