我正在使用 Plotly 4.10.2、R 4.3.1、RStudio 2023.06.1 Build 524 制作包含两条迹线的散点图。一条迹线是点集,另一条迹线是最佳拟合线。
m1 <- lm(dt$d50 ~ dt$r501b3grange1)
m <- list(l = 60, r = 60, b = 60, t = 60, pad = 10)
fig <- plot_ly(dt, x = ~r501b3grange1, y = ~d50,
type = "scatter",
mode = "markers",
text = ~`Image`,
name = paste0("n = ", nrow(dt))) %>%
layout(xaxis = list(title = "Point layer median thickness (inches, 1 ft ball, 3 ft grid)"),
yaxis = list(title = "SAM minor axis d50 (g11 subset (All 57), inches)"),
font = list(size = 25, color = "black"),
margin = m) %>%
add_trace(dt, x = ~r501b3grange1, y = predict(m1), name = paste0("y = ",
round(summary(m1)$coefficients[[1]],2), " + ",
round(summary(m1)$coefficients[[2]],2), "x", "\n",
"R^2 = ", round(as.numeric(summary(m1)[8]),2)),
type = "scatter",
mode = "lines")
fig
我想让标记更大。但是当我尝试向第一条轨迹添加标记大小时:
m1 <- lm(dt$d50 ~ dt$r501b3grange1)
m <- list(l = 60, r = 60, b = 60, t = 60, pad = 10)
fig <- plot_ly(dt, x = ~r501b3grange1, y = ~d50,
type = "scatter",
mode = "markers",
marker = list(size = 10),
text = ~`Image`,
name = paste0("n = ", nrow(dt))) %>%
layout(xaxis = list(title = "Point layer median thickness (inches, 1 ft ball, 3 ft grid)"),
yaxis = list(title = "SAM minor axis d50 (g11 subset (All 57), inches)"),
font = list(size = 25, color = "black"),
margin = m) %>%
add_trace(dt, x = ~r501b3grange1, y = predict(m1), name = paste0("y = ",
round(summary(m1)$coefficients[[1]],2), " + ",
round(summary(m1)$coefficients[[2]],2), "x", "\n",
"R^2 = ", round(as.numeric(summary(m1)[8]),2)),
type = "scatter",
mode = "lines")
fig
即使我有
mode = "lines"
,它也会向我的最佳拟合线添加标记。我还尝试使用两个 add_trace
语句,而不是将第一个跟踪放在主 plot_ly
调用中,并得到了相同的结果。这是一个错误吗?
您应该使用
add_marker()
添加您的积分。由于您没有提供数据,我使用随机生成的数据来演示:
## packages
library(plotly)
## data
set.seed(123)
df1 <- data.frame(x = (1:20) + rnorm(20, 1, 0.1),
y = sort(rnorm(20, 10, 10)),
h = letters[1:20])
## fitted line
m1 <- lm(y ~ x, data = df1)
## figure
plot_ly(df1, x = ~x,
mode = "markers",
text = ~h,
name = paste0("n = ", nrow(df1))) %>%
add_markers(y = ~y, marker=list(size=10)) %>%
add_trace(y = predict(m1),
name = paste0("y = ", round(summary(m1)$coefficients[[1]],2), " + ",
round(summary(m1)$coefficients[[2]],2), "x", "\n",
"R^2 = ", round(as.numeric(summary(m1)[8]),2)),
type = "scatter",
mode = "lines") %>%
layout(xaxis = list(title = "Point layer median thickness (inches, 1 ft ball, 3 ft grid)"),
yaxis = list(title = "SAM minor axis d50 (g11 subset (All 57), inches)"),
font = list(size = 25, color = "black"),
margin = list(l = 60, r = 60, b = 60, t = 60, pad = 10))