使用多个几何时如何消除 ggplotly 重复的图例条目

2024-01-09

我正在尝试在 R 中制作一个分面绘图线图,无论有多少个组可用于分面,它都会生成一个图。运行以下代码时,我得到了一个理想的绘图,如下图所示

# R 3.6.0
# most of these packages not necessary 

library(shiny)
library(dplyr)
library(ggplot2)
library(bslib)
library(plotly)
library(readxl)
library(janitor)
library(tidyr)
library(lubridate)
library(DT)
library(stringr)
library(scales)
library(shinydashboard)
library(shinyBS)

times <- sample_data %>% 
  mutate(pre_start_time = if_else(pre_start_time == 999,
                                  NA_real_,
                                  pre_start_time),
         pre_end_time = if_else(pre_end_time == -999,
                                NA_real_,
                                pre_end_time))
plot <-
  sample_data %>%
  ggplot() +
  facet_wrap(~Group) +
  geom_line(aes(x = hour_ending,
                y = actual,
                color = "Actual"),
            linetype = 1) +
  geom_line(aes(x = hour_ending,
                y = baseline,
                color = 'Predicted'),
            linetype = 2) +
  scale_color_manual(values = c("grey","orange")) +
  labs(y = "Average", x = "Hour Ending") +
  theme(
    text = element_text(size = 14),
    strip.background = element_rect(fill = "white"),
    panel.grid.minor = element_blank(),
    panel.grid.major.x = element_blank()
  )
ggplotly(p = plot)

然而,当添加阴影矩形时,我得到每个 X 图中所有 4 个变量的重复图例条目。有谁知道为什么这个问题只在使用多个几何体时出现,以及如何解决它?我真的很感激任何见解! (样本数据在底部)

times <- sample_data %>% 
  mutate(pre_start_time = if_else(pre_start_time == 999,
                                  NA_real_,
                                  pre_start_time),
         pre_end_time = if_else(pre_end_time == -999,
                                NA_real_,
                                pre_end_time))
plot <-
  sample_data %>%
  ggplot() +
  facet_wrap(~Group) +
  geom_rect(aes(xmin = start_time,
                xmax = end_time,
                fill = "Event"),
  ymin = -9999,
  ymax = 9999,
  color = NA,
  alpha = .5,
  data = times) +
  geom_rect(aes(xmin = pre_start_time,
                xmax = pre_end_time,
                fill = "Pre"),
  ymin = -9999,
  ymax = 9999,
  color = NA,
  alpha = .5,
  data = times) +
  scale_fill_manual(values = c("green","blue")) +
  geom_line(aes(x = hour_ending,
                y = actual,
                color = "Actual"),
            linetype = 1) +
  geom_line(aes(x = hour_ending,
                y = baseline,
                color = 'Predicted'),
            linetype = 2) +
  scale_color_manual(values = c("grey","orange")) +
  labs(y = "Average", x = "Hour Ending") +
  theme(
    text = element_text(size = 14),
    strip.background = element_rect(fill = "white"),
    panel.grid.minor = element_blank(),
    panel.grid.major.x = element_blank()
  )
