0

I have data that includes the number of countries in each region per year in a data set. I have used the below code to plot a bar chart but I would like it to plot the numbers for each year but not sure how.

region_year <- data_na_removed%>%
 group_by(Region, Year)%>%
 summarise(Number = n())%>%
 arrange(desc(Number))

region_year%>%
 ggplot() +
 geom_col(aes(x = Region, y = Number), fill='lightblue') +
 labs(title = 'The Number of countries in each Region in 2014') +
 ylab('Number') + xlab('Region')

'''

stefan
  • 90,330
  • 6
  • 25
  • 51
  • You could try with `geom_col(aes(x = Region, y = Number, fill = factor(Year)), position = "dodge")` to show the different years via a dodged bar chart or another option would be to use facettting `+ facet_wrap(~Year)`. – stefan Nov 04 '22 at 10:17
  • Its not liking factor(Year) it says it can't find Year – Georgia Bingham Nov 04 '22 at 10:22
  • 1
    Hm. According to your code snippet there should be a column `Year` run your dataset `region_year`. So check that this is indeed the case. If you need more help then please provide [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a snippet of your data or some fake data. If you want to post your data type `dput(region_year)` into the console and copy the output starting with `structure(....` into your post. – stefan Nov 04 '22 at 10:27
  • There is defiantly a column called Year. – Georgia Bingham Nov 04 '22 at 10:35
  • Im not sure how to copy the data into the question – Georgia Bingham Nov 04 '22 at 10:35
  • Copy the "weird" looking output from your console as an edit into your post. And good practice would be to format is as code. – stefan Nov 04 '22 at 10:37
  • I have managed to sort it so it is one column with the multiple colours in but was aiming to have each year next to each other – Georgia Bingham Nov 04 '22 at 10:38
  • By default ´geom_col` will give you a stacked barchart. To change that use `geom_col(..., position = "dodge")`. That will you a dodged barchart with bars for the years placed side by side for each region – stefan Nov 04 '22 at 10:52

1 Answers1

0

What you want is facets, e.g. http://www.cookbook-r.com/Graphs/Facets_(ggplot2)/

simply add + facet_wrap(vars(Year))