Combine shapes in legend and override symbol size to set custom sizes for each shape

Suppose we have the following set of points in 2 groups (my_data). I want to plot a small dot over a larger triangle and assign different colors to triangles by group. The code below makes the plot just fine. I'm struggling to make legend symbols larger but setting different sizes for the triangles and different for the dots. override.aes size argument makes everything larger . Is there a way to set different sizes to make the symbol larger to follow the plot appearance..??...Thanx in advance.

my_data <- data.frame(x = sample(3592185:3850502, 10), y = sample(4104561:4258891, 10), var = c("A","A","B","A","A","A","B","A","B","A"))

ggplot() + 
  geom_point(data=my_data, aes(x = x, y = y, colour = var, shape = var, fill = var), stroke = 0.4) +
  scale_shape_manual("",values=c(24,24)) +
  scale_color_manual("",values=c("gray30", "steelblue")) +
  scale_fill_manual("",values=c("gray90", "lightblue")) +
  geom_point(data=my_data, aes(x = x, y = y, fill = var), color = "black",  shape = 16, size = 0.2) +
  guides(fill = guide_legend(override.aes = list(size = c(4,4)))) +
  labs(title = NULL, x = NULL, y = NULL)  

One of these may help you (?) :

library(ggplot2)
my_data <- data.frame(x = sample(3592185:3850502, 10),
                      y = sample(4104561:4258891, 10), 
                      var = c("A","A","B","A","A","A","B","A","B","A"))

ggplot() + 
  geom_point(data=my_data, aes(x = x, y = y, colour = var, shape = var, fill = var,size=var), stroke = 0.4) +
  scale_shape_manual(c("A","B"),values=c(24,24)) +
  scale_color_manual(c("A","B"),values=c("gray30", "steelblue")) +
  scale_fill_manual(c("A","B"),values=c("gray90", "lightblue")) +
  scale_size_manual(c("A","B"),values=c(2,4)) +
  geom_point(data=my_data, aes(x = x, y = y, fill = var), color = "black",  shape = 16, size = 1) +
  labs(title = NULL, x = NULL, y = NULL)  


ggplot() + 
  geom_point(data=my_data, aes(x = x, y = y, colour = var, shape = var, fill = var),size=4, stroke = 0.4) +
  scale_shape_manual(c("A","B"),values=c(24,24)) +
  scale_color_manual(c("A","B"),values=c("gray30", "steelblue")) +
  scale_fill_manual(c("A","B"),values=c("gray90", "lightblue")) +
  # scale_size_manual(c("A","B"),values=c(2,4)) +
  geom_point(data=my_data, aes(x = x, y = y, fill = var), color = "black",  shape = 16, size = 1) +
  labs(title = NULL, x = NULL, y = NULL)  


Created on 2021-07-08 by the reprex package (v2.0.0)
1 Like

Hello HanOostdijk. Many thanx for the examples.

Making bigger symbols in the plots is a nice solution. Unfortunately the plot is a map and the symbols are too many and too dense to make the size bigger and while this leads to correct symbols in the legend they are too small to see them in the scale of them map.
So finally I found a solution from another example in this SO conversation that allows you to keep the triangles and the dots small in the plot and make them bigger in the legend by editing the plot to make the dots smaller.

Many thanx again. Here is the code for anyone interested.


library(ggplot2)
library(grid)
my_data <- data.frame(x = sample(3592185:3850502, 10), y = sample(4104561:4258891, 10), var = c("A","A","B","A","A","A","B","A","B","A"))

g <- ggplot() + 
  geom_point(data=my_data, aes(x = x, y = y, colour = var, shape = var, fill = var), stroke = 0.4) +
  scale_shape_manual("",values=c(24,24)) +
  scale_color_manual("",values=c("gray30", "steelblue")) +
  scale_fill_manual("",values=c("gray90", "lightblue")) +
  geom_point(data=my_data, aes(x = x, y = y, fill = var), color = "black",  shape = 16, size = 0.2) +   
  guides(fill = guide_legend(override.aes = list(size = c(8,8)))) +
  labs(title = NULL, x = NULL, y = NULL)  

g

grid.ls(grid.force())    # get the names of all the grobs in the ggplot

# The edit - to set the size of the point in the legend to 1.5 mm
#grid.gedit("key-[-0-9]-1-1", size = unit(1.5, "mm"))    # 1-1 interacts with the symbols from the first geom point (triangles)
grid.gedit("key-[-0-9]-1-2", size = unit(1.5, "mm"))    # 1-2 interacts with the symbols from the second geom point (dots)

gg <- grid.grab()
ggsave(plot=gg, file="test_plot.png", units="in", width=8.8, height=6, dpi = 300, type ="cairo-png")

Finally you get a map in the scale of your choice and the combined symbols in the desired size.

This topic was automatically closed 21 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.