ggplotly(p = plot)
SAMPLE DATA:
sample_data <- structure(list(Group = c("A", "A", "A", "A", "A", "A", "A", "A", 
"A", "A", "A", "A", "A", "A", "A", "A", "B", "B", "B", "B", "B", 
"B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "C", "C", 
"C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", 
"C", "D", "D", "D", "D", "D", "D", "D", "D", "D", "D", "D", "D", 
"D", "D", "D", "D", "E", "E", "E", "E", "E", "E", "E", "E", "E", 
"E", "E", "E", "E", "E", "E", "F", "F", "F", "F", "F", "F", "F", 
"F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "G", "G", "G", 
"G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", 
"G", "G", "H", "H", "H", "H", "H", "H", "H", "H", "H", "H", "H", 
"H", "H", "H"), hour_ending = c(9, 10, 11, 12, 13, 14, 15, 16, 
17, 18, 19, 20, 21, 22, 23, 24, 9, 10, 11, 12, 13, 14, 15, 16, 
17, 18, 19, 20, 21, 22, 23, 24, 9, 10, 11, 12, 13, 14, 15, 16, 
17, 18, 19, 20, 21, 22, 23, 24, 9, 10, 11, 12, 13, 14, 15, 16, 
17, 18, 19, 20, 21, 22, 23, 24, 9, 10, 11, 12, 13, 14, 15, 16, 
17, 18, 19, 20, 21, 22, 23, 24, 9, 10, 11, 12, 13, 14, 15, 16, 
17, 18, 19, 20, 21, 22, 23, 24, 9, 10, 11, 12, 13, 14, 15, 16, 
17, 18, 19, 20, 21, 22, 23, 24, 9, 10, 11, 12, 13, 14, 15, 16, 
17, 18, 19, 20, 21, 22, 23, 24), baseline = c(1.9077010172899, 
2.12655999407884, 2.41197416985174, 2.74162673465276, 3.02372981823073, 
3.33089700968182, 3.57394723410791, 3.15735222677014, 3.37064153193956, 
3.46202303231075, 3.2988268978525, 3.19380273795301, 3.39494847100847, 
3.16423855031123, 2.87590770187347, 2.62242044755453, 1.91837969817177, 
2.09605587597684, 2.35400325616559, 2.69368678708331, 2.95211113587406, 
3.27717322458092, 3.52177416324967, 3.10825406915911, 3.30992929450657, 
3.34630856600525, 3.24255670023855, 3.13894842902663, 3.38353563402117, 
3.16377736634442, 2.90389899178434, 2.58154912964004, 1.75202643782428, 
1.95987962050445, 2.25717577193271, 2.59545475336207, 2.89186581538241, 
3.18750480835101, 3.39609229031208, 3.08689595841167, 3.27283764354886, 
3.35569821773519, 3.22664274768324, 3.06136265180325, 3.32439657273435, 
3.13113242596454, 2.86702712497353, 2.55973838647848, 1.75109886182889, 
1.94067969528731, 2.21892615109579, 2.5357375096388, 2.85580015808717, 
3.16838503064795, 3.36737713160428, 3.01073017248253, 3.20380008801146, 
3.26592992821696, 3.15218179832372, 2.98931304543278, 3.26860865393324, 
3.09314311622858, 2.81770320200115, 2.51619056244461, 1.57306451648199, 
1.7696873344036, 2.02487555543073, 2.30853526084836, 2.57574304849849, 
2.83580654878125, 3.04490645265477, 2.85697900314717, 2.98071356731528, 
3.02137729426674, 2.9368021490555, 2.76006912238545, 2.84629853167221, 
2.6574544153176, 2.43116080779831, 2.19829301763476, 1.58537647490725, 
1.76528344218373, 2.03646190364937, 2.33758795942106, 2.62238206256903, 
2.86905933372095, 3.07361064877158, 2.8195083174373, 2.93217255361853, 
2.98756609399138, 2.88778793775513, 2.7356017384249, 2.86767530092669, 
2.69537094784409, 2.45836024615866, 2.21894264887326, 1.62245774927177, 
1.82860802815701, 2.07450744510814, 2.34192587241523, 2.62803024490283, 
2.91665747411445, 3.13200560169809, 2.88707311113673, 3.03644647227885, 
3.08254098145638, 2.9531395609934, 2.81382526166363, 2.90802138670738, 
2.76746258746592, 2.50629159744597, 2.2731739841822, 1.59609295943048, 
1.79453326340054, 2.0363530676287, 2.31854313612052, 2.60925295344696, 
2.88800151673408, 3.11099128741761, 2.84878965949279, 2.98275723405751, 
3.03282006535505, 2.93744358114238, 2.74668563536776, 2.87923567296551, 
2.69574807527118, 2.45871301999625, 2.22632476442406), actual = c(1.8904080196975, 
2.12062258177981, 2.40065072106929, 2.74594090749209, 3.04580372845586, 
3.34841013014421, 3.60839254308829, 3.19027435807246, 3.43037284558565, 
3.51726697150897, 2.14815335912768, 2.64856841364756, 3.92739711572283, 
3.39669011607457, 3.01470981357721, 2.69987337319733, 1.91719711853307, 
2.10992796332678, 2.3850884086444, 2.72041584806811, 2.99648002619515, 
3.31674197773412, 3.57856254092993, 3.16398821218075, 3.34720366732155, 
2.51604780615586, 2.8949115913556, 2.84865094957433, 3.88275703994761, 
3.42126719056974, 3.04166339227243, 2.70566142763589, 1.74723597867678, 
1.96971125137967, 2.26705156481774, 2.61576501253214, 2.90683435389552, 
3.20529863045216, 3.42776313034561, 3.12242460438547, 3.29840802246858, 
3.37654919425327, 1.89375336897544, 2.39456811321649, 3.91047356982817, 
3.39165267309245, 2.99897419306207, 2.67030894372122, 1.74757193816885, 
1.9339774078478, 2.22320927467301, 2.5438192627824, 2.87210939357907, 
3.18768014268728, 3.3974066587396, 3.04831985731272, 3.23633650416171, 
1.92882520808561, 2.53342449464923, 2.59724613555291, 3.88369797859691, 
3.38403091557669, 2.998112960761, 2.65884542211653, 1.55533834586466, 
1.76554958825636, 2.018141783029, 2.30549588256355, 3.50484783387039, 
3.45882563551736, 3.43867525957752, 2.08158968850698, 2.741858216971, 
2.97298961689939, 1.88632653061224, 2.24816684568564, 3.24166129609739, 
2.89050841389187, 2.50857500895095, 2.23477980665951, 1.58329600597238, 
1.76384845091452, 2.05471071295259, 2.35306084359836, 3.54227696901829, 
3.48377006345651, 3.46728630085853, 2.09081746920493, 2.72126913027249, 
1.87875326614408, 2.36983949234789, 2.42112355356476, 3.29287420679358, 
2.96752892870474, 2.55674132138858, 2.25788353863382, 1.62540127840909, 
1.82145596590909, 2.07686079545455, 2.35802201704545, 2.63303267045455, 
2.94107954545455, 3.14693181818182, 2.90647017045455, 3.05628196022727, 
3.10622869318182, 2.20209872159091, 2.02112926136364, 3.33937855113636, 
2.991015625, 2.60694957386364, 2.30830610795455, 1.58597605224964, 
1.79940856313498, 2.03201378809869, 2.32591436865022, 2.61089985486212, 
2.89880624092888, 3.13890420899855, 2.86659288824383, 2.99750362844702, 
2.44048984034833, 2.24725326560232, 2.20473149492017, 3.35098330914369, 
3.00576560232221, 2.635, 2.322793904209), pre_start_time = c(999, 
999, 999, 999, 999, 999, 999, 999, 999, 999, 999, 999, 999, 999, 
999, 999, 999, 999, 999, 999, 999, 999, 999, 999, 999, 999, 999, 
999, 999, 999, 999, 999, 999, 999, 999, 999, 999, 999, 999, 999, 
999, 999, 999, 999, 999, 999, 999, 999, 999, 999, 999, 999, 999, 
999, 999, 999, 999, 999, 999, 999, 999, 999, 999, 999, 12, 12, 
12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 
12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 999, 
999, 999, 999, 999, 999, 999, 999, 999, 999, 999, 999, 999, 999, 
999, 999, 999, 999, 999, 999, 999, 999, 999, 999, 999, 999, 999, 
999, 999, 999, 999, 999), pre_end_time = c(-999, -999, -999, 
-999, -999, -999, -999, -999, -999, -999, -999, -999, -999, -999, 
-999, -999, -999, -999, -999, -999, -999, -999, -999, -999, -999, 
-999, -999, -999, -999, -999, -999, -999, -999, -999, -999, -999, 
-999, -999, -999, -999, -999, -999, -999, -999, -999, -999, -999, 
-999, -999, -999, -999, -999, -999, -999, -999, -999, -999, -999, 
-999, -999, -999, -999, -999, -999, 15, 15, 15, 15, 15, 15, 15, 
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 
15, 15, 15, 15, 15, 15, 15, 15, 15, -999, -999, -999, -999, -999, 
-999, -999, -999, -999, -999, -999, -999, -999, -999, -999, -999, 
-999, -999, -999, -999, -999, -999, -999, -999, -999, -999, -999, 
-999, -999, -999, -999, -999), start_time = c(18, 18, 18, 18, 
18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 17, 17, 17, 17, 
17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 
18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 17, 17, 17, 17, 
17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 
18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 17, 17, 17, 17, 
17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 
18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 17, 17, 17, 17, 
17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17), end_time = c(20, 
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20)), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -128L))

