如何用线条创建玫瑰图

时间:2015-11-16 11:47:40

标签: r ggplot2

我正在尝试创建染色体数据的玫瑰图,如下所示

    structure(list(chr = c(11, 11, 11, 12, 12, 12, 13, 13, 13, 14, 
16, 16, 18, 2, 2, 20, 20, 3, 4, 4), leftPos = c(17640000, 2880000, 
29040000, 19680000, 6120000, 6480000, 14880000, 16200000, 17760000, 
13560000, 21240000, 7080000, 10440000, 16800000, 49080000, 12240000, 
8280000, 13320000, 12000000, 13560000), Means.x = c(218.523821652113, 
256.117545073851, 343.541494875886, 348.237645885147, 426.983644179467, 
228.568161732039, 283.269605668063, 440.686146437743, 218.674798674891, 
264.556139561699, 232.068688576066, 226.877793789348, 224.282711224934, 
215.935347248385, 253.472008896076, 230.683794652539, 305.788038763088, 
285.805349707436, 644.897029710454, 485.630136931446), Def.x = c(1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), Means.y = c(236.188586172547, 
345.958953367193, 250.194040077771, 304.25175004754, 336.629006416052, 
221.495167672412, 231.719055660279, 231.252826188427, 334.254524914754, 
271.392526335334, 236.848569235568, 261.62635228236, 246.090793604293, 
370.773978424351, 242.493276055677, 245.097715487835, 280.225103337613, 
370.736474095631, 1014.42590543955, 236.718929160423), Def.y = c(1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)), .Names = c("chr", 
"leftPos", "Means.x", "Def.x", "Means.y", "Def.y"), row.names = c(NA, 
20L), class = "data.frame")

最初我对自己非常满意,因为我可以得到这些情节

enter image description here

使用此代码:

ggplot(ZoutliersM)+
  geom_point(aes(x = ZoutliersM$leftPos/1000000,y = as.numeric(ZoutliersM$Def.x)),stat="identity",fill="magenta",size=2,colour="red")+
  geom_bar(aes(x = ZoutliersM$leftPos/1000000,y = as.numeric(ZoutliersM$Def.x)),stat="identity",fill="purple",size=1,colour="red")+
  ylim(0, 1)+
  ggtitle("Shared")+
  #geom_hline(aes(yintercept=0))+
  coord_polar(theta = "x", start = 0)+
  facet_wrap(~ chr)

但是我使用geom_bar时遇到问题,因为我经常收到错误

position_stack requires constant width: output may be incorrect

我认为输出不正确,因为它没有绘制所有点。

所以我花了很长时间寻找答案,但实际上并没有得到太多。我认为这是一个错误,因为geom_bar认为条形宽度都是不同的尺寸而不喜欢它。我已经尝试过更改为stat='bin',但我不想要频率图,我想只需要一条从点到x轴的线。

所以问题是我该如何做到这一点并且一起避免使用geom_bar。是否有例如为每个点绘制vline的方法,直到y=0点?

修改

然后我尝试了这个

ggplot(ZoutliersM)+
  geom_point(aes(x = ZoutliersM$leftPos/1000000,y = as.numeric(ZoutliersM$Def.x)),stat="identity",fill="magenta",size=2,colour="red")+
  geom_vline(xintercept = ZoutliersM$leftPos/1000000, linetype= 1, colour = "#919191")+
  ylim(0, 1)+
  ggtitle("Shared")+
  #geom_hline(aes(yintercept=0))+
  coord_polar(theta = "x", start = 0)+
  facet_wrap(~ chr)

我得到了这个:

enter image description here

但现在所有的vlines都绘制在一个图形上,然后每个染色体复制一次。所以还没有工作

1 个答案:

答案 0 :(得分:3)

试试geom_segment(),您可以使用两个坐标来指定线段:(x,y)(xend,yend)(x,y)坐标与您的点相同,而(xend,yend)坐标表示线段的另一端。在这种情况下,由于我们希望线从点延伸到x轴,xend应该与x相同,yend应该是0.我已整合所有将aes()个变量合并为一个,但与geom_segment()无关的所有其他变量保持不变:

ggplot(ZoutliersM,aes(x = ZoutliersM$leftPos/1000000,y = as.numeric(ZoutliersM$Def.x),
                      xend=ZoutliersM$leftPos/1000000,yend=0))+
  geom_point(stat="identity",fill="magenta",size=2,colour="red")+
  geom_segment(linetype= 1, colour = "#919191")+
  ylim(0, 1)+
  ggtitle("Shared")+
  coord_polar(theta = "x", start = 0)+
  facet_wrap(~ chr)

enter image description here