【问题标题】:Create a ggplotly object with a subtitle创建一个带有字幕的 ggplotly 对象
【发布时间】:2021-02-12 18:22:11
【问题描述】:

我正在使用ggplot() 绘制散点图,如下所示:

library(data.table)
library(plotly)
library(ggplot2)
library(lubridate)

dt.allData <- data.table(date = seq(as.Date('2020-01-01'), by = '1 day', length.out = 365),
                         DE = rnorm(365, 4, 1), Austria = rnorm(365, 10, 2), 
                         Czechia = rnorm(365, 1, 2), check.names = FALSE)

## Calculate Pearson correlation coefficient: ##
corrCoeff <- cor(dt.allData$Austria, dt.allData$DE,  method = "pearson", use = "complete.obs")
corrCoeff <- round(corrCoeff, digits = 2)

## Linear regression function extraction by creating linear model: ##
regLine <- lm(DE ~ Austria, data = dt.allData)

## Extract k and d values for the linear function f(x) = kx+d: ##
k <- round(regLine$coef[2], digits = 5)
d <- round(regLine$coef[1], digits = 2)
linRegFunction <- paste0("y = ", d, " + (", k, ")x")

## PLOT: ##
p1 <- ggplot(data = dt.allData, aes(x = Austria, y = DE, 
                                    text = paste("Date: ", date, '\n',
                                                 "Austria: ", Austria, "MWh/h", '\n',
                                                 "DE: ", DE, "\u20ac/MWh"),
                                    group = 1)
      ) +
      geom_point(aes(color = ifelse(date >= now()-weeks(5), "#419F44", "#F07D00"))) +
      scale_color_manual(values = c("#F07D00", "#419F44")) +
      geom_smooth(method = "lm", se = FALSE, color = "#007d3c") +
      annotate("text", x = 10, y = 10,
               label = paste("\u03c1 =", corrCoeff, '\n',
                             linRegFunction), parse = TRUE) +
      theme_classic() +
      theme(legend.position = "none") +
      theme(panel.background = element_blank()) +
      xlab("Austria") +
      ylab("DE")+
      ggtitle("DE vs Austria") +
      theme(plot.title = element_text(hjust = 0.5, face = "bold"))

# Correlation plot converting from ggplot to plotly: #
plot <- plotly::ggplotly(p1, tooltip = "text")

这里给出了以下情节:

我用annotate()来表示相关系数和回归函数。我手动定义了xy 坐标,以便文本输出显示在顶部的中间。由于我有一些此类数据表dt.allData 具有不同的轴缩放比例,因此我想在图中定义文本应始终显示在顶部的中间,这取决于轴缩放比例而不定义xy之前手动坐标。

【问题讨论】:

  • 您能否列出您正在使用的所有软件包的列表
  • 我现在已经添加了所有的库。
  • 对于那些感兴趣的人:我刚刚看到,here 是一个使用sup 而不是span 的相关问题。

标签: r ggplot2 r-plotly ggplotly


【解决方案1】:

我建议使用ggtitlehjust = 0.5

编辑:使用plotly::layoutspan标签来创建标题:

library(data.table)
library(ggplot2)
library(plotly)
library(lubridate)

dt.allData <- data.table(date = seq(as.Date('2020-01-01'), by = '1 day', length.out = 365),
                         DE = rnorm(365, 4, 1), Austria = rnorm(365, 10, 2), 
                         Czechia = rnorm(365, 1, 2), check.names = FALSE)

## Calculate Pearson correlation coefficient: ##
corrCoeff <- cor(dt.allData$Austria, dt.allData$DE,  method = "pearson", use = "complete.obs")
corrCoeff <- round(corrCoeff, digits = 2)

## Linear regression function extraction by creating linear model: ##
regLine <- lm(DE ~ Austria, data = dt.allData)

## Extract k and d values for the linear function f(x) = kx+d: ##
k <- round(regLine$coef[2], digits = 5)
d <- round(regLine$coef[1], digits = 2)
linRegFunction <- paste0("y = ", d, " + (", k, ")x")

## PLOT: ##
p1 <- ggplot(data = dt.allData, aes(x = Austria, y = DE, 
                                    text = paste("Date: ", date, '\n',
                                                 "Austria: ", Austria, "MWh/h", '\n',
                                                 "DE: ", DE, "\u20ac/MWh"),
                                    group = 1)
) +
  geom_point(aes(color = ifelse(date >= now()-weeks(5), "#419F44", "#F07D00"))) +
  scale_color_manual(values = c("#F07D00", "#419F44")) +
  geom_smooth(method = "lm", formula = 'y ~ x', se = FALSE, color = "#007d3c") +
  # ggtitle(label = paste("My pretty useful title", '\n', "\u03c1 =", corrCoeff, '\n', linRegFunction)) +
  theme_classic() +
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(legend.position = "none") +
  theme(panel.background = element_blank()) +
  xlab("Austria") +
  ylab("DE")

# Correlation plot converting from ggplot to plotly: #
# using span tag (directly in control of font-size):
span_plot <- plotly::ggplotly(p1, tooltip = "text") %>% layout(
    title = paste(
      '<b>My pretty useful title</b>',
      '<br><span style="font-size: 15px;">',
      '\u03c1 =<i>',
      corrCoeff,
      '</i><br>',
      linRegFunction,
      '</span>'
    ),
    margin = list(t = 100)
  )
span_plot

编辑:根据this answer添加了sup替代项

# using sup tag:
sup_plot <- plotly::ggplotly(p1, tooltip = "text") %>% layout(
    title = paste(
      '<b>My pretty useful title</b>',
      '<br><sup>',
      "\u03c1 =<i>",
      corrCoeff,
      '</i><br>',
      linRegFunction,
      '</sup>'
    ),
    margin = list(t = 100)
  )
sup_plot

Here你可以在plotly docs中找到一些相关信息。

【讨论】:

  • 对不起,我这里已经有标题了。我的问题中缺少这个!
  • 好的,感谢您的澄清。用另外两行修改每个标题不是一种选择?
  • 我会用字幕试试,我还没有想到。感谢您的想法。
  • 字幕不适用于ggplotly。您需要将这些行添加到标题中。
  • 我明白了,使用 subtitle = paste("\u03c1 =", corrCoeff, '\n', linRegFunction) 不起作用。当我的标题应该是 bold 其他两行不是时,我该怎么做?
【解决方案2】:

首先,我会先看看这样的事情是否可以帮助你:

annotate("text", 
         x = mean(dt.allData$Austria, na.rm = TRUE), 
         y = max(dt.allData$DE, na.rm = TRUE),
         label = paste("\u03c1 =", 
                       corrCoeff, '\n',
                       linRegFunction), 
         parse = TRUE,
         hjust = .5)

然后,如果您想要查看 x,y 对的列表,您最终会想要转向函数式编程,您将在其中传递 xx1, x2, x3 和 @987654325 @columns y1, y2, y3map 函数,然后从每对中提取相关信息并绘制它们。

【讨论】:

  • 谢谢,但如果我有一些极端异常值,那么它不在情节中间。
  • 那么让我们使用对异常值敏感的东西,比如均值!
  • 我已经尝试过使用mean()。然而,它从来没有完全居中,而且经常偏离很多。
  • 嗯。好吧,它适用于您在此处分享的案例!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-09-18
  • 2020-09-06
  • 1970-01-01
  • 2020-10-12
  • 1970-01-01
  • 2010-10-05
  • 2017-12-13
相关资源
最近更新 更多