基本上在plotly人们可以通过legendgroup争论。不幸的是,我不知道有任何方法可以通过ggplotly.

然而,适应this https://stackoverflow.com/a/59615148/12993861回答你的情况,实现你想要的结果的一种选择是操纵plotly像这样的对象:

注意:我在代码中添加了一些更多的解释性注释作为注释。


library(plotly)
library(dplyr)

plot <-
  sample_data %>%
  ggplot() +
  facet_wrap(~Group) +
  geom_rect(aes(
    xmin = start_time,
    xmax = end_time,
    fill = "Event"
  ),
  ymin = -9999,
  ymax = 9999,
  color = NA,
  alpha = .5,
  data = times
  ) +
  geom_rect(aes(
    xmin = pre_start_time,
    xmax = pre_end_time,
    fill = "Pre"
  ),
  ymin = -9999,
  ymax = 9999,
  color = NA,
  alpha = .5,
  data = times
  ) +
  scale_fill_manual(values = c("green", "blue")) +
  geom_line(aes(
    x = hour_ending,
    y = actual,
    color = "Actual"
  ),
  linetype = 1
  ) +
  geom_line(aes(
    x = hour_ending,
    y = baseline,
    color = "Predicted"
  ),
  linetype = 2
  ) +
  scale_color_manual(values = c("grey", "orange")) +
  labs(y = "Average", x = "Hour Ending") +
  theme(
    text = element_text(size = 14),
    strip.background = element_rect(fill = "white"),
    panel.grid.minor = element_blank(),
    panel.grid.major.x = element_blank()
  )

