【发布时间】:2018-09-07 12:49:05
【问题描述】:
我正在尝试创建一个图形,该图形绘制点、标签和连接给定起点和终点位置的点的线。然后将其转换为极坐标图。我可以绘制点、标签和线,但我的问题是当我将图表转换为极坐标时。我用过geom_curve 和geom_segment.
在使用geom_curve 时出现错误,因为geom_curve 未针对非线性坐标实现。因此,我能得到的最远的是:
在使用geom_segment 时,我让它更接近我想要的效果,但它会沿着圆圈的圆周绘制线条,考虑到我通过坐标的方式,这是有道理的。这是一张照片:
我基本上需要一个geom_curve 来表示极坐标,但我一直找不到。我想要圆圈内侧的线条和弯曲的线条,会有一些重叠,但无论如何建议它看起来不错,间距或其他东西会受到欢迎。
数据:
k<-18
ct<-12
q<-6
x_vector1<-seq(1,k,1)
x_vector2<-seq(1,3,1)
x_vector3<-seq(k-2,k,1)
x_vector<-c(x_vector1,x_vector2,x_vector3)
n<-9 ## sets first level radius
radius1<-rep(n,k)
b<-13 ## sets second level radius
radius2<-rep(b,q)
radius<-c(radius1,radius2)
name<-c('Alice','Bob','Charlie','D','E','F','G','H','I','J','K','L',
'M','N','O','Peter','Quin','Roger','Alice2','Bob2','Charlie2',
'Peter2','Quin2','Roger2')
dframe<-data.frame(x_vector,radius,name)
dframe$label_radius<-dframe$radius+1
from<-c('Alice2','Bob','Charlie','D','E','Alice2','Charlie2','Charlie',
'I','J','K','L','M','N','O','Peter','Quin','Alice')
to<-c('Alice','Alice','Alice','Alice','Alice','Bob',
'Bob','Bob','Bob','Charlie','Charlie','Peter',
'Peter','Quin','Quin','Quin','Roger','Roger')
amt<-c(3,8,8,8,6,2,2,4,2,4,8,1,10,5,9,5,2,1)
linethick<-c(0.34,0.91,0.91,0.91,0.68,0.23,0.23,0.45,0.23,0.45,
0.91,0.11,1.14,0.57,1.02,0.57,0.23,0.11)
to_x<-c(1,1,1,1,1,2,2,2,2,3,3,16,16,17,17,17,18,18)
to_rad<-c(9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9)
from_x<-c(1,2,3,4,5,1,3,3,9,10,11,12,13,14,15,16,17,1)
from_rad<-c(13,9,9,9,9,13,13,9,9,9,9,9,9,9,9,9,9,9)
stats<-data.frame(from,to,amt,linethick,to_x,to_rad,from_x,from_rad)
p<-ggplot()+
geom_point(data=dframe,aes(x=x_vector,y=radius),size=3,shape=19)+
geom_text(data=dframe,aes(x=x_vector,y=label_radius,label=name))+
geom_segment(data=stats,aes(x=from_x,y=from_rad,xend=to_x,yend=to_rad, color=to), ## I need arrows starting at TO and going to FROM. ##
arrow=arrow(angle=15,ends='first',length=unit(0.03,'npc'), type='closed'))+
## transform into polar coordinates coord_polar(theta='x',start=0,direction=-1)
## sets up the scale to display from 0 to 7 scale_y_continuous(limits=c(0,14))+
## Used to 'push' the points so all 'k' show up. expand_limits(x=0) p
【问题讨论】:
-
请提供您用于生成这些图的数据和代码。
-
我很抱歉。我完全以为我有。
-
好多了,但是你的格式很混乱。我已经重新格式化了您的大部分代码,但
ggplot代码仍然很乱。如果它与问题无关,则不要包含代码,如果是,请不要将其注释掉。 -
顺便说一句,您的
q变量丢失了 -
您可以使用三角函数手动将数据点转换为极坐标,而不是依赖坐标变换。也就是说,让它看起来不错需要一些工作,而且你可能不得不牺牲
coord_polar提供的漂亮的径向网格线。
标签: r ggplot2 line polar-coordinates