Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot2 legend with only one category / with only the shape and no scale

Tags:

r

maps

ggplot2

How can I have a section of the legend with only one category? I tried to mess with the override.aes without sucess. Alternatively, the desired output could be seen as a legend with only the shape but not the scale.

ggplot(iris) +
  geom_point(aes(x=Sepal.Width, y=Sepal.Length, color=Species, size=Sepal.Length))+
  scale_size_continuous("Legend with \n only 1 circle ",range = c(5,10))+
  guides(size = guide_legend( override.aes=list(range=  c(1,5)))) 

image1

An illustration of the type of product I am trying to get to:map2

Points are scaled but the legend does not report the scale.

like image 682
GPierre Avatar asked Dec 21 '15 13:12

GPierre


3 Answers

Just create one break in the scale. You can add a custom label to it as well (here it is ""). You can also control the size of the point in the legend with the break you choose.

The scale_color_discrete() line is there because otherwise the 1-point legend would be on top, not what you had in your desired picture.

require(ggplot2)
g <- ggplot(iris) + geom_point(aes(x = Sepal.Width, y = Sepal.Length,
                                   color = Species, size = Sepal.Length)) +
  scale_color_discrete(name = "Color") +
  scale_size_continuous(name = "Legend with \n only 1 circle",
                        breaks = 5, labels = "")

enter image description here

like image 86
maccruiskeen Avatar answered Oct 19 '22 16:10

maccruiskeen


Although @choff solution is the best solution for the example I gave, here is the slightly different one I ended up using as I needed to have control over the range size of the circles.

ggplot(iris) +
  geom_point(aes(x=Sepal.Width, y=Sepal.Length, color=Species, size=Sepal.Length))+
  scale_size_continuous("Legend with \n only 1 circle ",range = c(5,10), labels=c("","","","",""))+
  guides(size =guide_legend( override.aes=list(size=c(4,0,0)))) +
  theme(legend.key = element_blank())
like image 35
GPierre Avatar answered Oct 19 '22 15:10

GPierre


From your target map chart, it looks like you want your legend to separate your chart symbols by color and shape. Size used only to set the size of the symbols. Also, your data would likely have a column for separately countries with investments from those without. So we can add a column to iris which separates the rows by two values, map color and shape to that column, and then display the legend for those two aesthetics in a single, combined legend. The code looks like:

sp <- ggplot(transform(iris, Flower_size = ifelse(Petal.Width < 1, "Small Flower","Big Flower"))) 
sp <- sp + geom_point(aes(x=Sepal.Width, y=Sepal.Length, fill=Flower_size, shape=Flower_size, size=Sepal.Length), colour=NA)
sp <- sp + scale_size_continuous(range = c(4,7))
sp <- sp + scale_shape_manual(values=c(21, 22))
sp <- sp + scale_fill_manual(values=c("grey", "orange"))
sp <- sp + labs(fill="", shape="")
sp <- sp + guides(size=FALSE, fill=guide_legend(override.aes=list(size=7)))
sp <- sp + theme(legend.text=element_text(size=12)) 
 plot(sp)

enter image description here

like image 29
WaltS Avatar answered Oct 19 '22 17:10

WaltS