我正在尝试在 geom_line 上绘制 geom_point 并根据一系列值(值阈值为 0-8.3 和 8.4-946)对点进行颜色编码。 我已经尝试根据 ggplot geom_point() 使用基于特定离散值的颜色对点进行颜色编码,但图例上仅显示一种颜色(绿色)和一个阈值。我可以看到一些绿点,但看不到红点。 该图也仅显示 1 月份的数据,但时间序列是从 1 月到 8 月。
# Load libraries
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(ggplot2)
library(scales)
# Define start and end times
start_time <- as.POSIXct("2024-01-01 00:00:00")
end_time <- as.POSIXct("2024-08-31 23:59:59")
# Generate a sequence of 15-minute intervals
time_sequence <- seq(start_time, end_time, by = "15 mins")
# 1455 timestamps to match the number of observations
time_sequence <- time_sequence[1:1455]
# Create a "Value" variable
set.seed(123) # For reproducibility
Value <- runif(1455, min = 2.3, max = 946)
# Generate the "Flow" variable
t <- 1:1455 # Sequence representing each time point
Flow <- 5.2 + 4.2 * sin(2 * pi * t / 365)
# Create the data frame
data <- data.frame(
Timestamp = time_sequence,
Value = Value,
Flow = Flow
)
# Create a duplicate data frame for the "Flow" variable
flow_duplicate <- data %>%
select(Timestamp, Flow) # Keep only Timestamp and Flow columns
# Display the first few rows of both data frames
head(data)
#> Timestamp Value Flow
#> 1 2024-01-01 00:00:00 273.68691 5.272296
#> 2 2024-01-01 00:15:00 746.22356 5.344571
#> 3 2024-01-01 00:30:00 388.25152 5.416803
#> 4 2024-01-01 00:45:00 835.60352 5.488970
#> 5 2024-01-01 01:00:00 889.81898 5.561052
#> 6 2024-01-01 01:15:00 45.29167 5.633027
head(flow_duplicate)
#> Timestamp Flow
#> 1 2024-01-01 00:00:00 5.272296
#> 2 2024-01-01 00:15:00 5.344571
#> 3 2024-01-01 00:30:00 5.416803
#> 4 2024-01-01 00:45:00 5.488970
#> 5 2024-01-01 01:00:00 5.561052
#> 6 2024-01-01 01:15:00 5.633027
# plot the values as geom-point on the flow geom_line
ggplot(data = flow_duplicate, aes(Timestamp, Flow)) +
geom_line() + # plot the flow
geom_point(data = data, aes(Timestamp, Flow, colour = cut(Value, c(0, 8.3, 946))),
size = 1) +
scale_color_manual(name = "Value",
values = c("(0,8.3]" = "green",
"(8.3, 945.8]" = "red"),
labels = c("<value threshold (8.3 NTU)", ">value threshold (8.3 NTU)"))+
scale_y_continuous(labels = comma, name = 'Flow (cubic meters/d)') +
theme_bw()
Created on 2024-11-05 with reprex v2.1.0
正如我在评论中提到的,使用
"(8.3,946]" = "red"
应该可以解决红点问题:
library(dplyr, warn=FALSE)
library(ggplot2)
library(scales)
# Define start and end times
start_time <- as.POSIXct("2024-01-01 00:00:00")
end_time <- as.POSIXct("2024-08-31 23:59:59")
# Generate a sequence of 15-minute intervals
time_sequence <- seq(start_time, end_time, by = "15 mins")
# 1455 timestamps to match the number of observations
time_sequence <- time_sequence[1:1455]
# Create a "Value" variable
set.seed(123) # For reproducibility
Value <- runif(1455, min = 2.3, max = 946)
# Generate the "Flow" variable
t <- 1:1455 # Sequence representing each time point
Flow <- 5.2 + 4.2 * sin(2 * pi * t / 365)
# Create the data frame
data <- data.frame(
Timestamp = time_sequence,
Value = Value,
Flow = Flow
)
# Create a duplicate data frame for the "Flow" variable
flow_duplicate <- data %>%
select(Timestamp, Flow) # Keep only Timestamp and Flow columns
# plot the values as geom-point on the flow geom_line
ggplot(data = flow_duplicate, aes(Timestamp, Flow)) +
geom_line() + # plot the flow
geom_point(
data = data, aes(Timestamp, Flow, colour = cut(Value, c(0, 8.3, 946))),
size = 1
) +
scale_color_manual(
name = "Value",
values = c(
"(0,8.3]" = "green",
"(8.3,946]" = "red"
),
labels = c("<value threshold (8.3 NTU)", ">value threshold (8.3 NTU)")
) +
scale_y_continuous(labels = comma, name = "Flow (cubic meters/d)") +
theme_bw()