r - 工具提示删除回归线 ggplotly

标签 r ggplot2 shiny plotly ggplotly

我在 server.R 中有以下代码:

library(shiny)
library(plotly)

art.data <- read.csv("data1.csv", stringsAsFactors = FALSE)

shinyServer(function(input, output) {

output$distPlot <- renderPlotly({
    col.str <- paste0(input$colspa, ".", input$rgbchoice, ".median")
    p <- ggplot(art.data, aes(x = year, y = art.data[[col.str]], text = paste0(artist, "<br>", art))) + geom_point(size = 1) + xlab("Year") + stat_smooth(method = loess, se = FALSE)
    ggplotly(p , tooltip = "text")
})
})

如果我删除工具提示,则输出图中存在回归线,但包含工具提示时,回归线不会出现在图中。有什么解决方案可以将两者结合在一起?

With tooltip

Without tooltip in ggplotly() and text in aes()

最佳答案

审美 text需要审美 group .
让我们开始考虑这个例子:

art.data <- data.frame(year=1430:1490, y=rnorm(61), 
            artist=sample(LETTERS,61, replace=T), art=sample(letters,61, replace=T))
col.str <- 2

library(ggplot2)
ggplot(art.data, aes(x = year, y = art.data[[col.str]], 
                     text=paste0(artist, "<br>", art))) + 
    geom_point(size = 1) + 
    stat_smooth(method = loess, se = FALSE) +
    xlab("Year") 

在这种情况下,黄土线没有绘制在图表上:

enter image description here

添加 group=1ggplot美学解决了这个问题:
p <- ggplot(art.data, aes(x = year, y = art.data[[col.str]], 
                     text=paste0(artist, "<br>", art), group=1)) + 
    geom_point(size = 1) + 
    stat_smooth(method = loess, se = FALSE) +
    xlab("Year") 
p

enter image description here

ggplotly现在效果很好:
library(plotly)
ggplotly(p, tooltip="text")

enter image description here

关于r - 工具提示删除回归线 ggplotly,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47507186/

相关文章:

r - ggplot2 中的lazyeval 在其他函数中

r - 从 R 包标准修改 `pr_curve` 和 `auc_curve` 的图形大小

r - 如何手动将图例添加到 ggplot 对象

r - 安装 R 包 Ubuntu 22.04.1 LTE 时的包 libcurl4-openssl-dev 问题

r - knitr、开罗和 ggplot2 : font family 'Source Code Pro' not found in PostScript fontdatabase

r - 无法从 Shiny 下载 .png 文件

html - ioslides 中 Shiny 的应用程序大小

r - 无法将类型 'closure' 强制转换为类型 'character' 的向量

R:计算指定时间范围内不同类别的数量

r - 如何每次检查向量 (R) 中是否有 n 个升序/降序值