Untitpoi
Untitpoi

Reputation: 141

plotting shape adjusting to size of window

Context

This question is part of a project to plot genetic maps in R. The common representation is a "scale" of horizontal trait bordered by two half circle. genetic map example genetic maps are roughly a sequence of position.

Goal

I'm trying to make a half circle matching the "ladders of the scale" and with adjustable high so that it always look like a circle and not like some sort of oval shape.

Actual code

library(ggforce)  
df <- data.frame(position=rnorm(n=15)*10)  
ggplot(aes(xmin=-2.5,ymin=position-0.1,xmax=2.5,ymax=position+0.1),data=df)+  
geom_rect() +  
geom_arc_bar(aes(x0 = 0, y0 = max(position), r0 = 0, r = 2.5, start =-pi/2,end = pi/2), color = "grey20")+  
geom_arc_bar(aes(x0 = 0, y0 = min(position), r0 = 0, r = 2.5, start = pi/2,end = 3*pi/2), color = "grey20")  

enter image description here

Question

So my question is: How to make a reactive shape scaling with the size of Rplot viewing window so that the shape always look like a circle and stay link to my upper "ladder"?

Try

I can adjust manually the size of the plot so that x.axis and y.axis have the same scale before exporting the graph but this is not quite efficient and will become really difficult if I have multiple chromosome on the same plot.
I would be glad to answer comments if needed and hope my question is clear enough!

Upvotes: 2

Views: 169

Answers (1)

Axeman
Axeman

Reputation: 35377

coord_fixed will make sure that the x and y are always scaled 1:1, even when rescaling the plot.

ggplot(aes(xmin=-2.5,ymin=position-0.1,xmax=2.5,ymax=position+0.1),data=df)+  
  geom_rect() +  
  geom_arc_bar(aes(x0 = 0, y0 = max(position), r0 = 0, r = 2.5, start =-pi/2,end = pi/2), color = "grey20")+  
  geom_arc_bar(aes(x0 = 0, y0 = min(position), r0 = 0, r = 2.5, start = pi/2,end = 3*pi/2), color = "grey20") +
  coord_fixed()

enter image description here

Upvotes: 2

Related Questions