比例图传奇显示在ggplot2中

时间:2016-02-21 19:39:52

标签: r plot graphics ggplot2 legend-properties

如何在ggplot 2中合并显示比例尺的两个图例。 我有一个数据框df。我得到以下图1,代码如下。现在我如何合并这两个传说来显示比例尺寸和类别。我想制作一个如下图2所示的传奇。

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")

获取比例图的代码

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。 enter image description here 我想要的传奇就像这样enter image description here

1 个答案:

答案 0 :(得分:1)

您可以创建一组用于图例的新尺寸:

new_size <- c(3,4,5,8,11,13)

然后,您可以在override.aes中使用guides添加这些内容,并通过将guide="none"添加到scale_size_continuous隐藏原始尺寸图例。

您当然可以计算更准确的尺寸值,以确保它们是成比例的。

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))) 

enter image description here