简体   繁体   中英

Legend mixing shapes with geom_points R

My legend is not showing correctly when I am doing my graph in R using ggplot2. One column of my dataset is represented by a geom_bar and the two others are represented by geom_points (one shape each). The circle and the diamond shape are showing for both 2000 and 2008, the circle being in the diamong for both year. However, the graph works totally fine...

Here is a screenshot:

在此处输入图片说明

I have created a simplified version of my dataset:

order_var <- c(1, 4, 3, 5, 6, 2)
alt_name <- c('Agriculture', 'Mining', 'Food products',' Manufacture', 'Chemicals', 'Machinery')
y2000 <- c(20, 40, 50, 80, 30, 70)
y2008 <- c(40, 50, 80, 70, 30, 60)
y2018 <- c(10, 30, 80, 50, 40, 50)
datatest <- data.frame("order_var" = order_var, "alt_name" = alt_name, "y2000" = y2000, "y2008" = y2008, "y2018" = y2018)

And the code for my graph:

datatest %>% ggplot(aes(x = reorder(alt_name, order_var))) +
  geom_bar(stat = "identity", aes(y = `y2018`, fill = "2018"), width = 0.7, col = "black") +
  geom_point(aes(y = `y2008`, col = "2008"), shape = 23, fill = "white", size = 5) +
  geom_point(aes(y = `y2000`, col = "2000"), shape = 19, fill = "black", size = 3) +
  xlab("Industry") +
  ylab("Percentage") +
  theme(legend.position = "top") +
  scale_fill_manual(name = '', values = c("2018" = "#4F81BD"), breaks = c("2018")) +
  scale_colour_manual(name = '', values = c("2008" = "black", "2000" = "orange"))

If you know how to correct this problem, I would be very grateful!!

Thank you :)

That's a very tricky plot you are trying to make because you are in essence mapping the same aesthetics to different geom s.

The first thing you should do is to reshape your data to the long format. I also divided your dataset between 2018 (the bar), and 2000, 2008 (the points).

df2 <- datatest %>%
  pivot_longer(cols = -c(order_var, alt_name)) %>%
  mutate(bar = if_else(name == "y2018", 1, 0))

data_bar <- df2 %>% filter(bar == 1)
data_point <- df2 %>% filter(bar != 1)

I also find it useful to add a dodge to your points to avoid overlapping one inside the other as in the case of chemicals with position = position_dodge(width = 0.6) .

The first solution gives what you want, but it is a bit of a hack, and I wouldn't recommend doing it as a general strategy. You basically add an aesthetics that you are not going to use to the bars (in this case, linetype ), and then override it, as suggested in this answer .

ggplot(data_bar, aes(x = reorder(alt_name, order_var))) +
  geom_bar(aes(y = value, linetype = name), fill = "#4F81BD", stat = 'identity', color = 'black') +
  geom_point(data = data_point, position=position_dodge(width=0.6), aes(y = value, color = name, shape = name, size = name, fill = name)) +
  scale_colour_manual(values = c("orange", "black"), labels = c("2000", "2008")) +
  scale_fill_manual(values = c("orange", "white"), labels = c("2000", "2008")) +
  scale_shape_manual(values = c(19, 23), labels = c("2000", "2008")) +
  scale_size_manual(values = c(3, 5), labels = c("2000", "2008")) +
  scale_linetype_manual(values = 1, guide = guide_legend(override.aes = list(fill = c("#4F81BD"))), labels = c("2018")) +
  theme(legend.position = "top", legend.title = element_blank()) +
  labs(x = "Industry", y = "Percentage")

在此处输入图片说明

Another solution, more general, is to avoid using the fill aesthetics for the geom_point and changing the shape to a solid one instead:

ggplot(data_bar, aes(x = reorder(alt_name, order_var))) +
  geom_bar(aes(y = value, fill = name), stat = 'identity', color = "black") +
  geom_point(data = data_point, position=position_dodge(width=0.6), aes(y = value, color = name, shape = name, size = name)) +
  scale_fill_manual(values = c("#4F81BD"), labels = c("2018")) +
  scale_colour_manual(values = c("orange", "white"), labels = c("2000", "2008")) +
  scale_shape_manual(values = c(19, 18), labels = c("2000", "2008")) +
  scale_size_manual(values = c(4, 6), labels = c("2000", "2008")) +
  theme(legend.position = "top", legend.title = element_blank()) +
  labs(x = "Industry", y = "Percentage")

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM