【问题标题】:Map with geom_bin2d overlay with additional stat info使用 geom_bin2d 叠加的地图以及其他统计信息
【发布时间】:2014-02-25 10:53:03
【问题描述】:

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


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

这给了我这张地图:

最后一步是在我的 geom_bin2d 圆上显示大小与每个 bin 内的计数总和 (Grcounts) 成正比。

任何关于如何在 ggplot 中这样做的提示(最好)将不胜感激。


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

当我在diamonds 数据集上运行以下代码时,没有明显的问题: 但是,如果我在我的数据上运行相同的代码,我确实会遇到错位问题。关于可能导致此问题的任何想法?

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 和情节:

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

【问题讨论】:

    标签: r map ggplot2


    【解决方案1】:

    由于您的数据集无法在我的计算机上运行,​​因此将使用 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 位置作为 xmin,xmaxymin,ymax 的平均值。

    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)
    

    更新 - 分面解决方案

    要使用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() 从两个图中提取数据并将它们存储为对象。对于 sums 数据框 (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)
    

    【讨论】:

    • 谢谢!这部分是我的回答,我不希望圆圈代表从 stat_bin2d 中提取的计数,而是每个 bin 中 data$GRcounts 的总和。我需要计算每个 bin 的总和,然后绘制它。我现在知道如何从 ggplot_build(p)$data[[1]] 获取位置,但我不确定如何计算每个箱中的 GRcount 总和。
    • @GodinA 更新了我的答案以解决计数总和的问题
    • 我扩展了我的问题。关于可能导致问题的任何想法?
    • @GodinA 用 facet_wrap() 示例更新了我的答案。
    • 是否可以在每个面板上重复(相同)每个面板(蓝色刻度)的总计数,而不仅仅是显示值>0(红色圆圈)的位置。我试图在情节 2 中删除 facet_wrap(),但随后该模式仅出现在面板 1 上(如预期的那样)并且点未对齐...复制 x4 的 plot2 数据集并指定面板编号,可能会奏效.. . 但仍然会有错位问题。
    猜你喜欢
    • 2017-08-22
    • 1970-01-01
    • 2017-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多