使用geom_bin2d覆盖地图并附加其他统计信息

时间:2014-02-25 10:53:04

标签: r map ggplot2

我正在尝试使用ggplot2重现与此地图类似的内容:Map 这就是我到目前为止所做的:


    load("mapdata.Rdata")
    > ls() #2 datasets: "depth" for basemap (geom_contour) and "data" is use to construct geom_bin2d
    [1] "data"  "depth"
    > head(data)
        latitude longitude GRcounts
    740 67.20000 -57.83333        0
    741 67.11667 -57.80000        0
    742 67.10000 -57.93333        1
    743 67.06667 -57.80000        0
    751 67.15000 -58.15000        0
    762 67.18333 -58.15000        0
    ggplot(data=data,aes(x =longitude, y =latitude))
+theme_bw()
+ stat_bin2d(binwidth = c(0.5, 0.5))
+geom_contour(data=depth,aes(lon, lat, z=dn),colour = "black", bins=5)
+ xlim(c(-67,-56)) + ylim(c(65,71))

这给了我这张地图:My map

最后一步是显示我的geom_bin2d圈子,其大小与每个箱子内的计数总和(Grcounts)成比例。

有关如何在ggplot(最好)中这样做的任何提示将非常感激。


后续问题:使用facet_wrap时stat_bin2d和stat_summary2d之间的对齐不匹配

当我在diamonds数据集上运行以下代码时,没有明显的问题:Diamonds plot by cut但是,如果我在数据上运行相同的代码,则会出现错位问题。有什么可能导致这个问题的想法?

p<-ggplot(diamonds,aes(x =carat, y =price,colour=cut))+
   stat_summary2d(fun=sum,aes(z=depth,group=cut),bins=10)
p+facet_wrap(~cut)
df <- ggplot_build(p)$data[[1]]
summary(df)##now 5 groups, 1 panel
df$x<-with(df,(xmin+xmax)/2)
df$y<-with(df,(ymin+ymax)/2)
plot1<-ggplot(diamonds,aes(carat, price))+ stat_bin2d(bins=10)
plot1+geom_point(data=df,aes(x,y,size=value,group=group),color="red",shape=1)+facet_wrap(~group)

这是我的Rcode和情节:Mydata plot

p<-ggplot(dat,aes(x =longitude, y =latitude,colour=SizeClass))+
   stat_summary2d(fun=sum,aes(z=GRcounts,group=SizeClass),bins=10)
p+facet_wrap(~SizeClass)
df <- ggplot_build(p)$data[[1]]
summary(df)##now 4 groups, 1 panel
df$x<-with(df,(xmin+xmax)/2)
df$y<-with(df,(ymin+ymax)/2)
plot1<-ggplot(dat,aes(longitude, latitude))+ stat_bin2d(bins=10)
plot1+geom_point(data=df,aes(x,y,size=value,group=group),color="red",shape=1)+facet_wrap(~group)

> head(dat[c(7,8,14,21)])###mydata
     latitude longitude GRcounts SizeClass
742  67.10000 -57.93333        1 (100,150)
784  67.21667 -57.95000        1 (100,150)
756  67.11667 -57.80000        1    (<100)
1233 68.80000 -59.55000        2 (100,150)
1266 68.68333 -59.60000        2 (100,150)
1288 68.66667 -59.65000        1 (100,150)

我的数据集可以在这里下载:data

1 个答案:

答案 0 :(得分:5)

由于您的数据集无法在我的计算机上运行,​​因此请使用diamonds数据集作为示例。

使用stat_summary2d()制作新数据,并将z=设置为您想要求和的参数(在您的情况下为GRcounts),并提供fun=sum对这些值求和。将它存储为某个对象。

p<-ggplot(diamonds,aes(carat,price))+stat_summary2d(fun=sum,aes(z=depth))

使用函数ggplot_build()获取用于绘图的数据。矩形坐标位于xminxmaxyminymax列中,总和位于value列。

df <- ggplot_build(p)$data[[1]]
head(df)
     fill         xbin                ybin    value      ymax      ymin yint      xmax      xmin xint PANEL group
1 #55B1F7   [0.2,0.36]           [326,943] 641318.2  942.5667  326.0000    1 0.3603333 0.2000000    1     1     1
2 #1A3955   [0.2,0.36]      (943,1.56e+03]  75585.5 1559.1333  942.5667    2 0.3603333 0.2000000    1     1     1
3 #132B43   [0.2,0.36] (1.56e+03,2.18e+03]    415.8 2175.7000 1559.1333    3 0.3603333 0.2000000    1     1     1
4 #132B43   [0.2,0.36] (2.18e+03,2.79e+03]    304.4 2792.2667 2175.7000    4 0.3603333 0.2000000    1     1     1
5 #244D71 (0.36,0.521]           [326,943] 179486.8  942.5667  326.0000    1 0.5206667 0.3603333    2     1     1
6 #2D5F8A (0.36,0.521]      (943,1.56e+03] 271688.9 1559.1333  942.5667    2 0.5206667 0.3603333    2     1     1

对于点数,将x和y位置计算为xminxmaxyminymax的平均值。

df$x<-with(df,(xmin+xmax)/2)
df$y<-with(df,(ymin+ymax)/2)

使用此新数据框通过stat_bin2d()将点添加到原始图表。

ggplot(diamonds,aes(carat,price))+stat_bin2d()+
  geom_point(data=df,aes(x=x,y=y,size=value),color="red",shape=1)

enter image description here

更新 - 带分面的解决方案

要使用facet_wrap()并合并stat_bin2d()和积分,您应该使用一些解决方法,因为似乎存在一些问题。

首先,创建两个图 - 一个用于stat_summary2d()的总和,另一个用于带有stat_bin2d()的计数。两个地块都应该是分面的。

plot1 <- ggplot(dat,aes(x =longitude, y =latitude))+
  stat_summary2d(fun=sum,aes(z=GRcounts),bins=10)+facet_wrap(~SizeClass)

plot2 <- ggplot(dat,aes(longitude, latitude))+ stat_bin2d(bins=10)+
    facet_wrap(~SizeClass)

现在使用ggplot_build()从两个图中提取数据并将它们存储为对象。对于和数据帧(df1)计算的x和y坐标,如上例所示。

df1 <- ggplot_build(plot1)$data[[1]]
df1$x<-with(df,(xmin+xmax)/2)
df1$y<-with(df,(ymin+ymax)/2)

df2<-ggplot_build(plot2)$data[[1]]

现在使用这些新数据框绘制数据 - df1表示点,df2表示矩形。使用geom_rect(),您将获得fill=依赖于计数的矩形。对于分面使用列PANEL

ggplot()+geom_rect(data=df2,aes(xmin=xmin,xmax=xmax,
                          ymin=ymin,ymax=ymax,fill=count))+
  geom_point(data=df1,aes(x=x,y=y,size=value),shape=1,color="red")+
  facet_wrap(~PANEL)

enter image description here