Ggplotly - R, trace name designation

I am new to the conspiracy and cannot find the relevant documentation on what to name the traces, so a meaningful label appears on the graph displayed by ggplotly . Here is a ggplotly site that shows a series of examples. What you need to show a meaningful label on hover, not a value, followed by trace0, trace1, etc.

For example, in the first figure, how labels can appear so that they appear:

Proportion: value

Final score: value

Ideally, I would like to do this directly in R, and not through the web interface. Thank you in advance.

+6
source share
2 answers

You can also edit any of the properties of the graphic shape after converting ggplot2, but before sending it to the chart. Here is an example that changes the names of legend entries manually. I will 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) 

A detailed example here explains in more detail how and why this works.

Note that in the general case, the names of the legend elements, for example. "trace0" are the labels that you grouped into a dataframe (as in ggplot2).

+4
source

Using ggplot2 and Plotly, you can set text . You will want to install Plotly and get the key . Here are two examples. Example 1:

 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 gives this plot with the name of Canadian cities available in guidance mode.

enter image description here

Example two:

 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") 

What gives this plot .

enter image description here

For more information, see our R docs or this question on how to overwrite the hover_text element. The Plotly native R API allows you to add more controls to your stories. Thanks for asking Brian. We will also add a new section to our docs about this. Disclaimer: I work for Plotly.

+8
source

All Articles