【问题标题】:Adjust geom_text labels using plotly_build使用 plotly_build 调整 geom_text 标签
【发布时间】:2016-06-20 19:00:17
【问题描述】:

我正在使用ggplot2plotlyggthemes 创建数据摘要条形图。这是我的代码:

# data
dput(dat)

structure(list(Source = structure(1:11, .Label = c("GTEx", "Target", 
"RNASeq", "Microarray", "CNA", "Mutation", "HI51", "IH250", "IH251", 
"NB88", "OBER649"), class = "factor"), Type = c("External", "External", 
"Cell Line", "Cell Line", "Cell Line", "Cell Line", "Patient Sample", 
"Patient Sample", "Patient Sample", "Patient Sample", "Patient Sample"
), Samples = c(1641, 162, 41, 29, 34, 29, 51, 250, 251, 88, 649
), Genes = c(52576, 25818, 26770, 19822, 21376, 12716, 21118, 
17768, 17768, 21118, 19858)), .Names = c("Source", "Type", "Samples", 
"Genes"), class = "data.frame", row.names = c(NA, -11L))

library(ggplot2)
library(ggthemes)
library(plotly)
p <- ggplot(data = dat, aes(x = Source, y = Genes)) + 
                      geom_bar(stat="identity",aes(color = Type)) + 
                      theme_bw() + 
                      geom_text(aes(label = Samples, color = Type), position=position_dodge(width=0.9), vjust=-0.25, size=5) + 
                      theme_hc(bgcolor = "darkunica") +
                      scale_colour_hc("darkunica") +
                      theme(axis.title = element_blank(), 
                            axis.text.y = element_blank(), 
                            axis.ticks = element_blank(), 
                            panel.grid = element_blank())
p <- plotly_build(p)
p$layout$plot_bgcolor <- p$layout$paper_bgcolor
p$layout$annotations[[1]]$text <- ""

这是创建的情节:

条上的标签显示样本数。当我将鼠标悬停在上面时 - 它会显示 SourceGenesType。即使我在geom_text 中使用positionvjust 参数,它也不起作用。如何调整条形上方的标签,使其可见,即完全在条形上方或下方?

【问题讨论】:

  • 你可以看看here
  • 产生Error: Aesthetics must be either length 1 or the same as the data (11): label, colour, position, vjust, x, y
  • @dww 将geom_text() 中的内容替换为此geom_text(aes(label = Samples, color = Type), position=position_dodge(width=0.9), vjust=-0.25, size=5)
  • 请注意:这里的数据墨水比不好。
  • @zx8754 你能详细说明一下吗?谢谢

标签: r ggplot2 plotly geom-bar


【解决方案1】:

使用我在plotly_build阶段完成的数据。(没有深色主题)

如果你检查你的 plotly 对象,它有 2 个类别:数据和布局。 绘图的文本元素属于“scatter”类型,具有与条形图相同的 x、y 属性。因此,它们与您的条重叠。

p$data[[1]]$type   ## Prints result as "bar" type for dat$Type "Cell Line"
p$data[[4]]$type   ## Prints result as "scatter" for dat$Type "Cell Line"
p$data[[4]]$mode   ## Prints text's mode as "text"
P$data[[1]]$mode   ## Prints bar's mode as NULL

在 plotly 中,geom_text 不能很好地转换为 plotly 图表。 您需要更改散点类型的 $y(y 轴坐标)(此处的文本映射为散点类型)。

p$data[[4]]$y 
[1] 26770 19822 21376 12716

由于您的 y 轴缩放到 dat$Genes(范围从 0 到 52576),因此将它们的坐标增加/减少到 1 或 10 不会导致明显的差异。因此,您可能想尝试更大的值,例如范围 1000、2000。
或者,您可以编写代码为模式类型“文本”的所有 y 坐标添加一个统一的数量。这是一种方法

for(i in seq(p$data)){ 
  if(!is.null(p$data[[i]]$mode)){                        ## if mode is not NULL
    if(p$data[[i]]$mode =="text"){                       ## above TRUE and if mode is "text"
      p$data[[i]]$y = p$data[[i]]$y + max(dat$Genes)/50  ## add max(dat$Genes)/50 or any addend you wish
    }
  }
}

剧情如下: Plot from above

【讨论】:

    猜你喜欢
    • 2023-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多