R - 折线图轴重叠

时间:2015-07-27 11:41:31

标签: r plot

我正在尝试绘制一个情节(我不知道如何调用这个情节)并且我在axis标签重叠方面遇到了麻烦。

enter image description here

我想知道你是否有任何建议在标签之间添加一些空格。 谢谢。

因为第3个值高于其他值,所以我给它一个值30,然后用实际值115标记它。

dta$freqOri = dta$freq
dta$freq[3] <- 30

情节循环

par(mar = c(0, 10.1, 0, 10.1)) 

for(i in 1:nrow(dta)){
  plot(0, ylim = c( min(dta$freq) - 5, max(dta$freq) + 2), bty = 'n', type = 'n', axes = F, ylab = '',  xlab = '')
  abline(h = dta$freq[i])
  axis(2, at = dta$freq[i], labels = dta$freqOri[i], las=2, tick = F, line = 7, cex.axis=0.6)
  axis(2, at = dta$freq[i], labels = dta$nameFrom[i], las=2, line = 0.5, tick =  F, cex.axis=0.6)
  axis(4, at = dta$freq[i], labels = dta$nameTo[i], las=2, line = 0.5, tick = F, cex.axis=0.6)
  par(new = T)
}

数据

dta = structure(list(freq = structure(c(8.5, 9, 30, 3.2, 13.4, 1.3, 
0.3, 4.1, 5.3, 6.7, 18.3, 5, 17.7, 2.1, 0.2, 4.5, 5.6, 8.5, 18.3
 ), .Dim = 19L, .Dimnames = structure(list(aaaa = c("a alone->b partner", 
"b partner->a alone", "b partner->b partner", "b partner->c child", 
"b partner->d nuclear", "b partner->e nuclear and acquaintance", 
"b partner->f children and acquaintance", "b partner->g partner and acquaintance", 
"b partner->h acquaintance", "b partner->i True Missing", "b partner->j work study sleep", 
 "c child->b partner", "d nuclear->b partner", "e nuclear and acquaintance->b partner", 
"f children and acquaintance->b partner", "g partner and acquaintance->b partner", 
"h acquaintance->b partner", "i True Missing->b partner", "j work study sleep->b partner"
)), .Names = "aaaa")), name = c("a alone->b partner", "b partner->a alone", 
"b partner->b partner", "b partner->c child", "b partner->d nuclear", 
"b partner->e nuclear and acquaintance", "b partner->f children and acquaintance", 
"b partner->g partner and acquaintance", "b partner->h acquaintance", 
"b partner->i True Missing", "b partner->j work study sleep", 
"c child->b partner", "d nuclear->b partner", "e nuclear and acquaintance->b partner", 
"f children and acquaintance->b partner", "g partner and acquaintance->b partner", 
 "h acquaintance->b partner", "i True Missing->b partner", "j work study sleep->b partner"
 ), nameFrom = c("a alone", "b partner", "b partner", "b partner", 
"b partner", "b partner", "b partner", "b partner", "b partner", 
"b partner", "b partner", "c child", "d nuclear", "e nuclear and acquaintance", 
"f children and acquaintance", "g partner and acquaintance", 
"h acquaintance", "i True Missing", "j work study sleep"), nameTo = c("b partner", 
"a alone", "b partner", "c child", "d nuclear", "e nuclear and acquaintance", 
"f children and acquaintance", "g partner and acquaintance", 
"h acquaintance", "i True Missing", "j work study sleep", "b partner", 
"b partner", "b partner", "b partner", "b partner", "b partner", 
"b partner", "b partner"), freqOri = structure(c(8.5, 9, 115.2, 
 3.2, 13.4, 1.3, 0.3, 4.1, 5.3, 6.7, 18.3, 5, 17.7, 2.1, 0.2, 
 4.5, 5.6, 8.5, 18.3), .Dim = 19L, .Dimnames = structure(list(
aaaa = c("a alone->b partner", "b partner->a alone", "b partner->b partner", 
"b partner->c child", "b partner->d nuclear", "b partner->e nuclear and acquaintance", 
"b partner->f children and acquaintance", "b partner->g partner and acquaintance", 
"b partner->h acquaintance", "b partner->i True Missing", 
"b partner->j work study sleep", "c child->b partner", "d nuclear->b partner", 
"e nuclear and acquaintance->b partner", "f children and acquaintance->b partner", 
"g partner and acquaintance->b partner", "h acquaintance->b partner", 
"i True Missing->b partner", "j work study sleep->b partner"
)), .Names = "aaaa"))), .Names = c("freq", "name", "nameFrom", 
"nameTo", "freqOri"), row.names = c(NA, 19L), class = "data.frame")

1 个答案:

答案 0 :(得分:0)

似乎重叠有两个不同的原因:在同一位置绘制的线和靠近在一起绘制的线。当行在同一位置(例如18.3或8.5)绘制时,您可以使用srt中的text()参数手动指定旋转,以便两个标签都可见并终止于同一位置。您可能还需要更改adj参数来控制文本对齐。

当线条分开但标签重叠时,有几种可能性。首先,可以调整文本大小,直到标签不重叠,但它们可能不可读。其次,标签可以水平偏移地绘制(不是所有对齐到相同的x位置),这可能需要延长一些线以使它们终止于标签。第三,可以使用上述旋转方法来分离这些标签。最后,可以将线条绘制得更远,但以原始单位标记。