问题描述
我正在尝试使用由文本美学定义的工具提示来渲染一个简单的 geom_line.
I'm trying to render a simple geom_line with a tooltip defined by a text aesthetic.
q <- ggplot(data = graphDataFactor(), aes(x = Dates,
y=Variable_Data,
colour = Variable_Name
#,
#text = paste('Date: ', as.Date(Dates),
#'<br>Variable:', Variable_Name,
#'<br>Var Unit:', Variable_Data
#)
)) +
geom_line(size = 1) +
labs(colour = "Variables")
ggplotly(q
#, tooltip = c("text")
)
因此,注释掉文本美学的这段代码可以正常工作并呈现线条.但是,当我尝试使用文本美学和工具提示时,geom_line 不会出现在图中.将鼠标悬停在正确的位置上会显示正确的工具提示,并且颜色图例会正确显示.
So this code with the text aesthetic commented out works fine and renders the line. However, when I try to use the text aesthetic and tooltip, the geom_line does not appear in the plot. Hovering over the correct locations does show the right tooltip, and the colour legend displays properly.
另外,我有一个相同的调用,唯一的区别是我调用 geom_point 而不是 geom_line 并且渲染得非常好.
Also, I have an identical call where the only difference is that I call geom_point rather than geom_line and that renders perfectly fine.
这只是 ggplotly 的错误还是我的代码有问题?
Is this just a bug with ggplotly or is there something wrong with my code?
推荐答案
设置美学时添加group=1
.
library(plotly)
set.seed(1234)
df <- data.frame(Dates = c("01/14/92", "02/27/92", "03/27/92", "04/28/92", "01/03/92"),
Variable_Data=runif(5), Variable_Name=c("A","A","A","B","B"))
df$Dates <- as.Date(df$Dates,"%m/%d/%y")
q <- ggplot(data = df, aes(x=Dates, y=Variable_Data,
colour=Variable_Name, group=1,
text = paste('Date: ', Dates,
'<br>Variable:', Variable_Name,
'<br>Var Unit:', Variable_Data))) +
geom_line(size = 1) + labs(colour = "Variables")
ggplotly(q, tooltip = "text")
这篇关于ggplotly 文本美学导致 geom_line 不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!