在ggplot2中,在两个方面之间画线

时间:2023-02-03 07:33:19

How can I draw several lines between two facets?

如何在两个方面之间画出多条线?

I attempted this by plotting points at the min value of the top graph but they are not between the two facets. See picture below.

我试图通过绘制顶部图的最小值点来实现这一点,但它们不在两个方面之间。见下面的图片。

在ggplot2中,在两个方面之间画线

This is my code so far:

这是我目前的代码:

t <- seq(1:1000)
y1 <- rexp(1000)
y2 <- cumsum(y1)
z <- rep(NA, length(t))
z[100:200] <- 1

df <- data.frame(t=t, values=c(y2,y1), type=rep(c("Bytes","Changes"), each=1000))
points <- data.frame(x=c(10:200,300:350), y=min(y2), type=rep("Bytes",242))
vline.data <- data.frame(type = c("Bytes","Bytes","Changes","Changes"), vl=c(1,5,20,5))

g <- ggplot(data=df, aes(x=t, y=values)) +
  geom_line(colour=I("black")) +
  facet_grid(type ~ ., scales="free") +
  scale_y_continuous(trans="log10") +
  ylab("Log values") +
  theme(axis.text.x = element_text(angle = 90, hjust = 1), panel.margin = unit(0, "lines"))+
  geom_point(data=points, aes(x = x, y = y), colour="green")

g

1 个解决方案

#1


3  

In order to achieve that, you have to set the margins inside the plot to zero. You can do that with expand=c(0,0). The changes I made to your code:

为了实现这个目标,你必须将图内的边距设为0。你可以用展开=c(0,0)我对您的代码所做的更改:

  • When you use scale_y_continuous, you can define the axis label inside that part and you don't need a seperarate ylab.
  • 当您使用scale_y_continuous时,您可以在该部分中定义轴标签,并且不需要单独的ylab。
  • Changed colour=I("black") to colour="black" inside geom_line.
  • 改变颜色=I(“黑色”)到颜色=“黑色”在地纹内。
  • Added expand=c(0,0) to scale_x_continuous and scale_y_continuous.
  • 向scale_x_continuous和scale_y_continuous添加了expand=c(0,0)。

The complete code:

完整的代码:

ggplot(data=df, aes(x=t, y=values)) +
  geom_line(colour="black") +
  geom_point(data=points, aes(x = x, y = y), colour="green") +
  facet_grid(type ~ ., scales="free") +
  scale_x_continuous("t", expand=c(0,0)) +
  scale_y_continuous("Log values", trans="log10", expand=c(0,0)) +
  theme(axis.text.x=element_text(angle=90, vjust=0.5), panel.margin=unit(0, "lines"))

which gives: 在ggplot2中,在两个方面之间画线

这使:


Adding lines can also be done with geom_segment. Normally the lines (segments) will appear in both facets. If you want them to appear between the two facets, you will have to restrict that in data parameter:

添加线也可以使用geom_segment。通常情况下,线段(段)将出现在两个方面。如果您希望它们出现在两个方面之间,您将不得不在数据参数中进行限制:

ggplot(data=df, aes(x=t, y=values)) +
  geom_line(colour="black") +
  geom_segment(data=df[df$type=="Bytes",], aes(x=10, y=0, xend=200, yend=0), colour="green", size=2) +
  geom_segment(data=df[df$type=="Bytes",], aes(x=300, y=0, xend=350, yend=0), colour="green", size=1) +
  facet_grid(type ~ ., scales="free") +
  scale_x_continuous("t", expand=c(0,0)) +
  scale_y_continuous("Log values", trans="log10", expand=c(0,0)) +
  theme(axis.text.x=element_text(angle=90, vjust=0.5), panel.margin=unit(0, "lines"))

which gives: 在ggplot2中,在两个方面之间画线

这使:

#1


3  

In order to achieve that, you have to set the margins inside the plot to zero. You can do that with expand=c(0,0). The changes I made to your code:

为了实现这个目标,你必须将图内的边距设为0。你可以用展开=c(0,0)我对您的代码所做的更改:

  • When you use scale_y_continuous, you can define the axis label inside that part and you don't need a seperarate ylab.
  • 当您使用scale_y_continuous时,您可以在该部分中定义轴标签,并且不需要单独的ylab。
  • Changed colour=I("black") to colour="black" inside geom_line.
  • 改变颜色=I(“黑色”)到颜色=“黑色”在地纹内。
  • Added expand=c(0,0) to scale_x_continuous and scale_y_continuous.
  • 向scale_x_continuous和scale_y_continuous添加了expand=c(0,0)。

The complete code:

完整的代码:

ggplot(data=df, aes(x=t, y=values)) +
  geom_line(colour="black") +
  geom_point(data=points, aes(x = x, y = y), colour="green") +
  facet_grid(type ~ ., scales="free") +
  scale_x_continuous("t", expand=c(0,0)) +
  scale_y_continuous("Log values", trans="log10", expand=c(0,0)) +
  theme(axis.text.x=element_text(angle=90, vjust=0.5), panel.margin=unit(0, "lines"))

which gives: 在ggplot2中,在两个方面之间画线

这使:


Adding lines can also be done with geom_segment. Normally the lines (segments) will appear in both facets. If you want them to appear between the two facets, you will have to restrict that in data parameter:

添加线也可以使用geom_segment。通常情况下,线段(段)将出现在两个方面。如果您希望它们出现在两个方面之间,您将不得不在数据参数中进行限制:

ggplot(data=df, aes(x=t, y=values)) +
  geom_line(colour="black") +
  geom_segment(data=df[df$type=="Bytes",], aes(x=10, y=0, xend=200, yend=0), colour="green", size=2) +
  geom_segment(data=df[df$type=="Bytes",], aes(x=300, y=0, xend=350, yend=0), colour="green", size=1) +
  facet_grid(type ~ ., scales="free") +
  scale_x_continuous("t", expand=c(0,0)) +
  scale_y_continuous("Log values", trans="log10", expand=c(0,0)) +
  theme(axis.text.x=element_text(angle=90, vjust=0.5), panel.margin=unit(0, "lines"))

which gives: 在ggplot2中,在两个方面之间画线

这使: