2016-02-21 140 views
3

如何合併顯示ggplot 2中的比例尺的兩個傳說。 我有一個數據框df。我用下面的代碼得到了下面的圖1。現在我該如何合併這兩個傳說來一起顯示比例大小和類別的東西。我想讓圖例如圖2所示。ggplot2中的比例圖例顯示

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

您可以創建一個新的集大小要使用的傳說:

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

+0

謝謝。太棒了 – Cirrus