【问题标题】:Issues with ggplotly within Shiny contextShiny 上下文中的 ggplotly 问题
【发布时间】:2026-01-26 15:30:01
【问题描述】:

使用下面的代码,我可以创建图像中显示的图。我正在尝试解决以下一些问题:

1- 如何在标题和情节之间放置图例。我希望图例采用水平格式而不是当前的垂直格式。

2- 如何将情节标题加粗并居中?

3- 如何在 geom_point 函数中使用不同的形状,取决于“残疾”变量?

4- 如何重命名标签,使其显示为“死亡年龄”而不是当前名称:“AgeatDeath”?

谢谢,

纳德

 IDD_line <-
  ggplot(IDD_plot) +
  geom_line(aes(x = Year, y = AgeatDeath, color = Disability), size = 1) +
  geom_point(aes(x = Year,
                 y = AgeatDeath,
                 color = Disability),
             size = 1.5,
             shape = 21) +
  scale_x_continuous(breaks = seq(2008, 2017, 1)) +
  labs(
    x = "Year",
    y = "Age at Death",
    caption = (""),
    face = "bold"
  ) +
  theme_bw() +
  scale_y_continuous(breaks = seq(35, 80, 5)) +
  ggtitle(paste("Age of Death by Year (2008 to 2017)")) +
  theme(
    legend.title = element_blank(),
    axis.text = (blue.bold.10.text),
    legend.position = "top"
  )
output$IDD_plot <- renderPlotly(ggplotly(IDD_line) %>%
                                  config(displayModeBar = F))

【问题讨论】:

    标签: r shiny ggplotly geom-text


    【解决方案1】:

    这比我最初预期的要难一些。也许其他人会想出一些更好的解决方案,但这就是我所拥有的。

    1. 如何在标题和情节之间放置图例。我希望图例采用水平格式而不是当前的垂直格式。

    您可以在致电ggplotly 后在layout 中执行此操作。要放入水平形式,请使用orientation = "h"。如果您指定y = 1.2,这将出现在绘图上方(如果您只是将yanchor 设置在“顶部”,这将出现在靠近顶部的绘图本身上)。

    此外,您还需要添加额外的边距,这样图例就不会与标题重叠。使用margin 设置左、右、下、上边距(lrbt)。

    layout(legend = list(x = 0, y = 1.2, orientation = "h"),
           margin = list(l = 50, r = 50, b = 100, t = 100)...
    
    1. 如何将情节标题加粗并居中?

    要加粗标题,您可以在ggtitle 中添加HTML 标签,例如&lt;b&gt;title&lt;/b&gt;。要使标题居中,请将其添加到 layout,如 #1 中一样,使用 xanchor

    layout(legend = list(x = 0, y = 1.2, orientation = "h"),
           margin = list(l = 50, r = 50, b = 100, t = 100),
           title = list(xanchor = "center", x = .5))
    
    1. 如何在 geom_point 函数中使用不同的形状,取决于“Disability”变量?

    要使形状依赖于“残疾”,您的审美中需要shape = Disability。棘手的部分是,如果你在geom_point 中的aes 中执行此操作,则在转换为ggplotly 后会得到重复的图例。您可以在 geom_point 之外使用单独的 aes 来解决此问题。

    欲了解更多信息,请参阅this github issue comment

    1. 如何重命名标签,使其显示为“死亡年龄”而不是当前名称:“AgeatDeath”?

    一种方法是在您的aes 中使用text,并以这种方式澄清您的标签。这将包括用&lt;br&gt; 换行,%d 用于整数值,%s 用于字符串(用于残疾):

    text = sprintf("Year: %d<br>Age at Death: %d<br>Disability: %s", 
                   Year, AgeatDeath, Disability)
    

    然后,在您的ggplotly 调用中,包括tooltip = "text"

    所以把这一切放在一起,你有:

    IDD_line <- ggplot(IDD_plot) +
      aes(color = Disability, 
          shape = Disability, 
          group = Disability, 
          text = sprintf("Year: %d<br>Age at Death: %d<br>Disability: %s", 
                         Year, AgeatDeath, Disability)) +
      geom_line(aes(x = Year, y = AgeatDeath), size = 1) +
      geom_point(aes(x = Year,
                     y = AgeatDeath),
                 size = 1.5) +
      scale_x_continuous(breaks = seq(2008, 2017, 1)) +
      labs(
        x = "Year",
        y = "Age at Death",
        caption = (""),
        face = "bold"
      ) +
      theme_bw() +
      scale_y_continuous(breaks = seq(35, 80, 5)) +
      ggtitle(paste("<b>Age of Death by Year (2008 to 2017)</b>")) +
      theme(
        legend.title = element_blank(),
        axis.text = (blue.bold.10.text) #,
        #legend.position = "top"
      )
    

    还有,

    output$IDD_plot <- renderPlotly(
        ggplotly(IDD_line, tooltip = "text") %>%
          config(displayModeBar = F) %>%
          layout(legend = list(x = 0, y = 1.2, orientation = "h"),
                 margin = list(l = 50, r = 50, b = 100, t = 100),
                 title = list(xanchor = "center", x = .5))
        )
    

    【讨论】:

    • 谢谢,你的回答真的很有帮助。但是,我收到“无效格式 '%d'; 对数字对象使用格式 %f、%e、%g 或 %a”的错误。
    • 代替 %d 试试 %f 例如