ggplotly-R,标记跟踪名称 [英] ggplotly - R, labeling trace names

查看:89
本文介绍了ggplotly-R,标记跟踪名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是情节上的新手,无法找到有关如何命名迹线的相关文档,因此在ggplotly呈现的情节中会出现一个有意义的标签.这是 ggplotly网站,其中显示了许多示例.在悬停时显示有意义的标签,而不是用trace0,trace1等后面的值所需要的内容.

I'm new to plotly and not able to find the relevant documentation on how to name the traces so a meaningful label appears in plot rendered by ggplotly. Here is the ggplotly site that shows a number of examples. What is needed to show a meaningful label on hover instead of the value followed by trace0, trace1, etc.

例如,在第一个图中,标签如何显示以使其显示:

For example, in the first plot, how can the labels appear so it shows:

比例:价值

总账单:价值

理想情况下,我想直接在R中而不是通过Web界面执行此操作.提前致谢.

Ideally, I would like to do this directly in R rather than through the web interface. Thanks in advance.

推荐答案

您还可以在ggplot2转换后 之前发送任何可绘制地物属性密谋. 这里是一个示例,它可以手动更改图例条目名称.我将在这里重复:

You can also edit any of the plotly figure properties after the ggplot2 conversion but before you send it to plotly. Here is an example that changes the legend entry names manually. I'll repeat it here:

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 explains in more detail how and why this works.

请注意,通常 图例项名称,例如"trace0"将成为您在数据框中进行分组的标签(如ggplot2).

Note that in general the legend item names, e.g. "trace0", are going to be the labels that you grouped by in the dataframe (as in ggplot2).

这篇关于ggplotly-R,标记跟踪名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