Erstellen Sie ein ggplotly-Objekt mit einem Untertitel
Ich zeichne ein Streudiagramm ggplot()wie folgt:
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")
was hier die folgende Handlung gibt:
Ich verwende annotate(), um den Korrelationskoeffizienten und die Regressionsfunktion darzustellen. Ich definiere die xund yKoordinaten manuell, so dass die Textausgabe in der Mitte oben angezeigt wird. Da ich einige solcher Datentabellen dt.allDatamit unterschiedlichen Achsenskalierungen habe, möchte ich im Diagramm definieren, dass der Text immer in der Mitte oben angezeigt werden soll, abhängig von der Achsenskalierung, ohne vorher manuell zu definieren xund zu ykoordinieren.
Antworten
Ich würde vorschlagen , ggtitleund zu verwenden hjust = 0.5:
Bearbeiten: Verwenden von plotly::layoutund einem spanTag zum Erstellen des Titels:
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
Bearbeiten: Die supAlternative wurde gemäß dieser Antwort hinzugefügt
# 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
Zuerst würde ich sehen, ob so etwas Ihnen helfen könnte:
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)
und dann, wenn Sie eine Liste von x,yPaaren durchgehen möchten, möchten Sie schließlich zur funktionalen Programmierung übergehen, bei der Sie xSpalten x1, x2, x3und ySpalten y1, y2, y3an eine mapFunktion übergeben, die dann die relevanten Informationen aus jedem Paar herausholt und zeichnet sie.