如何使用不同的点大小来表示该点位置的数量

Ven*_*e12 5 r ggplot2

我正在使用分类数据,我正在尝试绘制一个散点图,其中点的大小应该代表该点位置的频率.

我先用抖动试了一下,但我对这个解决方案不满意.

我以为我可以创建一个Frequencies列但是没有设法为它创建代码.

    qplot(X, Y, data=datatable, geom=c("point"))
Run Code Online (Sandbox Code Playgroud)

有人有想法吗?

谢谢

San*_*att 9

这是对你所追求的东西的猜测.在df下面的数据帧,x并且y是你的分类变量.有多种方法可以获得频率计数.这里,使用包中的ddply()功能plyr.其次是情节.在呼吁ggplot:size美学确保点大小代表频率; 并且该scale_size_discrete()函数控制绘图上点的大小.

# Some toy data
df <- structure(list(x = structure(c(1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 
4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 
5L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 
4L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 1L, 2L, 1L, 2L, 
3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 
4L, 5L, 1L, 2L, 3L, 4L, 5L), .Label = c("1", "2", "3", "4", "5"
), class = "factor"), y = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 
5L, 5L, 5L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 
4L, 4L, 4L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 1L, 1L, 2L, 2L, 
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 
4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L), .Label = c("1", "2", "3", 
"4", "5"), class = "factor")), .Names = c("x", "y"), row.names = c(NA, 
79L), class = "data.frame")

# Required packages
library(plyr)
library(ggplot2)

# Get the frequency counts
dfc <- ddply(df, c("x", "y"), "nrow", .drop = FALSE)
#dfc

# The plot
ggplot(data = dfc, aes(x = x, y = y, size = factor(nrow))) + 
    geom_point() + 
    scale_size_discrete(range = c(1, 10))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

或使用df数据框的相同图- 未聚合数据.

ggplot(data = df, aes(x = x, y = y)) +
  stat_sum(aes(size = factor(..n..)), geom = "point") +
  scale_size_discrete(range = c(1, 10))
Run Code Online (Sandbox Code Playgroud)


mds*_*ner -1

尝试 spatstat 包中的 ppp 类,带有标记的对象的默认绘图可以满足您的要求。