Search code examples
rggplot2scaleaxes

Why does the top line of ggplot disappear when using xlim, ylim and scale_continuous to define breaks


I am using this piece of code to create a series of 4 point plots. I need the plots to go from x1 to x2 and y1 to y2. The length of the axes is always 10 with a single major break halfway and minor breaks at each unit.

When I get the scale and the breaks right I sometimes (not always) lose the top line on the box surrounding the map

The parts of the data set that are involved are

tag: an identifier used to label the points lx, ly: the coordinates of the plot which range from 0 -20 in the dataset but each map does only a quarter of the area. So x goes from 0 to 10 or 10 to 20 and y goes from 0 to 10 or 10 to 20.

I tried this piece of code. I expect a point plot surrounded by a box

n ranges from 1:4

    x1 = c(0, 10, 10, 0)
    x2 = c(10, 20, 20, 10)
    y1 = c(0, 0, 10, 10)
    y2 = c(10, 10, 20, 20)
    theme_set(theme_bw())
    ggplot(onemap, aes(x = onemap$lx, y = onemap$ly)) + geom_point(size =
                                                                     .3) +
      xlim(c(x1[n], x2[n])) +  ylim(c(y1[n], y2[n])) +
      #   coord_cartesian(expand = FALSE)
      #      scale_x_continuous(expand = c(0, 0), limits = c(0, NA)) +
      scale_y_continuous(breaks = seq(y1[n], y2[n], 5),
                         minor_breaks = seq(y1[n], y2[n], 1)) +
      scale_x_continuous(breaks = seq(x1[n], x2[n], 5),
                         minor_breaks = seq(x1[n], x2[n], 1)) +
      labs(
        x = element_blank(),
        y = element_blank(),
        title = hd,
        subtitle = subheads
      ) +
      theme(plot.title.position = "plot") +
      theme(
        plot.title = element_text(
          size = 14,
          face = "bold",
          margin = margin(8, 0, 8, 0)
        ),
        plot.subtitle = element_text(size = 10),
        plot.margin = margin(
          t = 1,
          r = 2,
          b = 1.5,
          l = 2,
          unit = "cm"
        ),
        axis.ticks = element_blank(),
        axis.text = element_text(size = 6),
        axis.text.y = element_text(angle = 90)
      ) +
      theme(
        panel.grid.major = element_line(color = "gray30", linewidth = .25),
        panel.grid.minor = element_line(
          color = "gray30",
          linewidth = .25,
          linetype = "dashed"
        ),
        panel.border = element_blank()
      ) +
      geom_text_repel(
        aes(label = tag),
        box.padding = 0.01,
        size = 2,
        xlim = c(-Inf, Inf),
        ylim = c(-Inf, Inf),
        max.overlaps = 45,
        segment.size = 2,
        segment.color = "grey"
      )

Solution

  • I worked out the problem. I should not have used xlim and ylim in the main body of ggplot. I should have used the limits in scale_x_continuous and scale_y_continuous as below.

      scale_y_continuous(breaks = seq(y1[n], y2[n], 5), minor_breaks = seq(y1[n], y2[n], 1),limits=c(y1[n], y2[n])) +
      scale_x_continuous(breaks = seq(x1[n], x2[n], 5), minor_breaks = seq(x1[n], x2[n], 1),limits=c(x1[n], x2[n])) +