generating two separate legends for 2 points in same plot

Hi,

I need your help in generating two separate legends for points in the same plot; using ggplot2. One legend should show the symbols for the proxy, while another legend should show the symbols for the fraction; including their names. I want to use the same shape for both legends as listed in the "Shape" column.

Thank you, I appreciate your help :blush::heart:

data---
data <- read.table(text = "proxy Longitude Latitude fraction variable values count Shape
A 845076.8133 7952906.509 ground_floor server 5 2 3
B 628213.526 4922672.854 second_floor server 3 38 6
C -7893548.463 4138071.5 ground_floor server 9 1 20
G -7669834.187 2590343.455 ground_floor server2 12 1 1
E -10699192.16 7126643.172 ground_floor server 14 1 19
F 64789.72751 4435533.366 ground_floor server 16 2 9
G -338124.8274 4561495.29 third_floor server 15 5 8
H -2531015.267 7503498.634 fourth_floor server 22.1 1 64
I 975533.7618 5024220.061 third_floor server 24.8 36 5
j -7920520.004 4804023.573 ground_floor server3 27.5 5 22
k -6697659.731 4128488.631 ground_floor server 30.2 9 35
D -6852811.697 3505961.577 ground_floor server2 32.9 6 2
G 1444849.581 5678072.381 ground_floor server3 35.6 6 4
B 2561971.64 4329459.255 fifth_floor server3 38.3 43 10
F -1225583.899 7057209.131 ground_floor server4 41 4 18
D 571523.8736 4726135.588 ground_floor server3 43.7 3 7
", header = TRUE)

My code:

ggplot(data, aes(x = Latitude, y = Longitude)) +
  geom_point(aes(shape = Shape), color = "black", fill = "white", size = 4) +
  geom_point(data = data, aes(x = Latitude, y = Longitude, shape = factor(fraction)),
             color = "black", fill = "white", size = 4) +
  scale_shape_manual(name = "Shape", values = unique(data$Shape)) +
  scale_shape_manual(name = "Fraction", values = unique(data$fraction)) +
  theme(legend.key = element_rect(fill = "white", color = "black"))

you can use ggnewscale;

data <- read.table(text = "proxy Longitude Latitude fraction variable values count Shape
A 845076.8133 7952906.509 ground_floor server 5 2 3
B 628213.526 4922672.854 second_floor server 3 38 6
C -7893548.463 4138071.5 ground_floor server 9 1 20
G -7669834.187 2590343.455 ground_floor server2 12 1 1
E -10699192.16 7126643.172 ground_floor server 14 1 19
F 64789.72751 4435533.366 ground_floor server 16 2 9
G -338124.8274 4561495.29 third_floor server 15 5 8
H -2531015.267 7503498.634 fourth_floor server 22.1 1 64
I 975533.7618 5024220.061 third_floor server 24.8 36 5
j -7920520.004 4804023.573 ground_floor server3 27.5 5 22
k -6697659.731 4128488.631 ground_floor server 30.2 9 35
D -6852811.697 3505961.577 ground_floor server2 32.9 6 2
G 1444849.581 5678072.381 ground_floor server3 35.6 6 4
B 2561971.64 4329459.255 fifth_floor server3 38.3 43 10
F -1225583.899 7057209.131 ground_floor server4 41 4 18
D 571523.8736 4726135.588 ground_floor server3 43.7 3 7
", header = TRUE)
library(tidyverse)
library(ggnewscale)

(data$variable   <- factor(data$variable  ))
(data$fraction   <- factor(data$fraction  ))
ggplot(data) + aes(x = Latitude, y = Longitude) +
  geom_point(aes(shape=variable),color = "red", fill = "white", size = 4) +
   scale_shape_manual(name = "Shape", values = unique(data$variable)) +
  ggnewscale::new_scale("shape")+
  geom_point(aes(shape=fraction),color = "black", fill = "white", size = 4) +
  scale_shape_manual(name = "Fraction", values = unique(data$fraction)) +
  theme(legend.key = element_rect(fill = "white", color = "black"))

ps. I didnt think Shape was a good field to map to shapes naively as it has so many unique values so I just picked variable instead, for sake of demonstration

image

1 Like

Hi @nirgrahamuk, thank you. ggnewscale worked for me. Do you have suggestion; how I can shift "Fraction" legend at bottom in horizontal position (with 2 column), while keeping "Shape " legend at the same place they are on left :slight_smile:

thank you :blush:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.