plotly::sublot 不显示两个标题

2024-01-11

我正在尝试绘制两个plotly一起绘制在R using plotly::subplot。问题是子图没有显示titles两者的plots。类似问题的其他答案建议使用facet_wrap or plot_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)

您可以使用annotations:

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'))
  )

EDIT

作为替代方案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)

然后,代替renderPlotly and plotlyOutput,你必须使用renderCombineWidgets and combineWidgetsOutput- see 组合小部件-闪亮 https://rdrr.io/cran/manipulateWidget/man/combineWidgets-shiny.html.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

plotly::sublot 不显示两个标题 的相关文章

随机推荐

  • 如何仅从一列中选择不同的值

    我有记录如下 key name 1111 aa 1111 bb 2222 cc 我需要选择key and name当 的时候key价值是独特的 当我尝试时 select distinct key name from table 我得到了所有
  • 在c++中将hdf5文件读取到动态数组

    由于堆栈的大小限制 我正在尝试将大型 3D hdf5 文件读入动态数组 我尝试了几种不同的方法 但由于分段错误而失败 下面是显示我的问题的示例代码 我非常感谢一些帮助 This example was based on several ex
  • RestTemplateBuilder bean

    我的应用程序与不同的休息端点交互 每个端点都需要一个专门的 RestTemplate 对象 我正在使用 RestTemplateBuilder 创建每个 RestTemplate 对象 克隆 Spring Boot 提供的 RestTemp
  • AngularJS module.constant() :如何仅在模块内定义常量?

    在一个页面上 我有几个 Angular 模块 对于每个模块 我定义一个包含模块版本的常量 var module1 angular module module1 constant version 1 2 3 var module2 angul
  • 如何在 C++ 中维护指向父级的弱指针?

    是否有一种标准方法可以在 C 的子对象中维护指向父对象 使用共享指针创建 的弱指针 本质上 我需要实现以下内容 Class B Class A private B m b Class B public void SetParentPtr c
  • 正常关闭 IHostedService

    我正在尝试在 NET Core 中开发一个简单的 API 允许异步处理请求 请求发送至控制器 后台服务 IHostedService 上安排的工作 控制器返回 202 后台服务执行长时间运行的操作 由于应用程序将在 IIS 上运行 因此控制
  • 成员函数模板放在哪里

    C 中经常让我感到沮丧的一个方面是决定模板在头文件 传统上描述接口 和实现 cpp 文件之间的位置 模板通常需要进入标头 公开实现 有时还需要引入以前只需要包含在 cpp 文件中的额外标头 我最近再次遇到这个问题 下面显示了一个简化的示例
  • 如何在android中现有的html内容附加附加文本?

    我正在开发一个应用程序 在其中我将文本附加到存储在 html 文件中的现有文本 html 文件的位置位于我的应用程序的 assets 文件夹中 我知道如何使用 URL 加载 html 但我的问题是附加文本 以下是我的java代码 publi
  • Servlet 3.0 注销不起作用

    我对 Servlet 3 0 的身份验证功能有疑问 在 Servlet v3 中使用此代码 log info request getUserPrincipal log info request getAuthType log info re
  • Laravel 5.3WhereIn 返回唯一结果

    我在这个地方遇到问题 我的代码看起来像这样 arrayKeys 1 2 1 4 5 1 products App Product whereIn id arrayKeys gt select id name outright price d
  • 为 ifeq 出错:意外标记附近出现语法错误

    我正在编写一个在一个地方进行字符串匹配的 Makefile 代码如下 if test then shell scripts fi ifeq DIST TYPE nightly shell scripts endif 这里是第一个if第二个是
  • Access Services 和 SharePoint 2010 - 需要信息

    我被要求研究将 Access 解决方案直接发布到 SharePoint 的能力 如下面的演示所示 http channel9 msdn com shows Access Microsoft Access 2010 Demo http cha
  • 如何设置标题栏的背景?

    我已按照此问题的说明进行操作 如何更改操作栏上的文本 https stackoverflow com questions 3438276 change title bar text in android 我能够成功创建自己的标题栏 但是当我
  • facebook django 应用程序的 oauth 中的 redirect_uri 错误

    我在从 Facebook 获取我正在尝试编写的 Django 应用程序的访问令牌时遇到问题 我的视图设置如下 from django http import HttpResponse HttpResponseRedirect from dj
  • 在 ScalaTest 中对集合元素使用 HavePropertyMatcher?

    我已经使用 ScalaTest 的 FeatureSpec 有几天了 我试图了解是否可以使用内置匹配器定义以下规范 如果不能 我如何编写合适的自定义匹配器 假设我有课本 case class Book val title String va
  • GPC 多边形初始化

    我正在使用 GPC多边形裁剪库 http www cs man ac uk toby alan software并想以编程方式创建多边形 我只看到如何从文件创建一个的代码 如何在我的代码中进行初始化 从您的链接中更好地阅读 找到doc ht
  • 当大小太大时,vector.resize 函数会损坏内存

    发生的情况是我正在读取加密数据包 并且遇到一个损坏的数据包 该数据包返回一个非常大的长度随机数 size t nLengthRemaining packet nLength packet m pSource gt GetPosition p
  • Lex VHDL '(勾号)令牌

    在 VHDL 中 字符可用于封装字符标记ie 或者它可以作为属性分隔符 类似于 CPP 的 token ie string hello 解析包含字符的属性名称时出现问题ie string a b c 在这种情况下 天真的词法分析器将错误地标
  • 如何在 Perl 中读取文件并执行 grep 操作?

    open MR
  • plotly::sublot 不显示两个标题

    我正在尝试绘制两个plotly一起绘制在R using plotly subplot 问题是子图没有显示titles两者的plots 类似问题的其他答案建议使用facet wrap or plot ly 但我正在寻找一个适用于ggplotl