如何将第二个 x 轴添加到 Plotly 子图

时间:2021-05-27 15:17:44

标签: r plotly r-plotly

如何添加第二个 x 轴来显示条形图的组名?

由于用图像更容易解释,我将当前阶段包括在左侧,我想在右侧创建情节。创建左侧图的代码在底部。

主要我想知道如何将mode1、mode2、mode3、mode4子组添加到x轴。

一些奖励积分:

  • 用图标代替星星
  • 在图表右下角放置版权文本
  • 在图表左下角放置说明文字。

我设法添加了带有注释的文本,但它们并不总是完美定位,而且我还必须使用 orca 导出情节,然后有时这些文本会被切断。所以我的问题是,如何将它们完美地定位在底角上?

enter image description here

代码:

library(data.table)
library(plotly)

## DATA ################

mobil_indic <- structure(list(
  time_labels = structure(c(5L, 4L, 2L, 7L, 6L, 1L, 3L, 8L, 7L, 4L, 2L, 3L, 1L, 6L, 5L, 3L, 1L, 2L, 5L, 4L, 6L, 
                            4L, 1L, 2L, 3L, 5L, 7L, 7L, 5L, 6L, 8L, 4L, 8L, 6L, 7L, 5L, 4L, 4L, 3L, 2L, 6L, 5L, 6L, 3L, 4L, 2L, 5L, 1L), 
                          .Label = c("timegroup8","timegroup7", "timegroup6", "timegroup5", "timegroup4", "timegroup3", 
                                     "timegroup2", "timegroup1"), class = c("ordered", "factor")), 
  mode = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                     1L, 1L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), 
                   .Label = c("mode1", "mode2","mode3", "mode4"), class = c("ordered", "factor")), 
  process_id = c(1969L, 1969L, 1969L, 1969L, 1969L, 1969L, 1969L, 1970L, 1970L, 1970L, 
                 1970L, 1970L, 1970L, 1970L, 1970L, 1969L, 1969L, 1969L, 1969L, 1969L, 1970L, 1970L, 1970L, 1970L,
                 1970L, 1970L, 1970L, 1969L, 1969L, 1969L, 1969L, 1969L, 1970L, 1970L, 1970L, 1970L, 1970L, 
                 1969L, 1969L, 1969L, 1969L, 1969L, 1970L, 1970L, 1970L, 1970L, 1970L, 1970L), 
  anteil = c(4, 30, 32, 5, 5, 18, 6, 0, 3, 20, 21, 13, 31, 7, 5, 7, 80, 8, 3, 2, 0, 2, 84, 6, 7, 1, 0, 33, 
             16, 25, 11, 15, 9, 26, 23, 19, 23, 32, 32, 20, 4, 11, 4, 33, 30, 20, 9, 3), 
  colors = c("#A4CFE5", "#538C4A", "#E7877D", "#3F83C1", "#72B6E0", "#953735", "#7EB24E", "#326C9F", "#3F83C1", 
             "#538C4A", "#E7877D", "#7EB24E", "#953735", "#72B6E0", "#A4CFE5", "#7EB24E", "#953735", "#E7877D", "#A4CFE5", "#538C4A", "#72B6E0", 
             "#538C4A", "#953735", "#E7877D", "#7EB24E", "#A4CFE5", "#3F83C1", "#3F83C1", "#A4CFE5", "#72B6E0", "#326C9F", "#538C4A", "#326C9F", 
             "#72B6E0", "#3F83C1", "#A4CFE5", "#538C4A", "#538C4A", "#7EB24E", "#E7877D", "#72B6E0", "#A4CFE5", "#72B6E0", "#7EB24E", "#538C4A", 
             "#E7877D", "#A4CFE5", "#953735")), 
  class = "data.frame", row.names = c(NA, -48L))

