将一个geom_line添加到R中facet_wrap图中的所有构面 [英] Adding a geom_line to all facets in a facet_wrap plot in R

查看:132
本文介绍了将一个geom_line添加到R中facet_wrap图中的所有构面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个 facet_wrap 图表,它将四条独立的行与一条普通的第五行进行比较;我们的目标是让第五行出现在所有其他四个 facet_wrap 图上。



这是我最小的代码:

  library(ggplot2)

x = c(1,3,1,3,2, (1,3,2,4,1,3,2,4)
type = c(A,A,B, B,C,C,D,D)
data = data.frame(x,y,type)

x = c(4,1)
y = c(1,4)
type = c(E,E)
line = data.frame(x,y,type)

ggplot(data,aes(x,y))+ geom_line()+ facet_wrap(〜type)+
geom_line(data = line,aes(x,y))

我希望将第五行添加为独立的 data.frame 可以让我做到这一点,但它只是将它添加为第五个方面,如下图所示:





我想让所有其他地块都显示E方面。有什么想法吗?我知道 geom_vline geom_hline geom_abline 都会出现在所有的方面,但我不知道是什么使它们独一无二。 您已指定 type ='E' data.frame中。如果你想在 A,B,C,D 类型上有这一行,那么创建一个 data.frame ($,$)

  xl = c(4,1)
yl = c( 1,4)
type = rep(LETTERS [1:4],each = 2)
line2 = data.frame(x = xl,y = yl,type)

ggplot(data,aes(x,y))+ geom_line()+ facet_wrap(〜type)+
geom_line(data = line2)

您也可以使用 annotate ,这意味着您不指定data.frame,而是传递x和y值直接

  ggplot(data,aes(x,y))+ geom_line()+ facet_wrap(〜type)+ 
注释(geom ='line',x = xl,y = yl)

p>


I'm trying to create a facet_wrap plot that compares four separate lines to a common fifth line; the goal is to have this fifth line appearing on all four of the other facet_wrap plots.

Here's my minimal code:

library(ggplot2)

x    = c( 1,  3,  1,  3,  2,  4,  2,  4)
y    = c( 1,  3,  2,  4,  1,  3,  2,  4)
type = c("A","A","B","B","C","C","D","D")
data = data.frame(x,y,type)

x    = c( 4,  1)
y    = c( 1,  4)
type = c("E","E")
line = data.frame(x,y,type)

ggplot(data, aes(x,y)) + geom_line() + facet_wrap(~type) +
geom_line(data = line, aes(x,y))

I was hoping that adding the fifth line as an independent data.frame would allow me to do this, but it just adds it as a fifth facet, as in the following image:

I want the "E" facet to show up on all of the other plots. Any thoughts? I know that geom_vline, geom_hline, and geom_abline will all appear on all of the facets, but I'm not sure what makes them unique.

解决方案

You have specified type='E' in your line data.frame. If you want to have this line on type A,B,C,D, then create a data.frame with the types on which you want the line to display

xl    = c( 4,  1)
yl    = c( 1,  4)
type =rep(LETTERS[1:4], each=2)
line2 = data.frame(x=xl,y=yl,type)

ggplot(data, aes(x,y)) + geom_line() + facet_wrap(~type) +
   geom_line(data = line2)

You could also use annotate, which means you don't specify a data.frame, but pass the x and y values directly

ggplot(data, aes(x,y)) + geom_line() + facet_wrap(~type) +
  annotate(geom='line', x=xl,y=yl)

Both create

这篇关于将一个geom_line添加到R中facet_wrap图中的所有构面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