在 ggplotly 图形上添加文本标签

Adding text label on a ggplotly figure

我使用 ggplotplotly 在 R 中编译了以下代码。我的图表一切都很好,但我想在右上角添加一个文本标签,它将显示我指定模型的 R 平方。当我尝试使用 stat_poly_eq 函数时,它适用于我的标准 ggplot,但它在 ggplotly

中消失了

有没有办法让我在 ggplotly 中自动添加 R 平方或手动添加标签 "R^2 = 0.6"?

这是我的代码

library(rmsfuns)
packages <- c('tidyquant','PerformanceAnalytics','PortfolioAnalytics','tidyr',
              'dplyr','lubridate','tbl2xts', 'ggplot2','plotly','ggthemes','readxl', 'ISLR','ggrepel', 'ggpmisc','mgcv')

load_pkg(packages)
data <- read_excel(path = "C:/Users/user/Documents/latest website/website/SA_dataset.xlsx", col_names = T)

data <- data %>% mutate(Date = as.Date(Date))

my.formula <-  y~splines::bs(x, 3)

p1 <- ggplot(data, aes(x=Oil,y = ZAR)) +
  geom_point(aes(col = VIX),size = 2, alpha = 2) +
  scale_color_continuous(low = '#fb9d9d', high = '#4f042e') +
  geom_smooth(aes(group=1),method ='lm',formula = my.formula,se=FALSE,,color='#56242e', linetype = 'dashed', size = 1.5) +
  labs(title = 'USD-ZAR Exchange Rate vs Crude Oil Prices', subtitle = 'Higher oil prices tend to coincide with a stronger Rand',
       x = 'Spot Brent Crude, USD per barrel', y= 'USD-ZAR', caption = 'Data downloaded from Yahoo Finance') +
  theme_light() +
  stat_poly_eq(formula = my.formula, 
               aes(label = stat(adj.rr.label)), 
               parse = TRUE, label.x = 'middle', label.y ='middle') 


p2 <- ggplotly(p1)


p3 <- ggplotly(p2) %>% 
  layout(title = list(text = paste0('USD-ZAR Exchange Rate vs Crude Oil Prices',
                                    '<br>',
                                    '<sup>',
                                    'Higher oil prices (and lower global uncertainty) tend to coincide with a stronger Rand',
                                    '</sup>')))

我在此处添加了我的数据集的可复制文件

