首页 > 解决方案 > ggplot中geom_rect的roundrectGrob等价物

问题描述

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

我已经尝试了 roundrectGrob() 函数,grid但得到了错误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()

图的蛋白质 pfam 结构

标签: rggplot2

解决方案


按照@hrbrmstr 的建议,安装statebins

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

在此处输入图像描述


推荐阅读