R 在ggplot2图形下方添加注释

R 在ggplot2图形下方添加注释,r,ggplot2,R,Ggplot2,我想在ggplot2图形下面画一条线,上面有文本,类似这样: 其中可以指定x轴上基因的起点和终点 我迄今为止的努力: require(ggplot2) require(grid) require(gridExtra) data = data.frame(y = -log10(runif(100)), x = 1:100) p = ggplot(data=data, aes(x, y)) + geom_point() p = p + theme(plot.margin=unit(c(1, 1, 5

我想在ggplot2图形下面画一条线,上面有文本,类似这样:

其中可以指定x轴上基因的起点和终点

我迄今为止的努力:

require(ggplot2)
require(grid)
require(gridExtra)
data = data.frame(y = -log10(runif(100)), x = 1:100)
p = ggplot(data=data, aes(x, y)) + geom_point()
p = p + theme(plot.margin=unit(c(1, 1, 5, 1), "lines"))
t1 = textGrob("Gene1")
p1 = p + annotation_custom(grob=t1, xmin=0, ymin=0, xmax = 3, ymax=-.1)
print(p1)
其中:


如果我试图通过调整ymax向下移动文本,那么它就会消失

您可以关闭剪裁

g <- ggplotGrob(p1)
g$layout$clip[g$layout$name == "panel"] <- "off"
grid.newpage()
grid.draw(g)

您可以关闭剪辑

g <- ggplotGrob(p1)
g$layout$clip[g$layout$name == "panel"] <- "off"
grid.newpage()
grid.draw(g)

在我的回答中,我改变了两件事: 1-我将数据的名称更改为df,因为数据可能导致对象和参数之间的混淆。 2-我删除了主数据图周围额外的面板空间,这样注释就不会太远了

require(ggplot2)
require(grid)
require(gridExtra)

# make the data
df <- data.frame(y = -log10(runif(100)), x = 1:100)

p <- ggplot(data=df, aes(x, y)) + geom_point()

# remove this line of code:
# p <- p + theme(plot.margin=unit(c(1, 1, 5, 1), "lines"))

# set up the plot theme for the annotation
blank_axes_and_thin_margin <- theme(axis.text = element_text(color="white"),
                    axis.title = element_text(color="white"),
                    axis.ticks = element_blank(),
                    panel.grid = element_blank(),
                    panel.border = element_blank(),
                    plot.margin=unit(c(0, 2, 0,2),"mm"))

# define the position of the arrow (you would change this part)
arrow_start <- min(df$x)
arrow_end <- mean(c(min(df$x), max(df$x)))
arrow_height <- 1

# here's the rectangle with the arrow
t2 <- ggplot(df, aes(x,y))+
  theme_bw()+
  geom_rect(aes(xmin=min(x), xmax = max(x)),
                ymin=0, ymax=4,fill="gray50")+
  coord_cartesian(ylim=c(0,4))+
  annotate(geom="text", label="Gene1", 
           x=20, y=2, size=6, color="black")+
  geom_segment(x=arrow_start, xend=arrow_end,
               y=arrow_height, yend=arrow_height, 
               color="black", arrow=arrow(ends="both"))+
  blank_axes_and_thin_margin
t2

# arrange the graphic objects here
# I use arrangeGrob because it allows you to use ggsave(), unlike grid.arrange
plot_both <- arrangeGrob(p, t2, nrow=2, heights=unit(c(0.75,0.25), "null"))
plot_both

# ta-da !

在我的回答中,我改变了两件事: 1-我将数据的名称更改为df,因为数据可能导致对象和参数之间的混淆。 2-我删除了主数据图周围额外的面板空间,这样注释就不会太远了

require(ggplot2)
require(grid)
require(gridExtra)

# make the data
df <- data.frame(y = -log10(runif(100)), x = 1:100)

p <- ggplot(data=df, aes(x, y)) + geom_point()

# remove this line of code:
# p <- p + theme(plot.margin=unit(c(1, 1, 5, 1), "lines"))

# set up the plot theme for the annotation
blank_axes_and_thin_margin <- theme(axis.text = element_text(color="white"),
                    axis.title = element_text(color="white"),
                    axis.ticks = element_blank(),
                    panel.grid = element_blank(),
                    panel.border = element_blank(),
                    plot.margin=unit(c(0, 2, 0,2),"mm"))

# define the position of the arrow (you would change this part)
arrow_start <- min(df$x)
arrow_end <- mean(c(min(df$x), max(df$x)))
arrow_height <- 1

# here's the rectangle with the arrow
t2 <- ggplot(df, aes(x,y))+
  theme_bw()+
  geom_rect(aes(xmin=min(x), xmax = max(x)),
                ymin=0, ymax=4,fill="gray50")+
  coord_cartesian(ylim=c(0,4))+
  annotate(geom="text", label="Gene1", 
           x=20, y=2, size=6, color="black")+
  geom_segment(x=arrow_start, xend=arrow_end,
               y=arrow_height, yend=arrow_height, 
               color="black", arrow=arrow(ends="both"))+
  blank_axes_and_thin_margin
t2

# arrange the graphic objects here
# I use arrangeGrob because it allows you to use ggsave(), unlike grid.arrange
plot_both <- arrangeGrob(p, t2, nrow=2, heights=unit(c(0.75,0.25), "null"))
plot_both

# ta-da !

您是否需要以数据标度为单位相对于x轴ie进行精确定位?另请参见@baptiste Yes,需要精确定位。您是否需要以数据标度为单位相对于x轴ie进行精确定位?另请参见@baptiste Yes,需要精确定位。酷!这些箭头有点过大,有办法改变吗?谢谢您可能想尝试?箭头帮助文件。它不是很彻底,但它提到了可能会帮助你的控制变量。酷!这些箭头有点过大,有办法改变吗?谢谢您可能想尝试?箭头帮助文件。它不是很彻底,但它提到了可能帮助您的控制变量。