I am trying to include a legend for a scatterplot where size of plot indicates number of pairings
freqData <- as.data.frame(table(galton$child, galton$parent))
names(freqData) <- c("child", "parent", "freq")
plot(as.numeric(as.vector(freqData$parent)),
as.numeric(as.vector(freqData$child)),
pch = 21, col = "black", bg = "lightblue",
cex = .10 * freqData$freq,
xlab = "parent", ylab = "child")
legend("bottomright","(freqData)",pch=21, title="freqData")
Changing the size of points in the legend can be done by passing a vector of pt.cex
values to legend()
. The following code was used to generate the sample plot. The example uses a square root of the frequency so that the point area is proportional to the count in that pairing.
# historical data
library('HistData')
# Galton Data
rawData <- Galton
# making a set of unique parings and counting frequency
freqData <- unique(rawData)
freqData$count <- NA
for(i in 1:nrow(freqData)){
freqData$count[i] <- length(intersect(which(rawData$parent %in% freqData$parent[i]),which(rawData$chil %in% freqData$child[i])))
}
# making plots
plot(freqData$parent
,freqData$child
,pch=19 # plot symbol
,cex=0.1*sqrt(freqData$count)) # point expansion
# adding legend
legend('bottomright' # location
,legend=c(1,5,10,15,20,25,30,35) # entries
,title='count' # title
,pt.cex=0.1*sqrt(c(1,5,10,15,20,25,30,35)) # point expansion
,pch=19 # plot symbol
,ncol=2 # number of columns
)
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.