在 R 中的 svyplot() 中用颜色填充气泡(调查包)

Fill bubbles with color in svyplot() in R (survey package)

我使用非常好的 survey 包在 R 中创建分层样本图。创建代表采样权重的散点图有不同的方法,但我更喜欢所谓的 Bubble plots . Bubble plots 是带有圆圈的散点图,圆圈的面积与采样权重成正比。

我想知道是否可以用指定颜色填充气泡。轮廓颜色的设置如下所述。但是,使用 pch 参数更改符号类型失败(这将是 R 中散点图的标准方法)。

library("survey")
data(api)
dstrat<-svydesign(id=~1,strata=~stype, weights=~pw, data=apistrat, fpc=~fpc)

# create bubble plot
svyplot(api00~api99, design=dstrat,
        basecol=function(df){c("goldenrod","tomato","sienna")[as.numeric(df$stype)]},
        style="bubble",alpha=c(0,1))

# try to set fill with pch (fails without error message)
svyplot(api00~api99, design=dstrat,
        basecol=function(df){c("goldenrod","tomato","sienna")[as.numeric(df$stype)]},
        style="bubble",alpha=c(0,1),pch=15) 

我们可以做到以下几点:

getcol <- function(df) c("goldenrod","tomato","sienna")[as.numeric(df$stype)]

svyplot(api00 ~ api99,
    design = dstrat,
    basecol = getcol,
    style = "bubble",
    alpha = c(0,1),
    pch = 21,
    bg = getcol(dstrat$variables))

一些评论:

  1. 在 base R 的 plot 中,当使用 pch = 21 时,您可以使用 col 设置轮廓颜色并使用 bg.
  2. 设置填充颜色
  3. 这里,basecol是一个svyplot特定的参数,它在内部设置轮廓颜色col
  4. 我们可以使用我们用于 basecol 的相同函数来用匹配的颜色填充圆圈,通过识别 dstrat$variables 是包含列 [=21= 的 data.frame ].