简体   繁体   中英

How to change the legend position when transfer ggplot2 to plotly using `ggplotly`?

As the code below, I constructed a plotly from a ggplot, and want to change the position of the legend.

df <- data.frame(n=1:10, Var1 = rnorm(10), Var2 = rnorm(10))
data_long <- melt(df, id = "n")
a <- ggplot(data_long, 
            aes(x = n, y = value, colour = variable, group = variable)) + 
      geom_line() 
ggplotly(a) %>% layout(legend = list(x = 0, y = 0), yaxis = list(title = ""))

The ggplot looks like ggplot

But the plotly looks like

plotly

Part of the legend title is still on the top of the graph.

You can use legend.position argument:

a <- ggplot(data_long,
            aes(x = n, y = value, colour = variable, group = variable)) +
    geom_line() +
    theme(legend.justification=c(0,0), legend.position=c(0,0))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM