Forcing X-axis on ggplot to integer values

I am working with the R programming language. I downloaded the following data on world populations and tried to make a histogram:

#data source: https://population.un.org/wpp/Download/Files/1_Indicators%20(Standard)/CSV_FILES/WPP2019_TotalPopulationBySex.csv

`WPP2019_TotalPopulationBySex.(1)` <- read.csv("C:/Users/ME/Downloads/WPP2019_TotalPopulationBySex (1).csv")

world_data = WPP2019_TotalPopulationBySex.(1)

> head(world_data)
  LocID    Location VarID Variant Time MidPeriod  PopMale PopFemale PopTotal PopDensity
1     4 Afghanistan     2  Medium 1950    1950.5 4099.243  3652.874 7752.117     11.874
2     4 Afghanistan     2  Medium 1951    1951.5 4134.756  3705.395 7840.151     12.009
3     4 Afghanistan     2  Medium 1952    1952.5 4174.450  3761.546 7935.996     12.156
4     4 Afghanistan     2  Medium 1953    1953.5 4218.336  3821.348 8039.684     12.315
5     4 Afghanistan     2  Medium 1954    1954.5 4266.484  3884.832 8151.316     12.486
6     4 Afghanistan     2  Medium 1955    1955.5 4318.945  3952.047 8270.992     12.669


world_data$PopMale = as.numeric(substr(world_data$PopMale,1,1))
world_data$PopFemale = as.numeric(substr(world_data$PopFemale,1,1))
world_data$PopTotal = as.numeric(substr(world_data$PopTotal,1,1))
world_data$PopDensity = as.numeric(substr(world_data$PopDensity,1,1))

I tried to make the histogram:

library(ggplot2)
library(scales)
library(cowplot2)

g1 = ggplot(world_data, aes(x=PopMale)) + geom_histogram() +   + ggtitle("Male Population") 
g2 = ggplot(world_data, aes(x=PopFemale)) + geom_histogram() +  ggtitle("Female Population")
g3 = ggplot(world_data, aes(x=PopTotal)) + geom_histogram() + ggtitle("Total Population")
g4 = ggplot(world_data, aes(x=PopDensity)) + geom_histogram()+  ggtitle("Population Density")


plot_row = plot_grid(g1, g2, g3, g4)

# now add the title
title <- ggdraw() + 
  draw_label(
    "World Population: Does Benford's Law Exist?",
    fontface = 'bold',
    x = 0,
    hjust = 0
  ) +
  theme(
    # add margin on the left of the drawing canvas,
    # so title is aligned with left edge of first plot
    plot.margin = margin(0, 0, 0, 7)
  )
plot_grid(
  title, plot_row,
  ncol = 1,
  # rel_heights values control vertical title margins
  rel_heights = c(0.1, 1)
)

enter image description here

Question: I am trying to format the x-axis of the histograms so that they display integers (e.g. 0,1,2,3,4,5,6,7,8,9). I tried to use the following code to do this:

integer_breaks <- function(n = 5, ...) {
fxn <- function(x) {
breaks <- floor(pretty(x, n, ...))
names(breaks) <- attr(breaks, "labels")
breaks
}
return(fxn)
}


library(ggplot2)

g1 = ggplot(world_data, aes(x=PopMale)) + geom_histogram() +    scale_x_continuous(breaks = integer_breaks()) + ggtitle("Male Population") 
g2 = ggplot(world_data, aes(x=PopFemale)) + geom_histogram() +  scale_x_continuous(breaks = integer_breaks()  +  ggtitle("Female Population")
g3 = ggplot(world_data, aes(x=PopTotal)) + geom_histogram() +   scale_x_continuous(breaks = integer_breaks() + ggtitle("Total Population")
g4 = ggplot(world_data, aes(x=PopDensity)) + geom_histogram()+  scale_x_continuous(breaks = integer_breaks() + ggtitle("Population Density")


plot_row = plot_grid(g1, g2, g3, g4)

# now add the title
title <- ggdraw() + 
  draw_label(
    "World Population: Does Benford's Law Exist?",
    fontface = 'bold',
    x = 0,
    hjust = 0
  ) +
  theme(
    # add margin on the left of the drawing canvas,
    # so title is aligned with left edge of first plot
    plot.margin = margin(0, 0, 0, 7)
  )
plot_grid(
  title, plot_row,
  ncol = 1,
  # rel_heights values control vertical title margins
  rel_heights = c(0.1, 1)
)

Problem: But this is still displaying the x-axis as before.

Can someone please show me how to fix this problem?

Thanks!

you dropped bracket early here and scale_x_continous is swallowing your ggtitle.
but that aside, scale_x_continuous(breaks = integer_breaks() ) is equivalent to scale_x_continuous(breaks = integer_breaks(5) based on your default and setting scale_x_continuous(breaks = integer_breaks(10) seems necessary for your requirement

However, I think the chart could be improved further by a different approach. Since your x axis is not representative of continous data (rather it is a discrete axis and intended as such. converting to factor and using the goem_bar is both succinct and gives a more pleasing result to my eyes.

world_data$PopMale = factor(substr(world_data$PopMale,1,1))
ggplot(world_data, aes(x=PopMale)) + geom_bar()   + ggtitle("Male Population")
1 Like

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.