3

I have a dataframe with the following information:

Year      Total Population         UK         EEA          NON-EEA
2007          60510               54102       1999          4409
2008          60995               54225       2154          4615
2009          61437               54415       2235          4787
2010          61933               54699       2331          4903
2011          62448               54787       2580          5080 
2012          62864               55042       2671          5151
2013          63230               55309       2762          5160
2014          63653               55375       3042          5236
2015          64212               55642       3204          5365

How can I create a graph in which I could see the evolution of Total Population per year, but showing UK, EEA and Non-EEA population too, because, when we sum up all 3 variables, we have the data from Total Population column.

Additionaly, I have used this code:

dat <- read.table(text = "Total_Population  United_Kingdom   EEA   Non_EEA
                  2007 60510 54102 1999 4409
                  2008 60995 54225 2154 4615
                  2009 61437 54415 2235 4787
                  2010 61933 54699 2331 4903
                  2011 62448 54787 2580 5080 
                  2012 62864 55042 2671 5151
                  2013 63230 55309 2762 5160
                  2014 63653 55375 3042 5236
                  2015 64212 55642 3204 5365", header = TRUE)

library(reshape2)

dat$row <- seq_len(nrow(dat))
dat2 <- melt(dat, id.vars = "row")

library(ggplot2)

ggplot(dat2, aes(x = variable, y = value, fill = row)) + 
  geom_bar(stat = "identity") +
  xlab("\nCountry of Birth") +
  ylab("Population\n") +
  guides(fill = FALSE) +
  theme_bw()

It gives me the barplot, but I was thinking to have something more sophisticated.

Chris
  • 45
  • 6
  • 1
    you could use stacked barplots. Take a look at https://stackoverflow.com/questions/20349929/stacked-bar-plot-in-r and https://stackoverflow.com/questions/54511958/can-this-chart-be-created-in-r-using-ggplot2 – boski May 06 '19 at 08:51
  • @boski, Thank you, I tried to use the code, but it gives me an error. Could you please sugest me how can I solve it? Moreover, I am not 100% sure about the modification that I made in the code :) – Chris May 06 '19 at 09:06
  • I think you should `reshape/gather/pivot` your data from wide to long to make life easier. – NelsonGon May 06 '19 at 09:09
  • @NelsonGon, thank you, but how can I do this? – Chris May 06 '19 at 09:14
  • 2
    The error is from `gather(Year, value, 2007:2009)`. It should probably be `gather(group, value, -Year)` – JohannesNE May 06 '19 at 09:14
  • @JohannesNE, I do not know, anyway is not working :( I would like to have the barplot from the code, but I really do not know how to manipulate it to get the result that I want. – Chris May 06 '19 at 09:32
  • Could you make some kind of sample plot you would like to make and add it to the question? – NelsonGon May 06 '19 at 09:39
  • @NelsonGon, yes, I have changed my code from the question, could you please take a look? Mostly I wanted to have a barplot like in the link: (http://stackoverflow.com/questions/20349929/stacked-bar-plot-in-r – Chris May 06 '19 at 09:46
  • Does this work for you: `dat %>% mutate(Year=row.names(.)) %>% gather(key,value,-c(Year,Total_Population)) %>% ggplot(aes(Year,Total_Population,fill=key))+ geom_bar(stat="identity")`? – NelsonGon May 06 '19 at 09:55
  • 1
    @NelsonGon Yeeees, it works for me, it is more clear and accurate. Thank you so much. You can answer to the question, and I will validate it!!! Thank you once again!! – Chris May 06 '19 at 10:02
  • I'm not sure about the `row` section. I have not used this code as part of the possible solution: `dat$row <- seq_len(nrow(dat)) dat2 <- melt(dat, id.vars = "row")` – NelsonGon May 06 '19 at 10:09
  • 1
    @NelsonGon, yes, it was my mistake, but I corrected it and now it shows me exactly what I needed. Thank you once again! I appreciate a lot!!! – Chris May 06 '19 at 10:13
  • 1
    Glad you got it working! – NelsonGon May 06 '19 at 10:14

1 Answers1

3

We could use the following:

library(dplyr)
library(ggplot2)
library(tidyr)
dat %>% 
mutate(Year=row.names(.)) %>% 
gather(key,value,-c(Year,Total_Population)) %>%   
ggplot(aes(Year,Total_Population,fill=key))+ 
geom_bar(stat="identity")

Alternatively:

dat %>% 
  mutate(Year=row.names(.)) %>% 
  gather(key,value,-c(Year,Total_Population)) %>%   
  ggplot(aes(Year,value,fill=key))+ 
  geom_bar(stat="identity",position="dodge")

Data:

dat<-structure(list(Total_Population = c(60510L, 60995L, 61437L, 61933L, 
62448L, 62864L, 63230L, 63653L, 64212L), United_Kingdom = c(54102L, 
54225L, 54415L, 54699L, 54787L, 55042L, 55309L, 55375L, 55642L
), EEA = c(1999L, 2154L, 2235L, 2331L, 2580L, 2671L, 2762L, 3042L, 
3204L), Non_EEA = c(4409L, 4615L, 4787L, 4903L, 5080L, 5151L, 
5160L, 5236L, 5365L)), class = "data.frame", row.names = c("2007", 
"2008", "2009", "2010", "2011", "2012", "2013", "2014", "2015"
))
NelsonGon
  • 13,015
  • 7
  • 27
  • 57