Reputation: 648
How can i merge two legends showing proportional scale in ggplot 2.
I have a dataframe df
. I got the following plot 1 with code below. Now how can i merge the two legends to show something proportional size and class together. I want to make legend something like in figure 2 below.
dput(df)
structure(list(x = c(58.54, 58.07, 57.9, 56.71, 56.74, 56.11,
55.22, 55.13, 56.35, 55.43, 55.46, 55.43, 53.93, 54.12, 54.62,
54.65, 54.81, 54.61, 54.63, 54.48), y = c(-133.7, -130.82, -131.15,
-132.13, -131.67, -129.48, -129.14, -129.35, -130.69, -127.71,
-127.85, -126.7, -127.45, -127.42, -126.9, -127.12, -127.2, -127.5,
-128.43, -128.33), value = c(387.619784071665, 37.1752327050295,
409.872664195269, 1579.07184277674, 459.494444037465, 15.605721248393,
779.623269533058, 1.01892098254864, 104.561783471334, 45.1993322681666,
0.421293689514635, 50.6940007133332, 28.9504788576929, 75.1053594034691,
134.557578318905, 1.77584069574919, 0.265461776554673, 14.3060733821441,
903.028086907651, 105.510779979692), value_interval = c("100-500",
"1-100", "100-500", ">1500", "100-500", "1-100", "500-1000",
"1-100", "100-500", "1-100", "<1", "1-100", "1-100", "1-100",
"100-500", "1-100", "<1", "1-100", "500-1000", "100-500")), .Names = c("x",
"y", "value", "value_interval"), row.names = c(NA, 20L), class = "data.frame")
code to obtain the proportional plot
plott<-ggplot(df)+
geom_point(aes(x = x, y = y,color=value_interval, size =value),
alpha = 1, shape=16)+
scale_size_continuous(name="Proportion",range=c(3,10))+
scale_color_manual(name= " ",limits=c("<1","1-100","100-500","500-1000","1000-1500",">1500"),
values=c("Red","Blue","darkgoldenrod2","darkgreen","firebrick2","darkorange","deepskyblue"))
plott
Plot1.
Legend i want is like this
Upvotes: 3
Views: 772
Reputation: 10131
You could create a new set of sizes to use for the legend:
new_size <- c(3,4,5,8,11,13)
You can then add those using override.aes
in guides
and hide the original size legend by including guide="none"
to scale_size_continuous
.
You could of course calculate more exact size values to make sure they are proportional.
ggplot(df)+
geom_point(aes(x = x, y = y,color=value_interval, size =value),
alpha = 1, shape=16)+
scale_size_continuous(name="Proportion",range=c(3,13), guide="none")+
scale_color_manual(name= " ",limits=c("<1","1-100","100-500","500-1000","1000-1500",">1500"),
values=c("Red","Blue","darkgoldenrod2","darkgreen","firebrick2","darkorange","deepskyblue")) +
guides(colour=guide_legend(override.aes=list(size=new_size)))
Upvotes: 1