Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何创建';聚集点图';对于分类数据?_R_Categorical Data - Fatal编程技术网

R 如何创建';聚集点图';对于分类数据?

R 如何创建';聚集点图';对于分类数据?,r,categorical-data,R,Categorical Data,我希望创建一个图形,就像这个从软件名为 我有一个分类频率数据的双向表,我想创建一个类似波动图的东西,但关键的区别是你可以看到单个数据点。 我尝试了ggplot2(…),levelplots(…)和各种包(比如ggplot2),但都没有成功。我在任何论坛上都找不到任何帮助 如果有人能帮我找到或创建一些代码来实现我的目标,我将不胜感激 这里是改进版 sample_data = structure(list(set = structure(c(1L, 2L, 3L, 4L, 1L, 2L, 3L,

我希望创建一个图形,就像这个从软件名为

我有一个分类频率数据的双向表,我想创建一个类似波动图的东西,但关键的区别是你可以看到单个数据点。 我尝试了
ggplot2(…)
levelplots(…)
和各种包(比如
ggplot2
),但都没有成功。我在任何论坛上都找不到任何帮助


如果有人能帮我找到或创建一些代码来实现我的目标,我将不胜感激

这里是改进版

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

你好,达珊。我很乐意提供一些示例数据,但我不确定如何最好地在这个论坛上发布它。你能为你推荐最好的格式来处理这个请求吗?欢迎来到StackOverflow。请看一下这些关于如何生成a的技巧,以及这篇文章。好的,这是一个样本数据集,我正在寻找一个在y轴上有“set”和在x轴上有“grade”的图,其中“freq”向量中的数据驱动显示的点数。样本数据你好,Darshan,看起来确实很有希望!感谢您在这个项目上投入的时间。我很好奇为什么有些点与主块有点“游离”,比如“09t02010 EH”的2级点。我将逐行检查您的代码,并试图弄清楚它是如何做的,但这需要一些时间。您可以添加的任何评论都将得到衷心的解释!啊!!我想我知道为什么有些观点是飘忽不定的。变量“theta”仍在以pi/4的步长递增,当它需要以较小的步长递增时,从簇的“中心”开始的步长越远。这也会影响斜边的取值。我可以看到你是如何试图绘制一个点的“螺旋”捕捉到一个“整数网格”的排序。聪明!现在,将你的方法推广到任何大小的频率数,而不仅仅是那些小于10的频率数……在谷歌上搜索一下生成方形螺旋的算法,我发现了这一点(但没有一个是在“r”中)。这种代码解决方案是否可以进行调整,以避免必须基于圆形底层结构进行绘图?达珊-完美!非常感谢改进后的代码和所有的评论。希望这段代码能帮助其他关注类似情节的人。再次非常感谢你。内维尔