ggplot中的geom_rect的roundrectGrob等效项

时间:2018-10-15 16:10:10

标签: r ggplot2

我正在尝试使图形附加在背景蛋白骨架上(使用geom_rect()函数绘制)。有什么方法可以创建圆角矩形而不是基本矩形(特别是红色矩形)。 这是我使用的代码。

我从grid开始尝试了roundrectGrob()函数,但收到了错误Error: Don't know how to add roundrectGrob(x = 28, y = -2.3, width = 22, height = 0.45) to a plot

library(ggplot2)
p <- ggplot() +
ylim(-4, 4) +
xlim(0, 100)
## First rect
p <- p + geom_rect(mapping=aes(xmin=10,
xmax=90,
ymin=-2.15,
ymax=-2.00),
colour = "black",
fill = "grey")
## Second rect
p <- p + geom_rect(mapping=aes(xmin=28,
xmax=50,
ymin=-2.30,
ymax=-1.85),
colour = "black",
fill = "red")
# print
p
dev.off()

Protein pfam structure for plot

1 个答案:

答案 0 :(得分:1)

在安装statebins之后,按照@hrbrmstr的建议

devtools::install_github("hrbrmstr/statebins")

尝试

library(ggplot2)
p <- ggplot() + ylim(-4, 4) + xlim(0, 100)
## First rect
p <- p + geom_rect(mapping=aes(xmin=10,
         xmax=90, ymin=-2.15,  ymax=-2.00),
         colour = "black", fill = "grey")
## Second rect
p <- p + statebins:::geom_rrect(mapping=aes(xmin=28, 
         xmax=50, ymin=-2.30, ymax=-1.85),  
         colour = "black", fill = "red")
# Print
p

enter image description here