You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用RShiny+ggplotly通过selectInput为散点图特定点上色

问题

现有包含学生姓名、数学成绩、出勤次数的grades数据集,使用ggplot2绘制成绩与出勤的散点图并转为ggplotly交互式图表后,希望通过Shiny的selectInput选择学生,使对应散点变为红色。但运行代码时报错:mapping must be created by aes()。

错误原因
  1. 试图直接在ggplotly对象(iGraph)上添加ggplot的geom_point()图层,这是不被支持的——ggplotly对象是plotly的可视化结构,无法兼容ggplot的图层语法。
  2. 即使在ggplot对象上操作,原代码中geom_point(filter_data, aes(color= "red"))的写法也存在问题:filter_data是reactive对象,需要通过filter_data()调用;同时aes(color= "red")的写法错误,静态颜色不应放在aes()内部,若要基于条件映射颜色,需在数据层面生成分组变量。
解决方案

在ggplot层面动态处理颜色映射,再转为ggplotly,既保留交互功能,又能实现选中学生的高亮效果。核心思路是:

  • 动态生成分组变量,标记当前选中的学生
  • 将该变量映射到color美学,通过颜色标尺指定高亮颜色
  • 在server中动态生成ggplot对象并转为ggplotly
完整修正代码
library(tidyverse)
library(plotly)
library(shiny)

# 数据集
grades <- data.frame(
  Name = c("A","B","C","D","E","F","G","H","I","J","K","L"),
  math = c(10,15,20,5,18,11,8,3,19,5,6,13),
  participation = c(10,10,10,4,8,5,4,2,8,6,5,6)
)

ui <- fluidPage( 
  titlePanel("Grades and Participation"),
  selectInput(
    inputId = "name", 
    label = "Student's Name",
    choices = grades$Name,
    selected = "A"
  ),
  plotlyOutput("scatterplot")
)

server <- function(input, output, session) {
  # 动态生成带高亮标记的数据集
  highlighted_data <- reactive({
    grades %>%
      mutate(is_selected = ifelse(Name == input$name, "Selected", "Other"))
  })
  
  output$scatterplot <- renderPlotly({
    # 动态生成ggplot图表
    p <- highlighted_data() %>%
      ggplot(aes(math, participation, label = Name, color = is_selected)) +
      geom_point(size = 3) +  # 放大点方便观察高亮效果
      geom_smooth(method = "lm", se = F, color = "gray50") +
      labs(
        title = "Grades and Participation of students", 
        x = "Grades in math", 
        y = "Participation in math's class",
        color = NULL  # 隐藏颜色图例
      ) +
      scale_color_manual(values = c("Other" = "black", "Selected" = "red")) +
      theme_minimal()
    
    # 转为ggplotly并指定tooltip
    ggplotly(p, tooltip = c("Name", "x", "y"))
  })
}

shinyApp(ui, server)
关键改动说明
  • 在highlighted_data reactive对象中,新增is_selected变量,标记当前选中的学生
  • 将is_selected映射到color美学,通过scale_color_manual设置选中点为红色,其他点为黑色
  • 移除颜色图例(可选),避免干扰视觉
  • 动态生成ggplot对象后再转为ggplotly,确保每次选择学生时图表都会重新渲染

内容的提问来源于stack exchange,提问作者Renato

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 11:27:01