R 在ggplot上绘制与线图相交的垂直/水平线

R 在ggplot上绘制与线图相交的垂直/水平线,r,ggplot2,data-visualization,geom-hline,geom-vline,R,Ggplot2,Data Visualization,Geom Hline,Geom Vline,以下代码生成以下绘图: time_step <- c(1:5, 1:5) perceived_signal_slow <- c(1:5, cumsum(1:5)) signal_name <- c("perceived","perceived","perceived","perceived","perceived","accumulated","accumulated","accumulated","accumulated","accumulated") df <-

以下代码生成以下绘图:

time_step <- c(1:5, 1:5)

perceived_signal_slow <- c(1:5, cumsum(1:5))

signal_name <- c("perceived","perceived","perceived","perceived","perceived","accumulated","accumulated","accumulated","accumulated","accumulated")

df <- cbind(perceived_signal_slow, signal_name, time_step)

df <- as.data.frame(df)

df$time_step <- as.numeric(as.character(df$time_step))
df$perceived_signal_slow <- as.numeric(as.character(df$perceived_signal_slow))

ggplot(df, aes(x = time_step, y = signal, colour = signal_name)) +
  geom_line() +
  geom_vline(xintercept = 4, colour = "black", size = 1, alpha = .4) +
  geom_hline(yintercept = 10, colour = "black", size = 1, alpha = .4) +
  geom_hline(yintercept = 4, colour = "black", size = 1, alpha = .4)

time\u step我们可以使用
geom\u段

time_step <- c(1:5, 1:5)

perceived_signal_slow <- c(1:5, cumsum(1:5))

signal_name <- c("perceived","perceived","perceived","perceived","perceived","accumulated","accumulated","accumulated","accumulated","accumulated")

df <- cbind(perceived_signal_slow, signal_name, time_step)

df <- as.data.frame(df)

df$time_step <- as.numeric(as.character(df$time_step))
df$signal <- as.numeric(as.character(df$perceived_signal_slow))

library(ggplot2)
ggplot(df, aes(x = time_step, y = signal, colour = signal_name)) +
  geom_line() +
  geom_segment(x = 4, xend=4, y = -Inf, yend=10, colour = "black", size = 0.2) +
  geom_segment(x= -Inf, xend=4, y = 10, yend = 10, colour = "black", size = 0.2) +
  geom_segment(x= -Inf, xend=4, y = 4, yend = 4, colour = "black", size = 0.2)

time\u步骤可能会将打印区域限制为数据的最小值和最大值(例如(
x=1
5
)?您可以在
ggplot2
帮助中检查
xlim()
ylim()
属性。