2016-12-28 76 views
2

我想創建一個圖形,就像名爲Fathom的軟件一樣。如何爲分類數據創建「聚類點圖」?

http://fathom.concord.org/help/HelpFiles/_img331.png

我有希望創造這樣一個波動的情節絕對頻率數據的雙向表,但關鍵的區別是,你可以看到各個數據點。 我試過ggfluctuation(...),levelplots(...)和各種包裝(如ggplot2),但沒有成功。我在任何論壇上都找不到任何幫助。

如果有人能夠幫助我指導或創建一些能達到我目標的代碼,我將非常感激。

+1

你好達山。我很樂意提供一些示例數據,但我不確定如何在此論壇上發佈它。你能否建議最好的格式讓你接受並運行這個請求? – Nevil

+2

歡迎來到StackOverflow。請看看這些關於如何產生[最小,完整和可驗證的例子](http://stackoverflow.com/help/mcve)的技巧,以及這篇文章[在R中創建一個很好的例子]( http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)。 – lmo

+0

好吧,這裏是一個樣本數據集,我在「頻率」向量尋找已經在y軸和「品位」在x軸「設置」情節,用數據對駕駛點數顯示。 (「1」,「1」,「0」,「0」,「0」,「0」,「0」),sample_data < - data.frame(「set」= c(「09t0101 TJ」,「09t0102 MW」,「09t0201 EH」,「09t0202 NH」 「1」, 「1」, 「2」, 「2」, 「2」, 「2」, 「3」, 「3」, 「3」, 「3」, 「4」, 「4」,「4 「,」4「),」freq「= sample.int(length(0:10),16,replace = TRUE)) – Nevil

回答

3

這是改進版本。

sample_data = structure(list(set = structure(c(1L, 2L, 3L, 4L, 1L, 2L, 3L, 
4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L), class = "factor", .Label = c("09t0101 TJ", 
"09t0102 MW", "09t0201 EH", "09t0202 NH")), grade = structure(c(1L, 
1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L), .Label = c("1", 
"2", "3", "4"), class = "factor"), freq = c(7L, 8L, 2L, 3L, 11L, 
4L, 11L, 3L, 3L, 8L, 3L, 8L, 3L, 9L, 3L, 2L)), .Names = c("set", 
"grade", "freq"), row.names = c(NA, -16L), class = "data.frame") 

group = unique(sample_data$set) #Obtain the unique 'set' values for y-axis 
max_x = length(unique(sample_data$grade)) #Obtain the maximum number of 'grades' to plot on x-axis 
max_y = length(group) #Obtain the maximum number of 'set' to plot on y-axis 
pdf(file="plot.pdf",width=8,height=6) 
par(mar = c(5, 10, 4, 2)) #c(bottom, left, top, right) 
plot(max_x,max_y,xlim=c(0.5,max_x+0.5),ylim=c(0.5,max_y +0.5),pch=NA,xlab="Grades",ylab=NA,xaxt="n",yaxt="n",asp=1) #asp = 1 IMPORTANT 
axis(side = 2, at=c(1:length(group)), labels=c(as.vector(group)),las=2) 
axis(side = 1, at=c(1:length(unique(sample_data$grade))), labels=c(as.vector(unique(sample_data$grade)))) 

r = 0.15 #The diameter of circles to be plotted 

for (i in 1:length(group)){ 
a = subset(sample_data,sample_data$set==group[i]) #Subset new data.frame corresponding to first 'set' 

for (j in 1:nrow(a)){ 
matrix_sz = ceiling(sqrt(a$freq[j])) #Determine the size of square matrix that can accomodate all the frequency 
matrix_x = matrix(nrow = matrix_sz, ncol = matrix_sz) #Initiate matrix 
matrix_y = matrix(nrow = matrix_sz, ncol = matrix_sz) #Initiate matrix 
matrix_x[,1] = -1*((matrix_sz/2) - 0.5) #Find out relatve x co-ordinates for first column 
matrix_y[1,] = 1*((matrix_sz/2) - 0.5) #Find out relatve y co-ordinates for first row 

# Find out other relative co-ordinates if the size of square matrix is more than 1x1 
if (matrix_sz > 1){ 
for (column in 2:matrix_sz){ 
matrix_x[,column] = matrix_x[,column - 1] + 1 
} 
for (row in 2:matrix_sz){ 
matrix_y[row,] = matrix_y[row-1,] - 1 
} 
} 

#Determine the co-ordinate of the center of the square matrix grid 
xx = as.integer(a$grade[j]) 
yy = i 
fq = 1 #To keep track of the corresponding 'freq' 

# Plot circles around the center based on relative co-ordinates 
for (row in 1:matrix_sz){ 
for (column in 1:matrix_sz){ 
if (fq > a$freq[j]){break} #Break if the necessary number of points have been plotted 
xx1 = xx + r * matrix_x[row, column] 
yy1 = yy + r * matrix_y[row, column] 
# points (x = xx1, y = yy1, pch=1) 
fq = fq + 1 
symbols (x = xx1, y = yy1, circles=c(r/2.25),add =TRUE,inches=FALSE,bg = "gray") 
} 
} 
} 
} 
dev.off() 

enter image description here

+0

你好Darshan 這看起來非常有希望!感謝您在這個項目上投入的時間。我很想知道爲什麼一些點在主要區域有點「漂泊」,比如'09t02010 EH'的二級點。 我會逐行瀏覽你的代碼,並試圖弄清楚它是如何做的,但這需要我一些時間。任何您可以添加的評論都會被感激地解釋! – Nevil

+0

啊!我想我知道爲什麼有些觀點是漂泊的。變量'theta'仍然以pi/4步增加,當它需要以更小的步幅增加時,離羣集的'中心'越遠越遠。這也會影響'斜邊'的價值觀。我可以看到你是如何繪製一個「螺旋」的點,這些點可以捕捉到各種各樣的「整數網格」。聰明!我們只是概括你的方法的頻率數量的任何尺寸,而不僅僅是那些不到10 ..... – Nevil

+0

在谷歌搜索周圍的算法來生成一個螺旋四方形的一點發現我這個(但沒有它是在'r')。這種代碼解決方案是否可以適應,以避免將繪圖基於圓形底層結構? http://stackoverflow.com/questions/398299/looping-in-a-spiral – Nevil