The easiest way to resolve this is with the ggplot2
package. But before you need to put the data in the so-called long format, using the melt
function of the reshape2
package:
library(reshape2)
dados_estado_civil <- melt(dados_estado_civil)
names(dados_estado_civil) <- c("Estado_Civil", "mes", "valor")
dados_estado_civil
Estado_Civil mes valor
1 AMIGADO Set 2494
2 CASADO Set 2500
3 SEPARADO Set 2504
4 SOLTEIRO Set 2504
5 AMIGADO Out 3857
6 CASADO Out 3291
7 SEPARADO Out 3545
8 SOLTEIRO Out 2910
Now I have a column with repeated values for the marital status, another column with all months and a third column with all values. This is important because ggplot2
understands the data in this format more easily. I can, for example, paint the bars of my chart according to the marital status:
library(ggplot2)
ggplot(dados_estado_civil, aes(x=mes, y=valor, fill=Estado_Civil)) +
geom_col(position="dodge") +
labs(x="Mês", y="Quantidade", fill="Estado Civil")
OR,ifIfinditbesttocomparetheevolutionfromonemonthtoanotherwithineachmaritalstatus,simplychangetheorderofx
andfill
:
ggplot(dados_estado_civil,aes(x=Estado_Civil,y=valor,fill=mes))+geom_col(position="dodge") +
labs(x="Estado Civil", y="Quantidade", fill="Mês")