Barplot for count data using ggplot2

Hi there,

I am trying to create a bar plot using ggplot2 in R studio so that it looks like the following snapshot:

I would like to create a bar plot of the variable called "Region" where it plots the the different levels of the Region variable on the x-axis and the percentage on the y-axis.

I have created a variable "Region_Percentage" which you can see from the R code creates a table of the different levels of the Region variable in percentages.

Using gglpot2, I tried to then create the bar plot of the variable "Region_Percentage" as it already has the percentages calculates but it hasn't worked.

I have tried to create a reprex below:

head(Dataset1, 20)[c('Region')]
#> Error in head(Dataset1, 20): object 'Dataset1' not found

data.frame(
      Region = as.factor(c("Wellington", "Bay of Plenty", "Bay of Plenty",
                           "Manawatu-Wanganui", "Auckland", "Wellington",
                           "Canterbury", "Northland", "Canterbury", "Canterbury",
                           "Canterbury", "Wellington", "Hawke's Bay",
                           "Bay of Plenty", "Waikato", "Manawatu-Wanganui", "Canterbury",
                           "Wellington", "Auckland", "Manawatu-Wanganui")),
      Region = as.factor(c("Wellington", "Bay of Plenty", "Bay of Plenty",
                           "Manawatu-Wanganui", "Auckland", "Wellington",
                           "Canterbury", "Northland", "Canterbury", "Canterbury",
                           "Canterbury", "Wellington", "Hawke's Bay",
                           "Bay of Plenty", "Waikato", "Manawatu-Wanganui", "Canterbury",
                           "Wellington", "Auckland", "Manawatu-Wanganui"))
)
#>               Region          Region.1
#> 1         Wellington        Wellington
#> 2      Bay of Plenty     Bay of Plenty
#> 3      Bay of Plenty     Bay of Plenty
#> 4  Manawatu-Wanganui Manawatu-Wanganui
#> 5           Auckland          Auckland
#> 6         Wellington        Wellington
#> 7         Canterbury        Canterbury
#> 8          Northland         Northland
#> 9         Canterbury        Canterbury
#> 10        Canterbury        Canterbury
#> 11        Canterbury        Canterbury
#> 12        Wellington        Wellington
#> 13       Hawke's Bay       Hawke's Bay
#> 14     Bay of Plenty     Bay of Plenty
#> 15           Waikato           Waikato
#> 16 Manawatu-Wanganui Manawatu-Wanganui
#> 17        Canterbury        Canterbury
#> 18        Wellington        Wellington
#> 19          Auckland          Auckland
#> 20 Manawatu-Wanganui Manawatu-Wanganui

Region <- as.factor(Dataset1$Region)
#> Error in is.factor(x): object 'Dataset1' not found
table(Region)
#> Error in table(Region): object 'Region' not found
Region_T <-table(Region)
#> Error in table(Region): object 'Region' not found
round(100 * prop.table(Region_T),digit = 1)
#> Error in prop.table(Region_T): object 'Region_T' not found
Region_Percentage <-round((prop.table(Region_T)*100),1)
#> Error in prop.table(Region_T): object 'Region_T' not found
Region_Percentage
#> Error in eval(expr, envir, enclos): object 'Region_Percentage' not found

library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 3.6.1
ggplot (data= Dataset1, aes(x= Region_Percentage)) +
  geom_bar() +
  labs (x = Region, title = "Regional demographics of data set")
#> Error in ggplot(data = Dataset1, aes(x = Region_Percentage)): object 'Dataset1' not found

Any help is greatly appreciated.

Thank you:-)

1 Like

Your example is not actually reproducible and not entirely clear, but I think this will get you started

library(tidyverse)
library(scales)

df <- data.frame(
    Region = as.factor(c("Wellington", "Bay of Plenty", "Bay of Plenty",
                         "Manawatu-Wanganui", "Auckland", "Wellington",
                         "Canterbury", "Northland", "Canterbury", "Canterbury",
                         "Canterbury", "Wellington", "Hawke's Bay",
                         "Bay of Plenty", "Waikato", "Manawatu-Wanganui", "Canterbury",
                         "Wellington", "Auckland", "Manawatu-Wanganui"))
)

df %>% 
    count(Region) %>% 
    mutate(prop = n/sum(n)) %>% 
    ggplot(aes(x = Region, y = prop)) +
    geom_col(fill = "#FF7F24") +
    geom_text(aes(label = percent(prop)), vjust = -1) +
    coord_cartesian(clip = "off") +
    scale_y_continuous(labels = percent_format()) +
    theme_minimal() +
    theme(axis.text.x = element_text(angle=45, hjust=1, vjust = 1),
          axis.title = element_blank(),
          plot.margin = margin(t = 20, r = 10, b = 10, l = 10))

4 Likes

Hi there,

Apologies for not creating a reprex for my example. I've created a reprex once before but I just couldn't seem to create it for this example.

Thank you for the code to create the required bar plot using ggplot2. I have given it a go and it works great with my data.

Many thanks:-)

If your question's been answered (even by you!), would you mind choosing a solution? It helps other people see which questions still need help, or find solutions if they have similar problems. Here’s how to do it:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.