PinkyL
PinkyL

Reputation: 351

ggplot customize legend for geom_point shapes and remove background

I have data that looks something like this:

df = data.frame(
  measure = c("Measure A","Measure B","Measure C","Measure D",
              "Measure E","Measure F","Measure G"),
  overall = c(56, 78, 19, 87, 45, 51, 19),
  company = c(45, 89, 18, 98, 33, 55, 4),
  company_p = c(32, 65, 5, 56, 12, 45, 10)
)

Using this code:

ggplot(df %>% 
         mutate(fill = ifelse(overall > company, " Below Overall  "," Above Overall  ")), 
       aes(measure)) + 
  geom_col(aes(y=company, fill= fill)) + 
  geom_point(aes(y=overall, color="overall"), size=6, shape=124) + 
  geom_point(aes(y=company_p, color="company_p"), size=2, shape=19) +
  coord_flip() + 
  scale_color_manual(values=c("red4","grey3"),labels=c("Company Last Year","Overall")) + 
  scale_fill_manual(values=c(" Below Overall  "="lightpink2",
                             " Above Overall  "="lightblue2")) +
  theme(legend.key=element_blank())

I get a graph that looks like this:

enter image description here

How do I set the legend so that the company_p values, or 'Company Last Year' on the legend looks like the corresponding shape, while the Overall looks like a black line?

I tried linking the shape type to the legend like this:

ggplot(df %>% 
         mutate(fill = ifelse(overall > company, " Below Overall  "," Above Overall  ")), 
       aes(measure)) + 
  geom_col(aes(y=company, fill= fill)) + 
  geom_point(aes(y=overall, color="overall"), size=6, shape="overall") + 
  geom_point(aes(y=company_p, color="company_p"), size=2, shape="company_p") +
  coord_flip() + 
  scale_shape_manual(values=c(overall=124, company_p=19)) +
  scale_color_manual(values=c("red4","grey3"),labels=c("Company Last Year","Overall")) + 
  scale_fill_manual(values=c(" Below Overall  "="lightpink2",
                             " Above Overall  "="lightblue2")) +
  theme(legend.key=element_blank())

enter image description here

But the shape symbols don't correlate to the reference (124 is |, 19 is a circle) and it seems the shapes are together in the legend, not separate.

Upvotes: 2

Views: 3973

Answers (1)

astrofunkswag
astrofunkswag

Reputation: 2698

Here's one way to do it. I reshaped the data with tidyr to combine the two columns at issue. Since you want the points with different color, size, and shape you need to include all of those in the aes() for geom_point, and then you can use scale_ functions with the same name argument. I named it Legend but you can change that to whatever makes sense.

library(tidyr)
df2 <- df %>% mutate(fill = ifelse(overall > company, " Below Overall  "," Above Overall  ")) %>%
  gather(key, val, -company, -measure, -fill)

ggplot(df2, aes(measure)) + 
  geom_col(aes(y=company, fill= fill), data = df2[!duplicated(df2$measure),]) +
  scale_fill_manual(values=c(" Below Overall  "="lightpink2"," Above Overall  "="lightblue2")) +
  geom_point(aes(y=val, shape=key, color = key, size = key), data = df2) +
  scale_color_manual(name = "Legend", values=c("red4", "grey3"), labels = c("Company Last Year", "Overall")) +
  scale_shape_manual(name = "Legend", values = c(19, 124), labels = c("Company Last Year", "Overall")) +
  scale_size_manual(name = "Legend", values = c(2, 6), labels = c("Company Last Year", "Overall")) +
  coord_flip()

enter image description here

Upvotes: 2

Related Questions