在某些图中添加y = 0线facet_grid ggplot2

热重

我有一个大阴谋,使用facet_grid()我想添加一条垂直线以指示y = 0,但仅在某些绘图中。

可重现的示例-

df <- data.frame(x = 1:100, y = rnorm(100,sd=0.5), type = rep(c('A','B'), 50))
ggplot(df) + facet_grid(type~.) +
     geom_point(data = df[df$type == 'A',], mapping = aes(x=x, y=y)) +
     geom_rect(data = df[df$type == 'B',], mapping=aes(xmin=x,ymin=0,xmax=(x+2),ymax=y)) +
     theme(panel.background=element_rect(fill="white")) 

例如,我只需要顶部ptot中的行。

弗里克先生

只需为hline geom创建另一个数据对象,并确保包括相关的多面变量即可。

df <- data.frame(x = 1:100, y = rnorm(100,sd=0.5), type = rep(c('A','B'), 50))
ggplot(df) + facet_grid(type~.) +
     geom_point(data = df[df$type == 'A',], mapping = aes(x=x, y=y)) +
     geom_rect(data = df[df$type == 'B',], mapping=aes(xmin=x,ymin=0,xmax=(x+2),ymax=y)) +
     geom_hline(data = data.frame(type="A", y=0), mapping=aes(yintercept=y)) +
     theme(panel.background=element_rect(fill="white")) 

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

ggplot2:在 facet_grid 中按组添加水平线

ggplot2:添加用于facet_grid的变量的名称

ggplot2:从图中删除未使用的因子级别组合的构面(facet_grid)

ggplot2:更改facet_grid图中的strip.text位置

在不同的facet_grid图中相等的面板高度,ggplot2 R

在ggplot2中的特定构面之间添加空间(facet_grid)

ggplot2和facet_grid:为每个图添加最大值

ggplot2 facet_grid在y轴上创建面板

如何使用facet_grid(ggplot2)在每个构面上绘制多条线

在ggplot2中使用`facet_grid`时,突出显示/在某些绘图周围画一个框

重新排列facet_grid并将其添加到ggplot2的geom_bar中

ggplot2 - facet_grid:如何沿 Y 轴设置不同范围的值但零对齐?

在ggplot2中使用facet_grid()时如何定义常见的y轴限制

ggplot2中的facet_grid错误

结合 facet_grid (ggplot2) 和 denscomp (fitdistrplus)

每条刻面线具有不同 y 轴的 ggplot2 刻面:如何从 facet_grid 和 facet_wrap 中获得“最佳”?

使用ggplot facet_grid R图添加图例

ggplot facet_grid具有不同的y映射

将标题添加到ggplot2图中,因为标题显示在facet_wrap图中

将geom_text添加到2D facet_grid ggplot

ggplot2中的facet_wrap()和facet_grid()有什么区别?

ggplot2图中的白线伪像

在ggplot2雷达/蜘蛛图中关闭线

gghighlight在ggplot2图中不会显示灰线吗?

在dplyr中按分组后在ggplot2条形图中添加误差线

将平均线添加到ggplot2中的分类抖动图中

R ggplot2:在箱形图中将均值添加为水平线

使用 ggplot2 在分组条形图中添加来自不同列的误差线

双Y轴图上的误差线--ggplot2