Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a rose plot with lines

Tags:

r

ggplot2

I am trying to create a rose plot of chromosome data as follows

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

Initially I was pretty pleased with myself because I could get these plots

enter image description here

using this code:

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)

However I have a problem with using geom_bar as I constantly get the error

position_stack requires constant width: output may be incorrect

and I think the output is incorrect as it doesn't plot all of the points.

So I spent ages searching for an answer but really didn't get much. I think it's an error related to the fact that geom_bar thinks that the bar widths are all different sizes and doesn't like it. I've tried changing to stat='bin' but I don't want a frequency plot, I want to just have a line from the point to the x-axis.

So the question is how can I do this and avoid the geom_bar all together. Is there, for example a way of having vline drawn for each point down to the y=0 point?

Edit

so then I tried this

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)

and I got this:

enter image description here

but now all the vlines are plotted on one graph and then replicated per chromosome. so not working still

like image 256
Sebastian Zeki Avatar asked Dec 28 '25 23:12

Sebastian Zeki


1 Answers

Try geom_segment(), which allows you to use two coordinates to specify a line segment: (x,y) and (xend,yend). The (x,y) coordinates are the same as your point, while the (xend,yend) coordinate represent the other end of the line segment. In this case, since we want the line to extend from the point to the x-axis, xend should be the same as x and yend should be 0. I've consolidated all of your aes() variables into one, but everything else not related to geom_segment() I've kept the same:

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

like image 79
Sam Dickson Avatar answered Dec 30 '25 11:12

Sam Dickson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!