gp <- ggplotly(p = plot)

for (i in seq_along(gp$x$data)) {
  # Is the layer the first entry of the group?
  is_first <- grepl("^\\(.*?,1\\)", gp$x$data[[i]]$name)
  # Extract the group identifier and assign it to the name and legendgroup arguments
  gp$x$data[[i]]$name <- gsub("^\\((.*?),\\d+\\)", "\\1", gp$x$data[[i]]$name)
  gp$x$data[[i]]$legendgroup <- gp$x$data[[i]]$name
  # Show the legend only for the first layer of the group 
  if (!is_first) gp$x$data[[i]]$showlegend <- FALSE
}
gp
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用多个几何时如何消除 ggplotly 重复的图例条目 的相关文章

  • 如何将旋转的 NetCDF 转换回正常的纬度/经度网格?

    我有一个带有旋转坐标的 NetCDF 文件 我需要将其转换为正常的纬度 经度坐标 经度为 180到180 纬度为 90到90 library ncdf4 nc open dat nf 对于尺寸 它显示 1 5 variables exclu
  • 计算 R 行中的非零条目数

    我有以下类型的数据 mode1 mode2 mode3 1 8 1 0 2 0 0 0 3 6 5 4 4 1 2 3 5 1 1 1 数据使用dput structure list mode1 c 8L 0L 6L 1L 1L mode2
  • 如何像在facet_grid中一样在facet_wrap中定位条带标签

    我想在使用时删除多余的条带标签facet wrap 并用两个变量进行分面 并且都是自由尺度的 例如 这个facet wrap下图的版本 library ggplot2 dt lt txhousing txhousing year in 20
  • 在 R 上安装 TDA 包时出错:目标“diag.o”的配方失败

    使用 Ubuntu 16 04 和 R 3 4 1 安装 R 包 TDA 时收到错误消息 它似乎与制作 CGAL diag cpp 和 或 diag o 最后的完整错误打印输出 有关 我仔细看了这个 在 R 上安装 TDA 包时出错 htt
  • 获取包含矩阵行内最大值的列名称,该矩阵在数组内包含单独的最大值

    例如给出 dim1 lt c P PO C T dim2 lt c LL RR R Y dim3 lt c Jerry1 Jerry2 Jerry3 Q lt array 1 48 c 4 4 3 dimnames list dim1 di
  • R 数据结构的运算效率

    我想知道是否有任何关于操作效率的文档R 特别是那些与数据操作相关的 例如 我认为向数据框添加列是有效的 因为我猜您只是向链接列表添加一个元素 我想添加行会更慢 因为向量保存在数组中C level你必须分配一个新的长度数组n 1并将所有元素复
  • 条件和分组 mutate dplyr

    假设我有以下每个抽屉库存增加的数据 gt socks year drawer nbr sock total 1990 1 2 1991 1 2 1990 2 3 1991 2 4 1990 3 2 1991 3 1 我想要一个二进制变量来标
  • 在 RGL 中将立方体绘制到 3D 散点图中

    我正在尝试向 3D 散点图添加较小的立方体 网格 具有指定边长 我希望立方体位于原点 我该怎么做呢 我已经玩过cube3d 但我似乎无法将立方体正确定位 也无法使其成为网格 因此我可以看到它包含的数据点 这是我所拥有的 library rg
  • 如何使用plotmath更新ggplot图例标签

    我正在尝试更新ggplot要使用的图例标签plotmath但是 当我这样做时 它将之前组合的图例分成两部分 通过一个例子可能更容易理解 test data and the default plot gives the correct col
  • 闪亮井板宽度

    library shiny library shinydashboard ui lt dashboardPage dashboardHeader dashboardSidebar dashboardBody wellPanel tags d
  • devtools::test() 有效,但 devtools::check() 无效。为什么?

    我正在测试this https github com beanumber etlR 包 以下三件事就可以正常工作 devtools test devtools test file tests testthat R Travis CI 与 R
  • R 颜色 - 许多独特的颜色仍然很漂亮

    我很好奇你是否有一些关于 R 中颜色酿造的技巧 对于许多独特的颜色 在某种程度上图形仍然好看 我需要相当数量的独特颜色 至少 24 种 可能需要更多 50 种 用于堆叠区域图 所以不是热图 渐变颜色不起作用 我发现了 viridis 它的调
  • rpart 决策树中的 rel 误差和 x 误差有什么区别? [关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 我有一个来自 UCI 机器学习数据库的纯分类数据框https archive ics uci edu ml datasets Diabet
  • R:根据列名部分匹配计算行平均值

    我有一个看起来像这样的表 er er 1 as as 1 as 2 rt op a 1 6 90 8 6 4 87 b 1 8 56 7 5 5 9 c 8 7 6 4 5 9 6 d 1 0 8 6 4 3 6 e 9 7 2 4 3 8
  • 使用非标准评估公式

    我正在创建一个使用的包非标准评价 http adv r had co nz Computing on the language html跟踪列的含义 该包在函数之间传递数据框 这些函数执行同一组列的各种操作 非标准评估对此非常有用 my s
  • 上传到 Shiny 服务器后在 R 中解压文件

    我正在尝试在我们的本地服务器上使用 Shiny 来构建一个应用程序 允许用户上传包含 ESRI shapefile 和关联文件的 zip 文件 Shiny 服务器的 fileInput 可以获取数据 并且当它这样做时 它会将其存储在临时目录
  • 在 R 中显示变量的精确值

    gt x lt 1 00042589212565 gt x 1 1 000426 如果我想打印的确切值x 我该怎么办呢 抱歉 如果这是一个愚蠢的问题 我尝试在谷歌上搜索 R 和 精确 或 圆形 但我得到的只是有关如何舍入的文章 先感谢您 所
  • 聚合函数在数据框中创建不需要的向量

    我在函数中创建数据帧时遇到了一个奇怪的问题 但是 在 data frame 之外使用相同的方法效果很好 这是基本函数 我用它来计算数据集的平均值 标准差和标准误差 aggregateX lt function formula dataset
  • 如何绘制 Voronoi 曲面细分的多边形而不是线段?

    我找到了一种使用 ggplot2 绘制 Voronoi 曲面细分的分段的方法 library deldir library ggplot2 library ggthemes set seed 123 df lt data frame lat
  • 根据列中的部分字符串匹配选择数据框行

    我想根据列中字符串的部分匹配从数据框中选择行 例如列 x 包含字符串 hsa 使用sqldf if它有一个like语法 我会做类似的事情 select from lt gt where x like hsa 很遗憾 sqldf不支持该语法

随机推荐