我刚开始使用plot,但找不到有关如何命名跟踪的相关文档,因此在ggplotly呈现的plot中会出现一个有意义的标签。下面是显示大量示例的ggplotly site。需要什么才能在悬停时显示有意义的标签,而不是后面跟着trace0、trace1等的值。
例如,在第一个图中,如何显示标签以使其显示:
比例:值
总账单:值
理想情况下,我希望直接在R中完成此操作,而不是通过web界面。提前谢谢。
发布于 2015-08-22 04:59:06
也可以在ggplot2转换之后但在将其发送到plotly之前编辑任何绘图地物属性。手动更改图例条目名称的Here is an example。我在这里重复一遍:
df <- data.frame(x=c(1, 2, 3, 4), y=c(1, 5, 3, 5), group=c('A', 'A', 'B', 'B'))
g <- ggplot(data=df, aes(x=x, y=y, colour=group)) + geom_point()
# an intermediate step that `ggplotly` calls
p <- plotly_build(g)
# manually change the legend entry names, which are "trace0", "trace1" in your case
p$data[[1]]$name <- 'Group A'
p$data[[2]]$name <- 'Group B'
# send this up to your plotly account
p$filename <- 'ggplot2-user-guide/custom-ggplot2'
plotly_POST(p)The extended example here更详细地解释了这是如何以及为什么工作的。
请注意,一般而言,图例项名称(例如"trace0“)将是您在数据帧中分组的标签(如在ggplot2中)。
发布于 2015-08-21 05:59:16
使用ggplot2和Plotly可以设置text。你会想要install Plotly and get a key。这里有两个例子。示例一:
data(canada.cities, package="maps")
viz <- ggplot(canada.cities, aes(long, lat)) +
borders(regions="canada", name="borders") +
coord_equal() +
geom_point(aes(text=name, size=pop), colour="red", alpha=1/2, name="cities")
ggplotly()
ggplotly(filename="r-docs/canada-bubble")这会在悬停时生成带有加拿大城市名称的this plot。

示例二:
install.packages("gapminder")
library(gapminder)
ggplot(gapminder, aes(x = gdpPercap, y = lifeExp, color = continent, text = paste("country:", country))) +
geom_point(alpha = (1/3)) + scale_x_log10()
ggplotly(filename="ggplot2-docs/alpha-example")这会产生this plot。

有关更多信息,请参阅有关如何覆盖hover_text元素的R docs或this question。Plotly的原生R允许您将more controls添加到绘图中。谢谢你邀请布赖恩。我们也会在我们的文档中添加一个关于这个的新部分。免责声明:我为Plotly工作。
https://stackoverflow.com/questions/32098836
复制相似问题