散点图指定时段数据点差异化着色及多粒度可视化技术问询
解决方案:给不同时间粒度数据的散点图高亮最后N个周期的点
我来帮你搞定这个差异化着色的需求,核心思路是先给数据集添加一个分组标记列,区分出需要高亮的最后N个周期的数据,再把这个分组映射到ggplot的颜色参数上即可。下面分三种时间粒度逐一实现:
日度数据:高亮最后10天的点
首先确保你的日度数据是按日期排序的,然后添加标记列区分最后10天和其他数据,最后在绘图时绑定颜色映射:
# 1. 生成日度示例数据(和你提供的一致) dt.day <- data.table( date = seq(as.Date('2020-01-01'), by = '1 day', length.out = 365), DE = rnorm(365, 4, 1), Austria = rnorm(365, 10, 2), Czechia = rnorm(365, 1, 2), check.names = FALSE ) # 2. 按日期排序(避免数据乱序导致取错最后10天) dt.day <- dt.day[order(date)] # 3. 添加分组标记列:区分最后10天和其他数据 dt.day[, color_group := ifelse(date %in% tail(date, 10), "Last 10 Days", "Other")] # 4. 线性回归(保持你的原有逻辑) regLine <- lm(DE ~ Austria, data = dt.day) # 5. 绘制带回归线的散点图,绑定颜色映射 p <- ggplot(data = dt.day, aes(x = Austria, y = DE, text = paste("Date: ", date, '\n', "Austria: ", Austria, "GWh/h", '\n', "DE: ", DE, "€/MWh"), group = 1, color = color_group)) + # 新增颜色映射 geom_point() + geom_smooth(method = "lm", se = FALSE, color = "#007d3c") + # 回归线颜色保持不变 theme_classic() + theme(panel.background = element_blank()) + xlab("Austria") + ylab("DE") + # 自定义颜色:最后10天用#F07D00,其他用#419F44 scale_color_manual(values = c("Other" = "#419F44", "Last 10 Days" = "#F07D00")) + theme(legend.position = "none") # 若需要显示图例,可删除此行 # 转成plotly交互图 AUSTRIA_DAY <- plotly::ggplotly(p, tooltip = "text")
周度数据:高亮最后4周的点
逻辑和日度完全一致,只是把取最后N个周期的数量改成4:
# 1. 生成周度示例数据(和你提供的一致) dt.week <- data.table( date = seq(as.Date('2020-01-01'), by = '7 day', length.out = 53), Germany = rnorm(53, 4, 1), Austria = rnorm(53, 10, 2), Czechia = rnorm(53, 1, 2), check.names = FALSE ) # 2. 按日期排序 dt.week <- dt.week[order(date)] # 3. 添加分组标记列:区分最后4周和其他数据 dt.week[, color_group := ifelse(date %in% tail(date, 4), "Last 4 Weeks", "Other")] # 4. 线性回归(这里用Germany和Austria为例,你可以按需调整) regLine_week <- lm(Germany ~ Austria, data = dt.week) # 5. 绘制散点图 p_week <- ggplot(data = dt.week, aes(x = Austria, y = Germany, text = paste("Date: ", date, '\n', "Austria: ", Austria, "GWh/h", '\n', "Germany: ", Germany, "€/MWh"), group = 1, color = color_group)) + geom_point() + geom_smooth(method = "lm", se = FALSE, color = "#007d3c") + theme_classic() + theme(panel.background = element_blank()) + xlab("Austria") + ylab("Germany") + scale_color_manual(values = c("Other" = "#419F44", "Last 4 Weeks" = "#F07D00")) + theme(legend.position = "none") # 转成plotly交互图 AUSTRIA_WEEK <- plotly::ggplotly(p_week, tooltip = "text")
小时级数据:高亮最后2天的所有点
小时级数据的date是POSIXct格式,需要先提取日期部分,再筛选最后2天的所有小时记录:
# 1. 生成小时级示例数据(2020年1月1日到1月10日,共240条小时数据) dt.hour <- data.table( date = seq(as.POSIXct("2020-01-01 00:00:00"), by = "1 hour", length.out = 24*10), DE = rnorm(24*10, 4, 1), Austria = rnorm(24*10, 10, 2), check.names = FALSE ) # 2. 按时间排序 dt.hour <- dt.hour[order(date)] # 3. 提取所有唯一日期,取最后2天 last_2_dates <- unique(as.Date(dt.hour$date)) %>% tail(2) # 4. 添加分组标记列:标记属于最后2天的所有小时点 dt.hour[, color_group := ifelse(as.Date(date) %in% last_2_dates, "Last 2 Days", "Other")] # 5. 线性回归 regLine_hour <- lm(DE ~ Austria, data = dt.hour) # 6. 绘制散点图 p_hour <- ggplot(data = dt.hour, aes(x = Austria, y = DE, text = paste("Datetime: ", date, '\n', "Austria: ", Austria, "GWh/h", '\n', "DE: ", DE, "€/MWh"), group = 1, color = color_group)) + geom_point() + geom_smooth(method = "lm", se = FALSE, color = "#007d3c") + theme_classic() + theme(panel.background = element_blank()) + xlab("Austria") + ylab("DE") + scale_color_manual(values = c("Other" = "#419F44", "Last 2 Days" = "#F07D00")) + theme(legend.position = "none") # 转成plotly交互图 AUSTRIA_HOUR <- plotly::ggplotly(p_hour, tooltip = "text")
关键说明
- 所有步骤都先对数据按
date排序,这一步非常重要,避免数据乱序导致取错最后N个周期的记录。 - 如果你需要显示图例区分高亮组和普通组,只需要删除
theme(legend.position = "none")这一行即可。 - 回归线的颜色保持你原来的
#007d3c,不受数据点颜色映射的影响。
内容的提问来源于stack exchange,提问作者Miko
相关产品推荐
相关产品推荐