npermode <- structure(list(
  process_id = c("1969", "1970", "1969", "1970", "1969", "1970", "1969", "1970"),
  mode = structure(c(4L, 4L, 1L, 1L, 2L, 2L, 3L, 3L), .Label = c("mode1", "mode2", "mode3", "mode4"), class = c("ordered","factor")), 
  sum_time = c(550, 848, 4583.35, 7075, 1650, 2547, 1645, 2365), 
  n = c(58L, 79L, 58L, 79L, 58L, 79L, 58L, 79L), 
  labels = c("mode4", "mode4", "mode1", "mode1", "mode2", "mode2", "mode3", "mode3"),
  type = c("Type1", "Type2", "Type1", "Type2", "Type1", "Type2","Type1", "Type2"), 
  xlabs = c("Type1\n(n=58)", "Type2\n(n=79)", "Type1\n(n=58)", "Type2\n(n=79)", 
            "Type1\n(n=58)", "Type2\n(n=79)", "Type1\n(n=58)", "Type2\n(n=79)")), 
  class = "data.frame", row.names = c(NA, -8L))

## WRANGLING ################
setDT(mobil_indic)
setDT(npermode)

mobil_indic[, process_id := as.character(process_id)]
mobil_indic <- merge(mobil_indic, npermode, by=c("process_id","mode"))

## Plot ###########
mobil_indic$showlegend = FALSE
mobil_indic[mobil_indic$mode == mobil_indic$mode[[1]],]$showlegend = TRUE
p <- lapply(split(mobil_indic, mobil_indic$mode), function(x) {
  x <- x %>% arrange(time_labels, mode)
  plot_ly(data = x, hoverinfo = "text") %>%
    add_trace(type = "bar",
              x=~xlabs,
              y=~anteil,
              name = ~time_labels,
              showlegend = ~showlegend[[1]],
              legendgroup = ~time_labels,
              marker = list(color = ~colors, fontsize = 12, line = list(width = 1.2, color = 'white')),
              text = ~paste0(anteil, "%"),
              textposition = 'inside', insidetextanchor = "middle",
              hovertext = ~sprintf("<b>Mode</b>: %s\n<b>time</b>: %s\n<b>perc</b>: %s%%",
                                   mode, time_labels, anteil)
    )
})

subplot(p, nrows = 1, shareY = TRUE) %>%
  plotly::layout(title = paste0("<b>Some Title</b>"),
                 barmode = "stack")

2 个答案:

答案 0 :(得分:1)

您可以使用注释

mobil_split = split(mobil_indic, mobil_indic$mode)
labs = names(mobil_split)

p <- lapply(labs, function(lab) {
  x = mobil_split[[lab]]
  x <- x %>% arrange(time_labels, mode)
  plot_ly(data = x, hoverinfo = "text") %>%
    add_trace(type = "bar",
              x=~xlabs,
              y=~anteil,
              name = ~time_labels,
              showlegend = ~showlegend[[1]],
              legendgroup = ~time_labels,
              marker = list(color = ~colors, fontsize = 12, line = list(width = 1.2, color = 'white')),
              text = ~paste0(anteil, "%"),
              textposition = 'inside', insidetextanchor = "middle",
              hovertext = ~sprintf("<b>Mode</b>: %s\n<b>time</b>: %s\n<b>perc</b>: %s%%",
                                   mode, time_labels, anteil)
    ) %>%
    layout(annotations = list(x = 0.5 , y = -5, text = lab, showarrow = F))
})

subplot(p, nrows = 1, shareY = TRUE) %>%
  layout(title = paste0("<b>Some Title</b>"), barmode = "stack"
  )

enter image description here

答案 1 :(得分:0)

这应该有效:

mobil_split = split(mobil_indic, mobil_indic$mode)
labs = names(mobil_split)
p <- lapply(labs, function(lab) {
  x = mobil_split[[lab]]
  x <- x %>% arrange(time_labels, mode)
  plot_ly(data = x, hoverinfo = "text") %>%
    add_trace(type = "bar",
              x=~xlabs,
              y=~anteil,
              name = ~time_labels,
              showlegend = ~showlegend[[1]],
              legendgroup = ~time_labels,
              marker = list(color = ~colors, fontsize = 12, line = list(width = 1.2, color = 'white')),
              text = ~paste0(anteil, "%"),
              textposition = 'inside', insidetextanchor = "middle",
              hovertext = ~sprintf("<b>Mode</b>: %s\n<b>time</b>: %s\n<b>perc</b>: %s%%",
                                   mode, time_labels, anteil)
    ) %>% 
    plotly::layout(xaxis = list(title=lab))
})

subplot(p, nrows = 1, shareY = TRUE, titleX = TRUE) %>%
  plotly::layout(title = paste0("<b>Some Title</b>"),
                 barmode = "stack")