Reputation: 2412
I want to draw a series of rectangles on a plot in ggplot, depicting regions of a protein. The basic data looks like:
x y ids
6 9851 IgLike
10216 12022 PEVK
12041 14012 IgLike
14019 32144 Fibronectin
32178 32432 ProteinKinase
32496 34344 IgLike
Column one to column two indicates the region across the x axis i want labelled as column 3. I tried to follow the guide at http://docs.ggplot2.org/current/geom_polygon.html by Hadley, but it's a tad confusing. I have the data transformed to "positions" ;
9851 .75 IgLike
6 0.75 IgLike
6 .9 IgLike
9851 .9 IgLike
12022 .75 PEVK
10216 0.75 PEVK
10216 .9 PEVK
12022 .9 PEVK
14012 .75 IgLike
12041 0.75 IgLike
12041 .9 IgLike
14012 .9 IgLike
32144 .75 Fibronectin
14019 0.75 Fibronectin
14019 .9 Fibronectin
32144 .9 Fibronectin
32432 .75 ProteinKinase
32178 0.75 ProteinKinase
32178 .9 ProteinKinase
32432 .9 ProteinKinase
34344 .75 IgLike
32496 0.75 IgLike
32496 .9 IgLike
34344 .9 IgLike
which is almost there, but some of the shapes are overlapping, instead of being discrete shapes as they should be. The y axis values of .75 and .9 are arbitrary, I just want some thickness so its readily visible as more than just a line
then
ggplot(positions, aes(x=x, y=y)) + geom_polygon(aes(fill=ids))
Upvotes: 0
Views: 949
Reputation: 13139
As you only want rectangles, geom_polygon might be a little overkill. When I use your original data
df1 <- read.table(text="
x y ids
6 9851 IgLike
10216 12022 PEVK
12041 14012 IgLike
14019 32144 Fibronectin
32178 32432 ProteinKinase
32496 34344 IgLike",header=T)
I can plot it as rectangles as you already gave xmin (x) and xmax(y) on one line; the ymin and ymax variables are arbitrary.
p1 <- ggplot(df1) +
geom_rect(aes(xmin=x,xmax=y,ymin=1,ymax=2,fill=ids))
p1
Upvotes: 1