2012-05-11 21 views
4

我正在使用分類數據,並且我正在嘗試繪製散點圖,其中點的大小應表示該位置的頻率點。如何使用不同的點大小來表示該點位置的數量

我首先嚐試了抖動,但我對該解決方案感到不滿。

我想我可以創建一個頻率列,但沒有設法爲此創建代碼。

qplot(X, Y, data=datatable, geom=c("point")) 

有沒有人有想法?

THX

+1

查看一些數據,圖形看起來像是什麼樣的圖像以及您想要查看的示例會很有用。 – dnagirl

+0

'ggplot(datatable,aes(x = X,y = Y,size = my.size))+ geom_point()'其中'my.size'是一個變量,表示您想要繪製的頻率?請參見示例6:http://had.co.nz/ggplot2/geom_point.html –

+0

或者您可能正在尋找'ggplot(datatable,aes(X,Y))+ stat_sum()'? –

回答

0

嘗試在包spatstat的PPP類,用於標記對象的默認情節做了你問什麼。

8

這是猜測你在做什麼。在下面的df數據框中,xy是您的分類變量。有多種方法可以獲得頻率計數。在此,使用來自plyr包的ddply()功能。隨後是情節。在致電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)) 

enter image description here

或者使用df數據幀相同的情節 - 在非聚集的數據。

ggplot(data = df, aes(x = x, y = y)) + 
    stat_sum(aes(size = factor(..n..)), geom = "point") + 
    scale_size_discrete(range = c(1, 10)) 
相關問題