r - plotly::sublot 不显示两个标题

标签 r ggplot2 plotly r-plotly ggplotly

我正在尝试使用 plotly::subplotR 中一起绘制两个 plotly 图。问题是子图不显示两个图的标题。类似问题的其他答案建议使用 facet_wrapplot_ly,但我正在寻找与 ggplotly 配合使用的解决方案。

如何解决这个问题?

带有代码的示例数据:

library(tidyverse)
library(plotly)

# Sample Data
Group_1_2020 = data.frame(Code = c("A", "B", "C"),
                 Count_2020 = c(1,2,3))

Group_2_2020 = data.frame(Code = c("D", "E", "F"),
                          Count_2020 = c(4,5,6))

Group_1_2021 = data.frame(Code = c("A", "B", "C"),
                 Count_2021 = c(4, 8, 6))
Group_2_2021 = data.frame(Code = c("D", "E", "F"),
                          Count_2021 = c(8, 10, 12))

# Merge Datasets
DF_Merged_1 = 
  inner_join(Group_1_2020, Group_1_2021)

DFF_Merged_1 = DF_Merged_1 %>% dplyr::select(Code, Count_2020, Count_2021) %>% 
  gather(key = Type, value = Value, -Code) %>% 
  mutate(Type = ifelse(Type == "Count_2020", "2020", "2021"))

DF_Merged_2 = 
  inner_join(Group_2_2020, Group_2_2021)

DFF_Merged_2 = DF_Merged_2 %>% dplyr::select(Code, Count_2020, Count_2021) %>% 
  gather(key = Type, value = Value, -Code) %>% 
  mutate(Type = ifelse(Type == "Count_2020", "2020", "2021"))


# ggplot
ggplot_1 = DFF_Merged_1 %>% 
  ggplot(aes(x = reorder(Code,Value), y = Value, fill = Type, 
             text = paste("Count:", Value,
                          "<br>", "Offense Code:", Code,
                          "<br>", "Year:", Type))) +
  geom_col(position = "dodge", show.legend = FALSE) +
  xlab("Offense Code") +
  ylab("Count") +
  ggtitle("Arrest Counts for Group 1 in Year 2020 and  2021") +
  theme(axis.text=element_text(size=8)) 

ggplot_2 = DFF_Merged_2 %>% 
  ggplot(aes(x = reorder(Code,Value), y = Value, fill = Type, 
             text = paste("Count:", Value,
                          "<br>", "Offense Code:", Code,
                          "<br>", "Year:", Type))) +
  geom_col(position = "dodge", show.legend = FALSE) +
  xlab("Offense Code") +
  ylab("Count") +
  ggtitle("Arrest Counts for Group 2 in Year 2020 and  2021") +
  theme(axis.text=element_text(size=8)) 

# Interactive Plots
fig1 = ggplotly(ggplot_1, tooltip = "text")  
fig2 = ggplotly(ggplot_2, tooltip = "text")  
subplot(fig1, fig2)

enter image description here

最佳答案

您可以使用注释:

library(plotly)

gg1 <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) + geom_point()
gg2 <- ggplot(iris, aes(x = Petal.Length, y = Petal.Width)) + geom_point()

pp1 <- ggplotly(gg1)
pp2 <- ggplotly(gg2)

subplot(pp1, pp2, margin = 0.05) %>% 
  layout(annotations = list(
    list(x = 0.2 , y = 1.1, text = "Title 1", showarrow = FALSE, xref='paper', yref='paper'),
    list(x = 0.8 , y = 1.1, text = "Title 2", showarrow = FALSE, xref='paper', yref='paper'))
  )

enter image description here


编辑

作为 subplot 的替代方案,您可以使用 manipulateWidget::combineWidgets:

gg1 <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) + 
  geom_point() + ggtitle("Title1")
gg2 <- ggplot(iris, aes(x = Petal.Length, y = Petal.Width)) + 
  geom_point() + ggtitle("Title2")

pp1 <- ggplotly(gg1)
pp2 <- ggplotly(gg2)

manipulateWidget::combineWidgets(pp1, pp2, nrow = 1)

enter image description here

然后,您必须使用 renderCombineWidgetscombineWidgetsOutput,而不是 renderPlotlyplotlyOutput - 请参阅 combineWidgets-shiny

关于r - plotly::sublot 不显示两个标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68796762/

相关文章:

python - 从 slider 输入数据以更改标记颜色

r - 在weatherData查询中设置时区

r - 如何在 R 中将 30 秒数据扩展到 60 分钟数据

r - ggplot2:每个 bin 中因子的单级分数的条形图

r - ggplot geom_point随窗口大小变化大小

python - 将 3D Matplot 保存为交互式 HTML

r - 你如何(以及为什么)使用对比?

r - 通过 R 中的方向矩阵回溯

regex - 如何使用 R 解析 html 字符串?

python-3.x - Plotly 散点图趋势线出现在散点下方。如何让趋势线出现在散点图上? [Python]