如何避免在绘图中重复图例,并在R中垂直设置标题



我使用的代码是:

DF <- data.frame(
stringsAsFactors = FALSE,
Month = c("2019-Dec","2019-Dec",
"2019-Dec","2019-Dec","2020-Jan","2020-Jan","2020-Jan", "2020-Jan"),
Week = c(4L, 4L, 5L, 5L, 1L, 1L, 1L, 2L),
Cat = c("A", "B", "A", "C", "A", "B", "C", "A"),
n = c(17L, 6L, 21L, 10L, 19L, 20L, 12L, 14L)
)
fig <- DF %>%
split(DF$Month) %>%
purrr::map(~{
plot_ly(colors = c("A" = "red", "B" = "green", "C" = "blue", "D" = "black")) %>%
add_bars(data = .x,
x = .x$Week,
y = .x$n,
type = 'bar',
split = .x$Cat,
color = .x$Cat,
legendgroup = .x$Cat) %>%
layout(xaxis = list(showticklabels = FALSE, title = unique(.x$Month)))
})
subplot(fig, shareY = TRUE, titleX = TRUE, margin = 0) %>% 
layout(barmode = 'stack', showlegend =TRUE)

我在这里面临的问题是右边的重复图例。我还想要垂直的月份标题,而不是水平的。

提前感谢!

这里有一个解决方案,可以使用带参数的showlegend=FALSE/TRUE来避免重复图例。我不确定plotly是否允许您旋转轴标题。但也许你可以玩单独的轴刻度,它可以旋转?

"%>%" <- magrittr::"%>%"
DF_grp <- DF %>%
split(DF$Month)

p <- lapply(seq_along(DF_grp),function(idx) {
show_legend <- switch(idx,
"1"=TRUE,
"2"=FALSE)

plotly:: plot_ly(colors = c("A" = "red",
"B" = "green",
"C" = "blue",
"D" = "black")) %>%
plotly::add_bars(data=DF_grp[[idx]],
x=~Week,
y=~n,
type="bar",
split=~Cat,
color=~Cat,
legendgroup=~Cat,
showlegend=show_legend) %>%
plotly::layout(xaxis=list(showticklabels=F,
title=unique(DF_grp[[idx]]$Month)),
barmode="stack",
showlegend =TRUE)
})
p %>%
plotly::subplot(shareY = TRUE,
titleX = TRUE,
margin = 0) 

相关内容

最新更新