0

How to remove border from quadrant lines in geom_point plot (ggplot2) after adding "size"?

ggplot(
  DurablesSIZE,
  aes(
    x = DurablesSIZE$`GDP LQ`,
    y = DurablesSIZE$Slope,
    color = DurablesSIZE$Sector,
    size = DurablesSIZE$`2019 GDP`
  )
) +
  geom_point() +
  geom_hline(yintercept = 0) +
  geom_vline(xintercept = 1) +
  xlim(0, 5.5) +
  ylim(-0.26, 0.26) +
  geom_rect(aes(
    xmin = 1,
    xmax = Inf,
    ymin = 0,
    ymax = Inf
  ),
  fill = "green",
  alpha = 0.03) +
  geom_rect(aes(
    xmin = -Inf,
    xmax = 1,
    ymin = -Inf,
    ymax = 0
  ),
  fill = "red",
  alpha = 0.03) +
  geom_rect(aes(
    xmin = -Inf,
    xmax = 1,
    ymin = 0,
    ymax = Inf
  ),
  fill = "yellow",
  alpha = 0.03) +
  geom_rect(aes(
    xmin = 1,
    xmax = Inf,
    ymin = -Inf,
    ymax = 0
  ),
  fill = "yellow",
  alpha = 0.03) +
  labs(y = "Slope of GDP LQ (5Y)",
       x = "2019 GDP LQ",
       color = "Sector",
       size = "2019 GDP") +
  ggtitle("Oregon Durable Manufacturing \nTargeting Potential (GDP)") +
  geom_text(
    aes(label = ifelse(Slope > 0 & LQ > 1, as.character(Sector), '')),
    hjust = 0,
    vjust = 0,
    size = 2.5,
    nudge_x = -0.07,
    nudge_y = 0.013
  ) +
  theme(legend.key = element_rect(colour = NA, fill = NA),
        legend.box.background = element_blank())
            

After adding size to my points, there is a weird border around the quadrant line weird border.

NDatGuy
  • 9
  • 3
  • 1
    To have a good [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example), it is helpful to provide some of your data, which you can do with `dput(head(df))`. – AndrewGB Jun 18 '21 at 02:24

1 Answers1

1

The size aesthetic is being applied globally, so it is creating a thick border around each geom_rect -- it controls border width for that geom.

To remove it, take size out of the global aes mapping and use geom_point(aes(size = '2019 GDP')) + to apply it to that layer alone.

Another note: if you use geom_rect for annotation purposes, it will be plotted once for each relevant line of your data, leading to massive overplotting and minimal control of alpha. It will be better to use annotate("rect" ...) for those, or to create a separate summary table which those layers can refer to so they only plot once.

Here's some fake data I made up so that I could run your code. Please include something like this in your questions.

DurablesSIZE <- tibble(
  `GDP LQ` = 0.5*(1:10),
  LQ = 10:1,
  Slope = 0.05*(-4:5),
  Sector = rep(LETTERS[1:5], 2),
  `2019 GDP` = 1:10
)

Result with original code:

enter image description here

Revision with size aesthetic only applied locally:

enter image description here

Jon Spring
  • 55,165
  • 4
  • 35
  • 53
  • Thank you! This is very helpful. Sorry about not including my data; first time posting here. I'll add it going forward. – NDatGuy Jun 18 '21 at 16:32