The name of the legend in the plot

How to specify a title for a legend in plotly? I have a summary histogram that displays different durations, such as 0-10, 11-20. I want the name of the legend to say "Duration".

+4
source share
2 answers

The easiest way to specify a legend title is to set it through ggplotand have it plotlyread from the corresponding object:

library( plotly )

gg <- ggplot( mtcars, aes( x=mpg, y=wt, color=factor(vs) ) ) +
  geom_point() + labs( color = "MyTitle" )
ggplotly( gg )

However, the problem is that it plotlyconverts the legend title into an annotation, which becomes disconnected from the legend in this process. In my browser, it also overlaps with the menu plotlyin the upper right corner:

enter image description here

, ggplot :

gg <- ggplot( mtcars, aes( x=mpg, y=wt, color=factor(vs) ) ) +
  geom_point() + theme( legend.title = element_blank() )
ggplotly( gg ) %>%
  add_annotations( text="MyTitle", xref="paper", yref="paper",
                  x=1.02, xanchor="left",
                  y=0.8, yanchor="bottom",    # Same y as legend below
                  legendtitle=TRUE, showarrow=FALSE ) %>%
  layout( legend=list(y=0.8, yanchor="top" ) )

, y, , . , "" . :

enter image description here

+4

- , , ...

. annotations, . , , x, y xref = 'paper' yref = 'paper'.

, : x y.

annotations. !

+1

All Articles