Ggplot2: adding another legend to the plot (twice)
I have the following dataset which is used to plot the frequencies of the bubble plot.
Frequency is the frequencies at time 1
Freq1 is the frequencies at time 2
id names variable value Freq Freq.1
1 1 item1 1 13 11
2 2 item2 1 9 96
3 3 item1 2 10 28
4 4 item2 2 15 8
5 5 item1 3 9 80
6 6 item2 3 9 10
7 7 item1 4 11 89
8 8 item2 4 14 8
9 9 item1 5 3 97
10 10 item2 5 25 82
I am using the following code to build and I like the plot. However, I am having some problems with the legend, which I explain below:
theme_nogrid <- function (base_size = 12, base_family = "") {
theme_bw(base_size = base_size, base_family = base_family) %+replace%
theme(panel.grid = element_blank())
}
plot1<- ggplot(Data, aes(x = variable, y = value, size = Freq, color=Freq.1))+
geom_point( aes(size = Freq, stat = "identity", position = "identity"),
shape = 19, color="black", alpha=0.5) +
geom_point( aes(size = Freq.1, stat = "identity", position = "identity"),
shape = 19, color="red", alpha=0.5) +
scale_size_continuous(name= "Frequencies ", range = c(2,30))+
theme_nogrid()
1- I would like to have two legends, one for the color, the other for the size, but I cannot get the correct arguments for this (I have a tutorial consultation and documentation on topics and I cannot solve my problem with my own ideas)
2- After two legends, I would like to increase the size of the legend shape to look bigger (not text, not background, just shape (no actual plot change)). Here is an example from what I would like and what I would like (which is an example from my real data). As you can see, it is almost impossible to distinguish the color from the first image.
Sorry if this is a newbie question, but I can't really get an example of this.
Thank,
Angulo
source to share
Try something like this
library(ggplot2)
library(tidyr)
d <- gather(Data, type, freq, Freq, Freq.1)
ggplot(d, aes(x = variable, y = value))+
geom_point(aes(size = freq, colour = type), shape = 19, alpha = 0.5) +
scale_size_continuous(name = "Frequencies ", range = c(2, 30)) +
scale_colour_manual(values = c("red", "blue")) +
theme_nogrid() +
guides(colour = guide_legend(override.aes = list(size = 10)))
The last line will make the circles in the color legend larger.
source to share