자막이있는 ggplotly 개체 만들기
Oct 30 2020
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()상관 계수와 회귀 함수를 나타내는 데 사용 합니다. 텍스트 출력이 상단 중앙에 표시되도록 x및 y좌표를 수동으로 정의합니다 . dt.allData축 스케일링이 다른 데이터 테이블 중 일부가 있으므로 이전에 수동으로 정의 x하고 y조정 하지 않고 축 스케일링에 따라 텍스트가 항상 상단의 가운데에 표시되도록 플롯에서 정의하고 싶습니다 .
답변
4 ismirsehregal Oct 30 2020 at 16:56
및 사용 하는 것이 좋습니다 .ggtitlehjust = 0.5
편집 : 사용 plotly::layout하고 span제목을 만들 수있는 태그 :
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
편집 : 이 답변 에 따라 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
BrianLang Oct 30 2020 at 16:42
먼저 다음과 같은 것이 도움이 될 수 있는지 확인하는 것으로 시작합니다.
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쌍, 당신은 결국 당신은 당신이 전달하는 기능 프로그램으로 이동할 것 것 x열 x1, x2, x3및 y열을 y1, y2, y3A와 map각 쌍에서 관련 정보를 꺼내서 기능 그리고 그들을 플롯합니다.