0

I am trying to have two legends: one based on variable c and the other on variable d, defined by their own shape and size. I do know if this is possible in ggplot2? Maybe it is not fitting to the philosophy behind the use of ggplot2. If I transform the data to long format, I can deal with the different shapes, but the sizes are confounded. The same is happening if I use a facet_wrap option.

structure(list(a = c(5, 6, 7), b = c(5, 6, 7), c = c(0.1, 0.5, 
1), d = c(10, 5, 1)), .Names = c("a", "b", "c", "d"), row.names = c(NA, 
-3L), class = "data.frame")

library(ggplot2)
plot <- ggplot() + geom_point(data=e,aes(x=a,y=b,size=c), shape=1, 
color="black") 
plot <- plot + geom_point(data=e,aes(x=a,y=b,size=d), shape=3, color="red")
plot

Any advice is more than welcome.

1 Answers1

1

you can write shape and size in aes() like geom_point(aes(x=a,y=b,shape=factor(c))) +geom_point(aes(x=a,y=b,size=d), shape=3). For example,

library(ggplot2)
ggplot(mpg) + geom_point(aes(x=hwy,y=cty,shape=class)) +
geom_point(aes(x=hwy,y=cty,size=cyl), shape=3)

enter image description here

Senzeybek
  • 151
  • 5
  • Thanks for the answer. But If I specify the c variable as factor, I will lose its value to be presented as numeric. –  Jan 18 '19 at 08:30
  • I see, if you want to show a numeric variable, you can use fill, instead of shape. Lİke, `geom_point(aes(x=a,y=b,fill=c)) +geom_point(aes(x=a,y=b,size=d), shape=3)` – Senzeybek Jan 18 '19 at 13:06