在散点图中包含图例,其中图的大小表示频率

时间:2016-05-11 17:03:41

标签: r plot

我正在尝试为散点图包含一个图例,其中图的大小表示配对数

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

1 个答案:

答案 0 :(得分:1)

通过将pt.cex值的向量传递给legend(),可以更改图例中点的大小。以下代码用于生成样本图。该示例使用频率的平方根,以便点区域与该配对中的计数成比例。

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

enter image description here