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

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

问题描述

我是 plotly 新手,无法找到有关如何命名轨迹的相关文档,因此在 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天全站免登陆