首页 文章

闪亮反应为ggplot2系列名称和图例标签?

提问于
浏览
-2

我希望在闪亮的情节中使用Shiny输入作为名称 .

这是我的代码:

ggplot() + 
    ...
    geom_point(data=data.frame(), aes(x=x, y=y, color=paste(input$name)),
      size = 3) + 
    scale_color_manual(values=c("df1"="blue", "df2"="blue",
      paste(input$name)="red"))

它不会将 paste(input$name) 识别为字符串 . 这是错误消息:

1512:     scale_color_manual(values=c("df1"="blue", "df2"="blue",
1513:       paste(input$name)=
                             ^

有谁知道如何正确构建这个?

1 回答

  • 1

    你不能像你一样混合字符串,符号和表达式 . 如果要在 aes() 映射中使用字符串,请使用 aes_aes_string (不需要 paste

    aes_string(x="x", y="y", color=input$name)
    

    而且你不能在一个命名向量中的 = 左侧放置一个表达式 . 请改用 setNames() 之类的东西 .

    values = setNames(c("blue", "blue", "red"), c("df1", "df2", input$name))
    

    如上所述,如果您包含reproducible example将来会更容易,以便可以正确测试可能的解决方案 . 这根本不是Shiny相关的 . 这就是 ggplot 和R的工作原理 .

相关问题