【问题标题】:How to add the text to germ-rect and ggplotly?如何将文本添加到germ-rect和ggplotly?
【发布时间】:2019-03-06 07:33:23
【问题描述】:

我想将文本添加到 plotly。例如,代码无法将 mytext 添加到 ply.test 图中?任何人都可以提供帮助。谢谢!

test.dat <- data.frame(xmin=c(0,1.5), ymin=c(-1,-1), xmax=c(1,2), ymax=c(1,1),        col=c("blue", "red"))
ggp.test <- ggplot() + geom_rect(data=test.dat, aes(xmin=xmin, ymin=ymin,    xmax=xmax, ymax=ymax, group = col), fill=test.dat$col) + theme_bw()
ggp.test

# How to add the text as hoverinfo?
ply.test <- plotly_build(ggp.test)
ply.test
mytext <- paste(test.dat$xmin,test.dat$ymin,sep = "")
style(ply.test, text = mytext, hoverinfo = "text", traces = c(0,1 ))

【问题讨论】:

  • 尝试把最后一行代码改成traces = c(1,2)
  • 只需更改痕迹。它没有用。
  • 您想将 mytext 中的文本添加到绘图中,还是希望在您将鼠标悬停在栏上时显示此文本?
  • 是的,我希望我的文字在鼠标悬停在栏上时出现

标签: r ggplot2 plotly


【解决方案1】:

以下代码对我有用:

library(plotly)

test.dat <- data.frame(
 xmin=c(0,1.5), xmax=c(1,2),
 ymin=c(-1,-1), ymax=c(1,1), col=c("blue", "red") )

ggp.test <- ggplot(data=test.dat, 
 aes(xmin=xmin, ymin=ymin, xmax=xmax, ymax=ymax, group = col) ) +
  geom_rect(fill=test.dat$col) +
  theme_bw()

ply.test <- plotly_build(ggp.test)
mytext <- paste(test.dat$xmin,test.dat$ymin,sep = "")
style(ply.test, text = mytext, hoverinfo = "text", traces = c(1,2))

它产生这个:

【讨论】:

  • 哦,天哪,为什么我只有在将鼠标悬停在栏上时才显示 0 、 1 的痕迹。相同的代码
  • 嗨 Lstat,另一个问题。如果我有很多酒吧并且想为每个酒吧添加个人名称,我该怎么办?到目前为止,hoverinfo 似乎依赖于组。
  • 我不确定我是否理解您的要求。在mytext 中重命名每个都行吗?然后使用traces=seq(lenght(mytext))自动激活所有这些。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-31
  • 2022-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-14
相关资源
最近更新 更多