简体   繁体   中英

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

I use the very nice survey package to create plots of a stratified samples in R. There are different ways to create scatter-plots that represent sampling weights but I prefer the so called Bubble plots . Bubble plots are scatter-plots with circles whose area is proportional to the sampling weight.

I would like to know if I can fill the bubbles with a specified color. Setting color of the outline works as described below. However changing the type of symbol with the pch parameter fails (this would be the standard method for scatter-plots in 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) 

具有自定义颜色但没有自定义填充的气泡图

We can do the following:

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

在此输入图像描述

Some comments:

  1. In base R's plot , when using pch = 21 you can set the outline colour with col and fill colour with bg .
  2. Here, basecol is an svyplot -specific argument, which internally sets the outline colour col .
  3. We can use the same function that we used for basecol to fill circles with the matching colour, by recognising that dstrat$variables is the data.frame that contains column stype .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM