在 Rshiny 中用绘图重叠点和文本

Kom*_*thi 5 r ggplot2 shiny plotly geom-text

我正在使用 Rshiny 的绘图来创建带有文本标签的散点图。下面是一个可重现的示例:

library(ggplot2)
library(plotly)
dat <- data.frame(LongExpressionValue = rnorm(1:100), 
                  LongMethylationValue = rnorm(1:100), 
                  LongCopyNumberValue = rnorm(1:100))

rownames(dat) <- paste0('n',seq(1:100))

# ggplot
p <- ggplot(data = dat, aes(x = LongExpressionValue, y = LongMethylationValue)) + 
  geom_point(size = 2) + geom_smooth(method = lm) +
  geom_text(aes(label = rownames(dat)), vjust=-1.5, size = 3)

# ggplotly
ggplotly(p)
Run Code Online (Sandbox Code Playgroud)

这会创建一个如下图:

在此输入图像描述

如何调整 geom_text 选项以使标签出现在上方并且不与点重叠?我确实想保留我的 ggplot 代码,以便跨应用程序使用它。

谢谢!

Sha*_*ape 3

尝试这个:

plot_ly(
    data = dat, 
    x = ~LongExpressionValue, 
    y = ~LongMethylationValue, 
    text = rownames(dat), 
    marker = list(size = 10), 
    mode = "markers+text",
    textposition = 'top center'
)
Run Code Online (Sandbox Code Playgroud)

当你可以直接访问源代码时,在 ggplot2 上花费太多精力是不值得的。这是无价的:https: //plot.ly/r/reference/

inplot_ly或 in 中的所有内容layout都是列表的列表,因此您可以轻松设置参数(注意marker = list(size = 10)

编辑:稍微复杂一点,展示了悬停信息+文本的强大功能:

plot_ly(
    data = dat, 
    x = ~LongExpressionValue, 
    y = ~LongMethylationValue, 
    text = paste0(rownames(dat), 
                      '<br>A:', 1:nrow(dat), #Examples of additional text
                      '<br>B:', sample(nrow(dat))), #Examples of additional text
        hoverinfo = 'text+x+y',
        marker = list(size = 10), 
        mode = "markers+text",
        textposition = 'top right')
Run Code Online (Sandbox Code Playgroud)