rggplot2graphbubble-chart

Axes will not start at 0 in ggplot


I have just created my first bubble chart in ggplot and, despite trying just about every 'solution' I could find on the internet for the past few hours, I have not been able to force the axes to start at 0. The x and y axes both start at 10 but I would like the axes to start at 0. I have tried using various combination of xlim, ylim, expand_limits, scale_y_continuous and scale_x_continuous with no luck.

Here's the code I use to create the graph:

ggplot(mean_diffs, aes(x=sample, y=marked, size = pop_est)) + 
  geom_point(alpha=0.7) + scale_size(range = c(2, 10), name="Absolute error", breaks = c(5,10,20,40))

The graph


Solution

  • It's not clear what exactly you tried, but the following seems to work well, assuming your data are all numeric:

    ggplot(mean_diffs, aes(x = sample, y = marked, size = pop_est)) + 
      geom_point(alpha = 0.7) + 
      scale_size(range = c(2, 10), name = "Absolute error", 
                 breaks = c(5, 10, 20, 40)) +
      scale_x_continuous(limits = c(0, 60), breaks = 0:6 * 10) +
      scale_y_continuous(limits = c(0, 60), breaks = 0:6 * 10)
    

    enter image description here


    Reproducible data set, inferred from plot in question

    mean_diffs <- data.frame(sample = rep(c(10, 30, 50), each = 3),
                             marked = rep(c(10, 30, 50), 3),
                             pop_est = c(30, 20, 10, 40, 20, 3, 30, 7, 3))