如何在geom_point()中反转大小

时间:2016-07-21 00:45:42

标签: r plot ggplot2

我想绘制正值和负值,其比例大小由点表示,使得0在两种情况下最小,最大值是它们的大小。我希望负点大小使得0最小而-5最大同时正值0最小而5最大。像这个图像的东西

enter image description here

我知道如果它只是一个像here这样的变量,我可以反转并执行此操作。

但我有两种尺寸。

我的示例数据和代码如下。

dput(ddf)
structure(list(tr = c(0.192833333333335, 0.800378947368421, 2.28270774476556, 
0.586631034482762, 0.217825000000001, -0.773253846153845, -0.459235294117648, 
-0.0706142857142855, -0.24942, 11.1025623422045, -0.871375, -0.929300000000012, 
-5.92639999999999, 3.87432045251869, -3.62026818181818, -2.49045555555556, 
-0.730590000000007, 3.03523157894737, 1.04723333333333, -0.120474999999999
), Itr = c(0.192833333333335, 0.800378947368421, 2.28270774476556, 
0.586631034482762, 0.217825000000001, NA, NA, NA, NA, 11.1025623422045, 
NA, NA, NA, 3.87432045251869, NA, NA, NA, 3.03523157894737, 1.04723333333333, 
NA), Dtr = c(NA, NA, NA, NA, NA, -0.773253846153845, -0.459235294117648, 
-0.0706142857142855, -0.24942, NA, -0.871375, -0.929300000000012, 
-5.92639999999999, NA, -3.62026818181818, -2.49045555555556, 
-0.730590000000007, NA, NA, -0.120474999999999), td = c("Increasing", 
"Increasing", "Increasing", "Increasing", "Increasing", "Decreasing", 
"Decreasing", "Decreasing", "Decreasing", "Increasing", "Decreasing", 
"Decreasing", "Decreasing", "Increasing", "Decreasing", "Decreasing", 
"Decreasing", "Increasing", "Increasing", "Decreasing"), lat = c(-58.07, 
-57.9, -56.74, -55.22, -55.43, -54.12, -54.62, -54.63, -54.48, 
-54.25, -52.89, -54.05, -49.79, -49.82, -48.83, -48.73, -49.69, 
-49.29, -48.89, -49.37), lon = c(130.82, 131.15, 131.67, 129.14, 
127.71, 127.42, 126.9, 128.43, 128.33, 130.17, 125.77, 128.69, 
123.2, 123.15, 124.05, 123.67, 125.03, 124.91, 124.97, 124.98
)), .Names = c("tr", "Itr", "Dtr", "td", "lat", "lon"), class = "data.frame", row.names = c(NA, 
20L))

代码:

plt<-ggplot()+
    geom_point(data = ddf,aes(x = lon, y = lat,size =  Itr,shape=td),fill="Blue")+
    geom_point(data = ddf,aes(x = lon, y = lat,size = Dtr,shape=td),fill="Green", show.legend = FALSE)+          
    scale_size_continuous(name="IDt",range=c(2,12))+
    scale_shape_manual(name= "td",values=c(25,24),labels=c("Decreasing","Increasing"))
plt 

2 个答案:

答案 0 :(得分:1)

也许我误解了这个问题,但你给出的例子是:

require(ggplot2)
plt<-ggplot()+
  geom_point(data = ddf,aes(x = lon, y = lat,size =  Itr,shape=td),fill="Blue")+
  geom_point(data = ddf,aes(x = lon, y = lat,size = Dtr,shape=td),fill="Green", show.legend = FALSE)+          
  scale_size_continuous(name="IDt",range=c(2,12))+
  scale_shape_manual(name= "td",values=c(25,24),labels=c("Decreasing","Increasing"))
plt 

enter image description here

并根据绝对值反转比例:

require(ggplot2)
plt<-ggplot()+
  geom_point(data = ddf,aes(x = lon, y = lat,size =  abs(Itr),shape=td),fill="Blue")+
  geom_point(data = ddf,aes(x = lon, y = lat,size = abs(Dtr),shape=td),fill="Green", show.legend = FALSE)+          
  scale_size_continuous(name="IDt",range=c(12,2))+
  scale_shape_manual(name= "td",values=c(25,24),labels=c("Decreasing","Increasing"))
plt 

enter image description here

答案 1 :(得分:1)

我认为问题是要求建立一个不同的大小比例,其中最接近0的值最小,而距离0最远的值最大。

为了实现这一目标,您需要修改$("#myBox").val() = "1,4"以包含两个新的结果(ddf组合all_trsDtrItr只是绝对的您将用于分散大小比例的abs_trs的值:

all_trs

你显然可以建立这些基本方式library(dplyr) library(magrittr) ddf %<>% mutate(all_trs = ifelse(td == "Increasing", Itr, Dtr), abs_trs =abs(all_trs)) 但是管道ddf$all_trs <- ifelse(...)更容易和更酷;)

一旦内置了,你就可以继续制作你的情节:

%>%

enter image description here

我添加了plt <- ggplot(data = ddf, aes(x = lon, y = lat, color = td)) + geom_point(aes(shape = td, size = abs_trs, fill = td), alpha = .5) + scale_shape_manual(values = c(25, 24)) + scale_color_manual(values=c("blue", "red")) + scale_size_continuous(range = c(2,20)) + scale_fill_manual(values = c("blue","red")) + geom_text(aes(label = round(all_trs,1))) + guides(size = F) plt 叠加层,以帮助显示尺寸正确缩放。希望有所帮助!