美文网首页
ggplot2 annotation_custom()函数添加线

ggplot2 annotation_custom()函数添加线

作者: 小明的数据分析笔记本 | 来源:发表于2022-07-18 00:15 被阅读0次
image.png

我想实现的效果是图中红色的线,但是添加上以后是灰色的线,这个先总是朝斜上方的,不知道如何让他朝斜下方

应该有参数可以设置的 但是找不到解决办法 暂时

20220722解决办法

可以先用ggplot2做一个图,然后用annotation_custom()函数来添加

比如

library(ggplot2)

ggplot()+
  geom_point(aes(x=1,y=1))+
  geom_point(aes(x=2,y=0))+
  annotation_custom(grob = segmentsGrob(),
                    xmin=1,xmax=2,ymin = 1,ymax = 0)

出图

image.png

代码

ggplot()+
  geom_segment(aes(x=1,y=1,xend=2,yend=0))+
  theme_void() -> p1
p1

然后把这个p1叠加上去

ggplot()+
  geom_point(aes(x=1,y=1))+
  geom_point(aes(x=2,y=0))+
  annotation_custom(grob =
                      ggplotGrob(p1))
image.png

两个图之间的点连线

ggplot()+
  geom_point(aes(x=1,y=1)) -> pp

library(patchwork)

pp+pp+
  coord_cartesian(clip = "off")+
  annotation_custom(grob = segmentsGrob(),
                    xmin=0.875,xmax=1,ymin=1,ymax=1)
image.png

但是有一个问题是不能够明确第一个图的位置坐标,如果调节图片的长宽 就得改第一个点的坐标

相关文章

网友评论

      本文标题:ggplot2 annotation_custom()函数添加线

      本文链接:https://www.haomeiwen.com/subject/bgjrirtx.html