2017-08-09 45 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