structure(list(Date = structure(c(16437, 16444, 16451, 16458, 
16465, 16472, 16479, 16486, 16493, 16500, 16507, 16514, 16521, 
16528, 16535, 16542, 16549, 16556, 16563, 16570, 16577, 16584, 
16591, 16598, 16605, 16612, 16619, 16626, 16633, 16640, 16647, 
16654, 16661, 16668, 16675, 16682, 16689, 16696, 16703, 16710, 
16717, 16724, 16731, 16738, 16745, 16752, 16759, 16766, 16773, 
16780, 16787, 16794, 16801, 16808, 16815, 16822, 16829, 16836, 
16843, 16850, 16857, 16864, 16871, 16878, 16885, 16892, 16899, 
16906, 16913, 16920, 16927, 16934, 16941, 16948, 16955, 16962, 
16969, 16976, 16983, 16990, 16997, 17004, 17011, 17018, 17025, 
17032, 17039, 17046, 17053, 17060, 17067, 17074, 17081, 17088, 
17095, 17102, 17109, 17116, 17123, 17130, 17137, 17144, 17151, 
17158, 17165, 17172, 17179, 17186, 17193, 17200, 17207, 17214, 
17221, 17228, 17235, 17242, 17249, 17256, 17263, 17270, 17277, 
17284, 17291, 17298, 17305, 17312, 17319, 17326, 17333, 17340, 
17347, 17354, 17361, 17368, 17375, 17382, 17389, 17396, 17403, 
17410, 17417, 17424, 17431, 17438, 17445, 17452, 17459, 17466, 
17473, 17480, 17487, 17494, 17501, 17508, 17515, 17522, 17529, 
17536, 17543, 17550, 17557, 17564, 17571, 17578, 17585, 17592, 
17599, 17606, 17613, 17620, 17627, 17634, 17641, 17648, 17655, 
17662, 17669, 17676, 17683, 17690, 17697, 17704, 17711, 17718, 
17725, 17732, 17739, 17746, 17753, 17760, 17767, 17774, 17781, 
17788, 17795, 17802, 17809, 17816, 17823, 17830, 17837, 17844, 
17851, 17858, 17865, 17872, 17879, 17886, 17893, 17900, 17907, 
17914, 17921, 17928, 17935, 17942, 17949, 17956, 17963, 17970, 
17977, 17984, 17991, 17998, 18005, 18012, 18019, 18026, 18033, 
18040, 18047, 18054, 18061, 18068, 18075, 18082, 18089, 18096, 
18103, 18110, 18117, 18124, 18131, 18138, 18145, 18152, 18159, 
18166, 18173, 18180, 18187, 18194, 18201, 18208, 18215, 18222, 
18229, 18236, 18243, 18250, 18257, 18264, 18271, 18278, 18285, 
18292, 18299, 18306, 18313, 18320, 18327, 18334, 18341, 18348, 
18355, 18362, 18369, 18376, 18383, 18390, 18397, 18404, 18411, 
18418), class = "Date"), ZAR = c(11.496, 11.549, 11.4148, 11.6468, 
11.513, 11.6695, 11.6215, 11.668, 12.0395, 12.479, 12.015, 12.045, 
11.798, 11.9953, 12.067, 12.1255, 12.0605, 11.926, 11.794, 11.895, 
12.1555, 12.5805, 12.3809, 12.1625, 12.2025, 12.3205, 12.4505, 
12.36, 12.6233, 12.6745, 12.6255, 12.8261, 12.97, 13.3044, 13.8545, 
13.5795, 13.3255, 13.9135, 13.732, 13.3522, 13.0814, 13.636, 
13.8247, 14.1585, 14.4019, 13.9525, 14.4061, 14.3393, 15.88, 
15.0983, 15.2865, 15.471, 16.3173, 16.8098, 16.4675, 15.891, 
16.0275, 15.8825, 15.398, 16.1688, 15.355, 15.2627, 15.2575, 
15.4548, 14.692, 14.98, 14.5697, 14.4, 14.2345, 14.8729, 15.405, 
15.6323, 15.7162, 15.093, 15.23, 15.1215, 15.065, 14.5603, 14.5333, 
14.58, 14.285, 13.88, 13.725, 13.4737, 13.5075, 14.375, 14.485, 
14.4196, 14.1831, 13.7139, 13.725, 13.8692, 14.3193, 13.9834, 
13.8265, 13.575, 14.384, 14.4755, 14.1153, 13.8061, 13.8296, 
14.08, 14.0074, 13.7386, 13.735, 13.505, 13.595, 13.47, 13.2788, 
13.3588, 13.065, 12.96, 13.015, 13.1628, 12.7238, 12.4396, 13.4247, 
13.76, 13.419, 13.1313, 13.3693, 13.4215, 13.356, 13.234, 12.8699, 
12.8115, 12.951, 12.814, 12.9318, 13.0551, 13.395, 13.0332, 12.9158, 
13.0096, 13.455, 13.4418, 13.1614, 13.038, 12.9417, 12.9376, 
13.1658, 13.2438, 13.5561, 13.775, 13.2663, 13.64, 14.0641, 14.2239, 
14.3704, 13.981, 14.1587, 13.7068, 13.655, 13.1464, 12.5987, 
12.3724, 12.3044, 12.3612, 12.191, 11.8672, 12.0916, 12.0352, 
11.6363, 11.545, 11.8966, 11.8172, 11.9745, 11.7428, 11.8385, 
12.025, 12.069, 12.0989, 12.3282, 12.5, 12.254, 12.7625, 12.491, 
12.6906, 13.0655, 13.4344, 13.4383, 13.7275, 13.4702, 13.2684, 
13.4051, 13.1793, 13.3256, 14.104, 14.6413, 14.2087, 14.6875, 
15.2372, 14.9345, 14.3136, 14.1473, 14.7747, 14.5194, 14.4188, 
14.6021, 14.3048, 14.3259, 14.0227, 13.8588, 13.8692, 14.1615, 
14.4016, 14.6372, 14.4298, 13.9623, 13.8351, 13.843, 13.6153, 
13.3233, 13.6257, 14.0828, 13.9978, 14.229, 14.4393, 14.4146, 
14.5, 14.4975, 14.096, 13.9568, 14.055, 14.3807, 14.3511, 14.1598, 
14.4188, 14.4, 14.585, 14.9535, 14.8393, 14.325, 14.0808, 14.1775, 
13.9857, 13.9409, 14.2963, 14.7885, 15.2623, 15.3056, 15.2519, 
15.185, 14.8083, 14.5778, 14.9268, 15.1563, 15.045, 14.8055, 
14.7905, 14.6329, 15.0318, 14.8605, 14.7137, 14.7175, 14.6535, 
14.627, 14.5226, 14.2751, 14.0326, 14.3164, 14.3679, 14.4569, 
14.3936, 15.0106, 15.0649, 14.9117, 15.0065, 15.6695, 15.6723, 
16.2771, 17.6077, 17.63, 19.049, 17.9689, 18.7408, 19.0263, 18.8453, 
18.3495, 18.5674, 17.6373, 17.548, 16.8765, 16.5863), Interest = c(6, 
5.9, 5.8, 5.75, 6.03, 6.04, 5.95, 5.91, 5.91, 5.91, 5.91, 5.91, 
6.15, 6, 6.15, 6.13, 6.16, 6.16, 6.25, 6.08, 6.25, 6.5, 6.6, 
6.6, 6.45, 6.44, 6.6, 6.38, 6.44, 6.49, 6.48, 6.44, 6.44, 6.49, 
6.5, 6.49, 6.49, 6.3, 6.3, 6.55, 6.55, 6.55, 6.65, 6.65, 6.65, 
6.65, 6.65, 6.35, 6.35, 6.35, 6.35, 6.35, 7.3, 7.3, 6.9, 7.3, 
7.3, 7.3, 7.3, 7.2, 7.3, 7.2, 7.7, 7.2, 7.1, 7.35, 7.15, 7.15, 
7.35, 7.35, 7.35, 7.35, 7.15, 7.15, 7.45, 7.9, 8, 7.95, 7.45, 
7.55, 8, 8, 7.35, 6.8, 7.3, 6.8, 6.95, 6.9, 7.8, 7.6, 7.3, 7.7, 
7.4, 8.05, 7.5, 7.9, 7.75, 7.45, 7.8, 7.5, 7.65, 7.5, 7.15, 8, 
7.31, 7.25, 7.25, 7.75, 6.85, 6.98, 6.6, 7.4, 6.8, 7.05, 6.39, 
6.1, 6.4, 7.15, 6.65, 7.2, 6.75, 6.5, 6.2, 6.5, 6.4, 6.9, 6.3, 
7.25, 6.7, 6.72, 6.8, 6.8, 6.75, 7.15, 6.5, 6.3, 5.95, 6.45, 
6.45, 6.35, 6.25, 5.95, 5.5, 6.2, 6.6, 6.35, 7.3, 6.4, 6.15, 
6.5, 6.25, 6.9, 6, 6.95, 7.15, 7.05, 5.6, 6.55, 6.55, 5.5, 6.5, 
6.5, 6.5, 6.6, 6.6, 6.55, 6.35, 6.6, 7.06, 6.95, 6.35, 6.35, 
6.35, 6.3, 6.3, 6.42, 6.95, 6.3, 6.38, 6.83, 6.7, 6.9, 6.9, 6.4, 
6.35, 6.29, 6.85, 6.2, 6.9, 7.07, 7.08, 7.08, 6.6, 7.1, 7.1, 
7.15, 7.15, 7.06, 7.06, 7.06, 6.78, 6.78, 6.99, 6.02, 7.35, 7.17, 
7.3, 6.74, 7.1, 7.2, 6.4, 6.92, 6.84, 6.87, 7.01, 7, 6.95, 6.95, 
6.4, 6.33, 6.15, 6.86, 6.77, 6.78, 7.1, 6.88, 6.82, 6.82, 6.81, 
6.83, 6.83, 6.82, 6.76, 6.74, 6.22, 5.58, 4.9, 6.73, 6, 6.68, 
6.09, 6.59, 6.75, 6.51, 6.4, 6.5, 6.62, 6, 6, 6, 6, 6, 6, 6, 
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5.5, 5.4, 
4.35, 4.3, 3.93, 3.5, 3.65, 2.29, 3.05, 3.63, 3.6, 3.55, 3.8), 
    Oil = c(56.03, 49.47, 46.58, 46.44, 46.76, 54.62, 56.57, 
    60.57, 60.63, 60.12, 56.51, 52.72, 55.01, 54.79, 56.51, 59.12, 
    61.41, 63.49, 65.01, 64.9, 64.31, 61.57, 61.9, 63.24, 60.65, 
    60.84, 60.36, 56.63, 57.17, 55.75, 54.14, 48.59, 47.94, 46, 
    43.53, 48.87, 47.6, 47.34, 47.33, 46.99, 51.33, 48.99, 46.81, 
    47.15, 47.23, 44.75, 41.54, 43.65, 42.41, 38.76, 36.84, 35.9, 
    36.55, 34.19, 29.1, 27.76, 31.75, 32.18, 30.41, 32.29, 33.12, 
    36.28, 39.3, 38.5, 39.41, 37, 37.51, 42.35, 43.04, 44.46, 
    44.2, 45.2, 48.34, 48.73, 48.91, 49.94, 47.39, 48.12, 47.09, 
    45.96, 45.54, 45.22, 42.55, 40.88, 43.63, 48.6, 48.65, 47.31, 
    47.51, 46.21, 46.24, 46.74, 49.52, 49.94, 50.33, 48.95, 44.63, 
    43.09, 44.01, 46.49, 48.78, 52.26, 53.26, 53.81, 54.96, 55.13, 
    54.02, 54.19, 54.91, 55.53, 54.48, 54.46, 55.4, 54.4, 52.78, 
    50.29, 50.1, 51.33, 53.28, 54.83, 52.08, 49.55, 48.7, 48, 
    51.75, 52.58, 50.23, 47.28, 46.18, 44.63, 46.16, 48.17, 47.1, 
    47.99, 49.95, 52.04, 51.96, 50.58, 52.03, 52.25, 53.88, 55.54, 
    57.03, 58.75, 56.08, 56.21, 57.78, 58.58, 61.04, 64.29, 61.52, 
    62.68, 63.73, 63.08, 64.35, 64.36, 66.52, 67.81, 69.47, 69.39, 
    70.04, 68, 65.5, 62.72, 65.52, 66.02, 65.12, 64.14, 67.4, 
    68.65, 67.03, 71.24, 73.39, 74.98, 74.42, 76.68, 78.75, 78.59, 
    75.35, 73.98, 74.26, 74.12, 75.24, 76.33, 75.29, 71.27, 74, 
    73.37, 71.42, 69.82, 72.77, 76.08, 76.64, 78.11, 78.97, 81.85, 
    85.44, 83.05, 80.41, 78.23, 74.09, 70.34, 66.21, 61.22, 58.65, 
    60.47, 59.51, 54.64, 51.03, 54.31, 58.64, 59.83, 61.34, 61.38, 
    61.71, 63.36, 66.58, 64.56, 64.73, 65.71, 67.34, 67.25, 69.54, 
    71.33, 70.87, 72.93, 71.59, 71.25, 73.32, 70.89, 69.29, 63.15, 
    63.19, 64.04, 66.51, 63.84, 65.98, 63.63, 62.8, 62.59, 57.33, 
    58.25, 59.57, 59.83, 60.23, 62.74, 65.56, 63.15, 59.22, 59.39, 
    59.32, 60.75, 60.23, 62.39, 62.56, 63.76, 64.74, 64.71, 66.5, 
    68.9, 68.73, 68.07, 67.93, 64.11, 62.2, 58.57, 54.59, 55.33, 
    58.61, 54.09, 50.7, 33.72, 25.65, 23.96, 18.72, 22.53, 20, 
    14.24, 17.05, 23.57, 28.18, 33.94, 33.7, 39.66), VIX = c(17.55, 
    20.95, 16.66, 20.97, 17.29, 14.69, 14.3, 13.34, 15.2, 16, 
    13.02, 15.07, 14.67, 12.58, 13.89, 12.29, 12.7, 12.86, 12.38, 
    12.13, 13.84, 14.21, 13.78, 13.96, 14.02, 16.79, 16.83, 11.95, 
    13.74, 12.12, 13.39, 12.83, 28.03, 26.05, 27.8, 23.2, 22.28, 
    23.62, 20.94, 17.08, 15.05, 14.46, 15.07, 14.33, 20.08, 15.47, 
    15.12, 14.81, 24.39, 20.7, 15.74, 18.21, 27.01, 27.02, 22.34, 
    20.2, 23.38, 25.4, 20.53, 19.81, 16.86, 16.5, 14.02, 14.74, 
    13.1, 15.36, 13.62, 13.22, 15.7, 14.72, 15.04, 15.2, 13.12, 
    13.47, 17.03, 19.41, 25.76, 14.77, 13.2, 12.67, 12.02, 11.87, 
    11.39, 11.55, 11.34, 13.65, 11.98, 17.5, 15.37, 12.29, 13.29, 
    13.48, 16.12, 13.34, 16.19, 22.51, 14.17, 12.85, 12.34, 14.12, 
    11.75, 12.2, 11.44, 14.04, 11.32, 11.23, 11.54, 10.58, 10.97, 
    10.85, 11.49, 11.47, 10.96, 11.66, 11.28, 12.96, 12.37, 12.87, 
    15.96, 14.63, 10.82, 10.57, 10.4, 12.04, 9.81, 9.75, 10.7, 
    10.38, 10.02, 11.18, 11.19, 9.51, 9.36, 10.29, 10.03, 15.51, 
    14.26, 11.28, 10.13, 12.12, 10.17, 9.59, 9.51, 9.65, 9.61, 
    9.97, 9.8, 9.14, 11.29, 11.43, 9.67, 11.43, 9.58, 9.42, 9.9, 
    11.04, 9.22, 10.16, 11.27, 11.08, 17.31, 29.06, 19.46, 16.49, 
    19.59, 14.64, 15.8, 24.87, 19.97, 21.49, 17.41, 16.88, 15.41, 
    14.77, 12.65, 13.42, 13.22, 13.46, 12.18, 11.98, 13.77, 16.09, 
    13.37, 12.18, 12.86, 13.03, 11.64, 13.16, 12.64, 11.99, 12.86, 
    14.88, 12.07, 11.68, 12.12, 14.82, 21.31, 19.89, 24.16, 19.51, 
    17.36, 18.14, 21.52, 18.07, 23.23, 21.63, 30.11, 28.34, 21.38, 
    18.19, 17.8, 17.42, 16.14, 15.72, 14.91, 13.51, 13.57, 16.05, 
    12.88, 16.48, 13.71, 12.82, 12.01, 12.09, 12.73, 12.87, 16.04, 
    15.96, 15.85, 18.71, 16.3, 15.28, 15.4, 15.08, 13.28, 12.39, 
    14.45, 12.16, 17.61, 17.97, 18.47, 19.87, 18.98, 15, 13.74, 
    15.32, 17.22, 17.04, 15.58, 14.25, 12.65, 12.3, 12.07, 12.05, 
    12.34, 12.62, 13.62, 12.63, 12.51, 13.43, 14.02, 12.56, 12.1, 
    14.56, 18.84, 15.47, 13.68, 17.08, 40.11, 41.94, 57.83, 66.04, 
    65.54, 46.8, 41.67, 38.15, 35.93, 37.19, 27.98, 31.89, 28.16, 
    27.51, 24.52, 28.27)), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -284L))

问题是 stat_poly_eq 尚未在 ggplotly 中实现。 ggplotly 应该给你一个警告。但是您可以像这样在绘图中添加文本注释,我使用 TeX 获取上标和下标:

p3 %>% 
  add_annotations(
    x = mean(p3$x$layout$xaxis$range),
    y = mean(p3$x$layout$yaxis$range),
    text = TeX("R^{2}_{adj} = 0.48"),
    showarrow = F
  ) %>% 
  config(mathjax = 'cdn')