在使用ggplotly()时,反转图例顺序。

huangapple go评论69阅读模式
英文:

Reverse the legend order when using ggplotly()

问题

想要颠倒水平条形图的图例顺序。在ggplot中添加guides(fill = guide_legend(reverse = TRUE))可以正常工作(见第二张图)。但是,在应用ggplotly()后,图例又恢复为默认顺序。

如何在改变条的顺序的情况下颠倒plotly图例的顺序?

library(ggplot2)
library(dplyr)
data(mtcars)

p1 <- mtcars %>%
  count(cyl, am) %>%
  mutate(cyl = factor(cyl), am = factor(am)) %>%
  ggplot(aes(cyl, n, fill = am)) +
  geom_col(position = "dodge") +
  coord_flip()
p1

在使用ggplotly()时,反转图例顺序。

p2 <- p1 + guides(fill = guide_legend(reverse = TRUE))
p2

在使用ggplotly()时,反转图例顺序。

plotly::ggplotly(p2)

在使用ggplotly()时,反转图例顺序。

英文:

I would like to reverse the order of the legend for a horizontal bar chart. When adding guides(fill = guide_legend(reverse = TRUE)) to the ggplot it works fine (see second plot). However, after applying ggplotly() the legend is again in the default order.

How to reverse the order of the plotly legend without changing the order of the bars?

library(ggplot2)
library(dplyr)
data(mtcars)

p1 &lt;- mtcars %&gt;%
  count(cyl, am) %&gt;%
  mutate(cyl = factor(cyl), am = factor(am)) %&gt;%
  ggplot(aes(cyl, n, fill = am)) +
  geom_col(position = &quot;dodge&quot;) +
  coord_flip()
p1

在使用ggplotly()时,反转图例顺序。


p2 &lt;- p1 + guides(fill = guide_legend(reverse = TRUE))
p2

在使用ggplotly()时,反转图例顺序。

plotly::ggplotly(p2)

在使用ggplotly()时,反转图例顺序。

答案1

得分: 6

在@Zac Garland的出色答案中添加一个适用于任意长度图例的解决方案:

library(ggplot2)
library(dplyr)

reverse_legend_labels <- function(plotly_plot) {
  n_labels <- length(plotly_plot$x$data)
  plotly_plot$x$data[1:n_labels] <- plotly_plot$x$data[n_labels:1]
  plotly_plot
}

p1 <- mtcars %>%
  count(cyl, am) %>%
  mutate(cyl = factor(cyl), am = factor(am)) %>%
  ggplot(aes(cyl, n, fill = am)) +
  geom_col(position = "dodge") +
  coord_flip()

p2 <- mtcars %>%
  count(am, cyl) %>%
  mutate(cyl = factor(cyl), am = factor(am)) %>%
  ggplot(aes(am, n, fill = cyl)) +
  geom_col(position = "dodge") +
  coord_flip()
p1 %>%
  plotly::ggplotly() %>%
  reverse_legend_labels()

在使用ggplotly()时,反转图例顺序。

p2 %>%
  plotly::ggplotly() %>%
  reverse_legend_labels()

在使用ggplotly()时,反转图例顺序。

英文:

Adding to the great answer of @Zac Garland here is a solution that works with legends of arbitrary length:

library(ggplot2)
library(dplyr)

reverse_legend_labels &lt;- function(plotly_plot) {
  n_labels &lt;- length(plotly_plot$x$data)
  plotly_plot$x$data[1:n_labels] &lt;- plotly_plot$x$data[n_labels:1]
  plotly_plot
}

p1 &lt;- mtcars %&gt;%
  count(cyl, am) %&gt;%
  mutate(cyl = factor(cyl), am = factor(am)) %&gt;%
  ggplot(aes(cyl, n, fill = am)) +
  geom_col(position = &quot;dodge&quot;) +
  coord_flip()

p2 &lt;- mtcars %&gt;%
  count(am, cyl) %&gt;%
  mutate(cyl = factor(cyl), am = factor(am)) %&gt;%
  ggplot(aes(am, n, fill = cyl)) +
  geom_col(position = &quot;dodge&quot;) +
  coord_flip()
p1 %&gt;%
  plotly::ggplotly() %&gt;%
  reverse_legend_labels()

在使用ggplotly()时,反转图例顺序。

p2 %&gt;%
  plotly::ggplotly() %&gt;%
  reverse_legend_labels()

在使用ggplotly()时,反转图例顺序。

答案2

得分: 3

当你调用ggplotly时,实际上只是创建了一个列表和对该列表的函数调用。

因此,如果你保存这个中间步骤,你可以直接修改列表,从而修改绘图输出。

library(ggplot2)
library(dplyr)
data(mtcars)

p1 <- mtcars %>%
  count(cyl, am) %>%
  mutate(cyl = factor(cyl), am = factor(am)) %>%
  ggplot(aes(cyl, n, fill = am)) +
  geom_col(position = "dodge") +
  coord_flip()

html_plot <- ggplotly(p1)

replace_1 <- html_plot[["x"]][["data"]][[2]]
replace_2 <- html_plot[["x"]][["data"]][[1]]
html_plot[["x"]][["data"]][[1]] <- replace_1
html_plot[["x"]][["data"]][[2]] <- replace_2

html_plot

绘图输出

英文:

When you call ggplotly, it's really just creating a list and a function call on that list.

So if you save that intermediate step, you can modify the list directly. and as such, modify the plot output.

library(ggplot2)
library(dplyr)
data(mtcars)

p1 &lt;- mtcars %&gt;%
  count(cyl, am) %&gt;%
  mutate(cyl = factor(cyl), am = factor(am)) %&gt;%
  ggplot(aes(cyl, n, fill = am)) +
  geom_col(position = &quot;dodge&quot;) +
  coord_flip()

html_plot &lt;- ggplotly(p1)

replace_1 &lt;- html_plot[[&quot;x&quot;]][[&quot;data&quot;]][[2]]
replace_2 &lt;- html_plot[[&quot;x&quot;]][[&quot;data&quot;]][[1]]
html_plot[[&quot;x&quot;]][[&quot;data&quot;]][[1]] &lt;- replace_1
html_plot[[&quot;x&quot;]][[&quot;data&quot;]][[2]] &lt;- replace_2

html_plot

plot output

答案3

得分: 1

A simple solution is to define the order of the levels of the factor variable am:

library(ggplot2)
library(dplyr)
data(mtcars)

df <- mtcars %>%
  count(cyl, am) %>%
  mutate(cyl = factor(cyl), am = factor(as.character(am), levels = c("1", "0"))) 

head(df)

p1 <- df %>%
  ggplot(aes(cyl, n, fill = am)) +
  geom_col(position = "dodge") +
  coord_flip()
p1

在使用ggplotly()时,反转图例顺序。

plotly::ggplotly(p1)

在使用ggplotly()时,反转图例顺序。

英文:

A simple solution is to define the order of the levels of the factor variable am:

library(ggplot2)
library(dplyr)
data(mtcars)


df &lt;-  mtcars %&gt;%
  count(cyl, am) %&gt;%
  mutate(cyl = factor(cyl), am = factor(as.character(am), levels = c(&quot;1&quot;, &quot;0&quot;))) 

head(df)


p1 &lt;- df %&gt;%
  ggplot(aes(cyl, n, fill = am)) +
  geom_col(position = &quot;dodge&quot;) +
  coord_flip()
p1

在使用ggplotly()时,反转图例顺序。

plotly::ggplotly(p1)

在使用ggplotly()时,反转图例顺序。

huangapple
  • 本文由 发表于 2020年1月6日 20:13:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/59611914.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定