Dani
Dani

Reputation: 161

ggplot legend not filled

I'm having difficulty modifying the legends on my ggplot to plot to my liking.

  1. The correct fill is not showing (the circles are all black atm, so I can't tel which group the points refer to)
  2. The position is incorrect (position = "bottom" is not working)
  3. Ideally, I would like the legends to use squares (the same as the shape used in my plot) rather than circles, but I'm not sure how to achieve this.

My data:

ors_fi = structure(list(col.x = c("Pre-frail", "Frail", "Pre-frail", "Frail", 
"Pre-frail", "Frail"), col.estimate = c(0.865018872113144, 1.63924006466768, 
0.972589483876898, 1.74300310363782, 0.836325226050668, 1.51301774619739
), col.stderr = c(0.0865502520576455, 0.0991158308454572, 0.0933472713471928, 
0.109413945850029, 0.0901760485538073, 0.109136014040399), col.group = c("Wave 1", 
"Wave 1", "Wave 2", "Wave 2", "Mean", "Mean")), row.names = c(NA, 
-6L), class = c("tbl_df", "tbl", "data.frame"))

My code:

plot_ors_fi = ggplot(data = ors_fi,
               aes(x = `col.x`, y = exp(`col.estimate`), group = as.factor(`col.group`))) +
  
  # Plot the point estimates
  geom_point(aes(size = 1,
                shape = 22,
                fill = as.factor(`col.group`)),
                stroke = 0.5,
                position = position_dodge(width = 0.4)) +
  
  # Plot point estimates text
  geom_text(aes(y = exp(`col.estimate`+1.96*`col.stderr`),
                label = format(round(exp(`col.estimate`), 2), nsmall = 2)),
            vjust = -0.8,
            size  = 3,
            position = position_dodge(width = 0.4)) +
  
  # Set the scale for the size of boxes
  scale_radius(guide  = "none",
               limits = c(0, NA_real_),
               range  = c(0, 3)) +
  
  # Plot the CIs
  geom_linerange(aes(ymin = exp(`col.estimate`-1.96*`col.stderr`),
                     ymax = exp(`col.estimate`+1.96*`col.stderr`),
                     colour = "black"), 
                 lwd = 0.5,
                 position = position_dodge(width = 0.4)) +
  
  # Use identity for aesthetic scales
  scale_shape_identity() +
  scale_colour_identity() +
  
  # Set the scale for fill colours
  scale_fill_grey(start = 0, 
                  end = 1, 
                  limits=c("Wave 1", "Wave 2", "Mean"),
                  guide="legend", 
                  position = "bottom", ## NOT WORKING?
                  name=NULL) +
  
  # Set the y-axis scale
  scale_y_continuous(trans = "log", breaks = c(1, 2, 4, 8)) +
  
  # Set the x-axis scale
  scale_x_discrete(limits = c("Pre-frail", "Frail")) +
  
  # Add titles
  xlab("Frailty Index") +
  ylab("OR (95% CI)") +
  ggtitle("")

This produces: enter image description here

As you can see the legend at present is not very useful. I couldn't find a similar question in SO where the legend fill colour does not show at all?

I'd appreciate some advice and thanks in advance.

Upvotes: 0

Views: 471

Answers (1)

Andy Baxter
Andy Baxter

Reputation: 7626

Shape needs to come out of the aes tag:

ggplot(data = ors_fi,
       aes(x = `col.x`, y = exp(`col.estimate`), group = as.factor(`col.group`))) +
  
  # Plot the point estimates
  geom_point(aes(size = 1,
                 fill = as.factor(`col.group`)),
             shape = 22,
             stroke = 0.5,
             position = position_dodge(width = 0.4))

# etc.

And legend position can be set by theme:

  # Add titles
  xlab("Frailty Index") +
  ylab("OR (95% CI)") +
  ggtitle("") +
  theme(legend.position = "bottom")

Which is hopefully what you're looking for?

enter image description here

Upvotes: 1

Related Questions