我有一個散點圖,我想在點之間添加一條平滑的連接線。我可以使用 ggplot 和一個 plotly 包裝器來做到這一點,沒有問題:
library(tidyverse)
library(plotly)
dat <- data.frame(x = c(0.89910345, 0.994439176, 0.881675547, 0.993289873, 0.990991268, 0.980292298, 0.984415918, 0.993125417, 0.990463749, 0.994603633, 0.965500006, 0.990827284, 0.993618787, 0.992961434, 0.938100735, 0.957212413, 0.981905481, 0.993289873, 0.906759406, 0.991483218),
y = c(0.819935601, 0.803471076, 0.820239219, 0.807804144, 0.812154115, 0.815320137, 0.814483142, 0.808271728, 0.812404613, 0.802747176, 0.816710715, 0.812240861, 0.806708415, 0.808695239, 0.818457407, 0.817328889, 0.815076674, 0.807785879, 0.819725289, 0.811638314))
ggplotly(dat %>%
ggplot(aes(x = x, y = y))
geom_point()
geom_line())

但是,我想使用本機 plotly 語法來做到這一點。然而,當我嘗試這樣做時,我得到以下資訊。誰能告訴我我的代碼有什么問題?
dat %>%
plot_ly(x = ~x,
y = ~y) %>%
add_trace(type = "scatter",
mode = "markers") %>%
add_trace(mode = "line")

uj5u.com熱心網友回復:
你可以嘗試使用geom_smooth而不是geom_line
ggplotly(dat %>%
ggplot(aes(x = x, y = y))
geom_point()
geom_smooth(method = "loess", se=FALSE, color="black"))

或者如果您確實想使用geom_line. 您可以使用spline
spline_int <- as.data.frame(spline(dat$x, dat$y))
ggplotly(dat %>%
ggplot(aes(x = x, y = y))
geom_point()
geom_line(data = spline_int, aes(x = x, y = y)))

如果你想單獨使用,plotly你可以在線條上添加一個形狀
dat %>%
plot_ly(x = ~x,
y = ~y) %>%
add_trace(type = "scatter",
mode = "markers") %>%
add_lines(line = list(shape = "linear"))

轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/361036.html
上一篇:使用twoord.plot使用兩個y軸在R中繪制線圖:第一個y軸的值缺失
下一篇:帶有正值和負值的條形圖
