r - Invert the order of the legend
问题描述
I have written a function to create plotly line graphs for my shiny dashboard. The order of the legends do not seem right. For some reason it is showing reverse of what is in the data frame. I want the category 'other' to be placed last in the legend. Is there a way to change the sort the order of the legend?
plot_maker <- function(df, var, legend_pos = -0.5, legend = TRUE) {
temp <- df %>%
pivot_wider(names_from = Year_month, values_from = count) %>%
ungroup() %>%
mutate_at(vars(contains("20")), function(x) x / sum(x, na.rm = TRUE)) %>%
pivot_longer(!(!!sym(var)), names_to = "Year_month", values_to = "value")
temp <- temp %>% inner_join(df %>%
mutate(Year_month = as.character(Year_month)),
by = c(var, "Year_month")
)
temp$Year_month <- factor(temp$Year_month,
levels = c(unique(temp$Year_month))
)
if (sum(temp$value, na.rm = TRUE) == 0) {
return(plot.new())
} else {
p <- plot_ly(
data = temp,
y = ~value,
x = ~Year_month,
color = ~ (get(var)),
legendgroup = ~ (get(var)),
hoverinfo = "text",
text = ~ paste(
get(var),
"<br>Count:", count,
"<br>PCT:", sprintf("%1.2f%%", 100 * value)
)
) %>%
add_lines() %>%
layout(
yaxis = list(
tickformat = "%",
title = ""
),
xaxis = list(title = ""),
legend = list(
orientation = "h", yanchor = "bottom", y = legend_pos,
font = list(size = 10),
traceorder= 'normal'
),
template = "plotly_dark"
)
if (legend == FALSE){
p <- p %>% layout(showlegend = FALSE)
}
return(p)
}
}
Running the function
plot_maker(df1, "therapy_class")
Here is the test data (df1)
structure(list(therapy_class = structure(c(1L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 5L,
5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L,
7L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L), .Label = c("ALK Inhibitors",
"Anti-VEGF-based therapies", "EGFR TKIs", "EGFR-antibody based therapies",
"Non-platinum-based chemotherapy combinations", "IO-based therapies",
"Platinum-based chemotherapy combinations", "Single agent chemotherapies",
"Other"), class = c("ordered", "factor")), Year_month = structure(c(2020.91666666667,
2021, 2021.08333333333, 2021.16666666667, 2021.25, 2021.33333333333,
2020.91666666667, 2021, 2021.08333333333, 2021.16666666667, 2021.25,
2021.33333333333, 2020.91666666667, 2021, 2021.08333333333, 2021.16666666667,
2021.25, 2021.33333333333, 2021.08333333333, 2021.16666666667,
2020.91666666667, 2021, 2021.08333333333, 2021.16666666667, 2021.25,
2021.33333333333, 2020.91666666667, 2021, 2021.08333333333, 2021.16666666667,
2021.25, 2021.33333333333, 2020.91666666667, 2021, 2021.08333333333,
2021.16666666667, 2021.25, 2021.33333333333, 2020.91666666667,
2021, 2021.08333333333, 2021.16666666667, 2021.25, 2021.33333333333,
2020.91666666667, 2021, 2021.08333333333, 2021.16666666667, 2021.25,
2021.33333333333), class = "yearmon"), count = c(18L, 17L, 16L,
15L, 15L, 15L, 37L, 39L, 38L, 47L, 48L, 32L, 63L, 45L, 64L, 73L,
63L, 57L, 1L, 1L, 6L, 5L, 5L, 12L, 2L, 8L, 312L, 327L, 296L,
371L, 324L, 307L, 127L, 115L, 99L, 148L, 124L, 141L, 69L, 51L,
48L, 66L, 58L, 38L, 45L, 44L, 34L, 43L, 64L, 52L)), row.names = c(NA,
-50L), groups = structure(list(therapy_class = structure(1:9, .Label = c("ALK Inhibitors",
"Anti-VEGF-based therapies", "EGFR TKIs", "EGFR-antibody based therapies",
"Non-platinum-based chemotherapy combinations", "IO-based therapies",
"Platinum-based chemotherapy combinations", "Single agent chemotherapies",
"Other"), class = c("ordered", "factor")), .rows = structure(list(
1:6, 7:12, 13:18, 19:20, 21:26, 27:32, 33:38, 39:44, 45:50), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = c(NA, -9L), class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
解决方案
p <- plot_ly(
data = temp,
y = ~value,
x = ~Year_month,
color = ~ (get(var)),
legendgroup = ~ (get(var)),
hoverinfo = "text",
text = ~ paste(
get(var),
"<br>Count:", count,
"<br>PCT:", sprintf("%1.2f%%", 100 * value)
)
) %>%
add_lines() %>%
layout(
yaxis = list(
tickformat = "%",
title = ""
),
xaxis = list(title = ""),
legend = list(
orientation = "h", yanchor = "bottom", y = legend_pos,
font = list(size = 10),
traceorder= 'reversed'
),
template = "plotly_dark"
)
推荐阅读
- javascript - Promise inside loop get last Item 之后
- azure-functions - Azure 函数 - 防止多次调用
- php - 来自应用程序外部文件夹的 Codeigniter HMVC 错误 Ajax
- algorithm - 是否存在不需要后向边缘的增广路径序列?
- c++ - 如何在 Mac 上调试从 Java 调用的动态库中的代码
- sql - SQL - 复杂的 SQL 查询
- vue.js - 如何将 Bootstrap-Vue 输入的旧值传递给它调用的方法?
- java - java:如果在构造函数中没有参数作为参数,是否有必要调用 super()?
- java - Flink 计时器未按时执行
- java - jackson/json 模式 - 用外部 json 模式生成器模块替换 ISO8601Utils