在faceted ggplot2中创建一条宽度来自dataframe的线

时间:2021-02-01 14:56:28

I am looking to create a graph that looks like the second picture below, using data provided below. However, I can't get the width of the lines to correspond to the axis, "B", and to take the values from datlines$width. What I'd like is for geom_hline (or maybe geom_ribbon?) to read the widths from the data.frame "datlines" and create a line where the width is equal to the number provided in datlines$width. So far I haven't been able to make it work, even if I specify "size = width" in the geom_hlines call.

我希望使用下面提供的数据创建一个看起来像下面第二张图的图表。但是,我不能得到线的宽度对应于轴“B”,并从数据线$ width获取值。我想要的是geom_hline(或者geom_ribbon?)从data.frame“datlines”中读取宽度,并创建一条宽度等于datlines $ width中提供的数字的行。到目前为止,即使我在geom_hlines调用中指定“size = width”,我也无法使其工作。

Any suggestions? Even if I get the size = width part to mirror my datlines data.frame, the size of the line doesn't correspond to the y-axis.

有什么建议么?即使我得到size = width部分来镜像我的datlines data.frame,该行的大小也不对应于y轴。

I'd be fine if I had to tell ggplot2 the upper and lower boundaries of the grey area, too.

如果我不得不告诉ggplot2灰色区域的上下边界,我会没事的。

dat
  A B      C
1 1 2 Facet1
2 1 3 Facet2
3 2 4 Facet1
4 2 5 Facet2

datlines
  int      C width
1   3 Facet1   1.0
2   4 Facet2   0.5

My data and code for the fist graph below

我的数据和代码如下图所示

dat <- data.frame(A = c(1, 1, 2, 2), B = c(2, 3, 4, 5), C = c("Facet1", "Facet2"))
dat
datlines <- data.frame(int = c(3, 4), C = c("Facet1", "Facet2"), width = c(1, 0.5))
datlines
plot <- ggplot(dat, aes(x = A, y = B)) +  
  geom_hline(data = datlines, aes(yintercept = int), colour = "gray") + geom_point() + facet_grid(. ~ C) 
plot

Graph that results from the code I provided: 在faceted ggplot2中创建一条宽度来自dataframe的线

由我提供的代码产生的图表:

Graph that I want: 在faceted ggplot2中创建一条宽度来自dataframe的线

我想要的图表:

2 个解决方案

#1


2  

use geom_rect

使用geom_rect

dat <- data.frame(A = c(1, 1, 2, 2), B = c(2, 3, 4, 5), 
                  C = c("Facet1", "Facet2"), 
                  width=c(1,0.5), int = c(3, 4))
require(ggplot2)
ggplot(dat, aes(x=A,y=B)) + geom_point() + facet_grid(~C) +
  geom_rect(aes(xmin=-Inf, xmax=Inf, 
                ymin=int-width/2, ymax=int+width/2), fill = 'grey')

在faceted ggplot2中创建一条宽度来自dataframe的线

#2


1  

You can use geom_rect, but you'll have to modify ymin and ymax first:

你可以使用geom_rect,但你必须先修改ymin和ymax:

datlines$int2 <- c(2.5, 3.75)

ggplot() + 
geom_point(data=dat, aes(x = A, y = B)) +  
geom_rect(data=datlines, aes(ymin=int2, ymax=int2+width, xmin=-Inf, xmax=+Inf), fill='grey', alpha=1)  + 
facet_grid(. ~ C) 

#1


2  

use geom_rect

使用geom_rect

dat <- data.frame(A = c(1, 1, 2, 2), B = c(2, 3, 4, 5), 
                  C = c("Facet1", "Facet2"), 
                  width=c(1,0.5), int = c(3, 4))
require(ggplot2)
ggplot(dat, aes(x=A,y=B)) + geom_point() + facet_grid(~C) +
  geom_rect(aes(xmin=-Inf, xmax=Inf, 
                ymin=int-width/2, ymax=int+width/2), fill = 'grey')

在faceted ggplot2中创建一条宽度来自dataframe的线

#2


1  

You can use geom_rect, but you'll have to modify ymin and ymax first:

你可以使用geom_rect,但你必须先修改ymin和ymax:

datlines$int2 <- c(2.5, 3.75)

ggplot() + 
geom_point(data=dat, aes(x = A, y = B)) +  
geom_rect(data=datlines, aes(ymin=int2, ymax=int2+width, xmin=-Inf, xmax=+Inf), fill='grey', alpha=1)  + 
facet_grid(. ~ C)