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:
plot
, when using pch = 21
you can set the outline colour with col
and fill colour with bg
. basecol
is an svyplot
-specific argument, which internally sets the outline colour col
. 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.