2017-08-09 82 views
0

以下R代码使用标记创建线条图。我希望创建一个绘图,当在标记上执行悬停时,它会在工具提示中给出“位数”和“sepw”值。此外,标记应该是粗体。请帮忙。添加标记并在R中显示工具提示

digits = 1:150 
sep = iris$Sepal.Length 
sepw = iris$Sepal.Width 
plot_f1 <- ggplot(iris, aes(x = digits)) + 
geom_line(aes(y = sep, color = "red", label = sepw)) + geom_point(y = sep) 
plot_f1 = ggplotly(plot_f1) 
plot_f1 
+2

YOu可以检查[这里](https://stackoverflow.com/questions/38917101/how-do-i-show-the-y-value-on-tooltip-while-hover-in-ggplot2) – akrun

+0

只是' ggplotly(plot_f1,tooltip = c(“x”,“label”))'会起作用 – parth

回答

1

这里是你的问题的第一解决方案:

library(ggplot2) 
library(plotly) 

digits = 1:150 
sep = iris$Sepal.Length 
sepw = iris$Sepal.Width 
plot_f1 <- ggplot(iris, aes(x=digits, y=sep, label=sepw)) + 
      geom_line(color="red") + geom_point() 
plot_f1 <- ggplotly(plot_f1, tooltip=c("x","label")) 
plot_f1 

enter image description here

第二种解决方案是基于text审美:

plot_f2 <- ggplot(data=iris, aes(x=digits, y=sep, group=1, 
        text=paste("Sepw =",sepw,"<br />Digits =",digits))) + 
      geom_line(color="red") + geom_point() 
plot_f2 <- ggplotly(plot_f2, tooltip="text") 
plot_f2 

enter image description here

+0

非常感谢Marco – AK94