我使用Plotly 4.10.2,R 4.3.1,RStudio2023.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

enter image description here

我想把记号笔做大一点.但当我try 将标记大小添加到第一个轨迹时:

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

enter image description here

它为我最合身的线条增加了标记,即使我有mode = "lines"条.我还try 使用两个add_trace语句,而不是将第一个跟踪放在主plot_ly调用中,得到了相同的结果.这是个窃听器吗?

以下是我的数据:

> dput(dt$d50)
c(2.191258065, 4.371259836, 4.163834921, 4.299901482, 0.721296517, 
0.806656288, 0.838300519, 2.123984458, 1.281270095, 1.271034705, 
1.126324259, 2.158535202, 0.811873646, 0.997379781, 0.791658847, 
0.865206465, 1.697433187, 1.306820106, 1.002612879, 0.932092299, 
1.741705726, 1.546688167, 1.408641227, 0.922844563, 0.845056924, 
0.997727831, 1.102327201, 1.120945941, 1.092093241, 1.182791188, 
1.303034055, 1.200149608, 1.051766949, 0.917999368, 0.879761687, 
0.25661637, 0.367374289, 0.451367428, 0.594794813, 0.412664784, 
0.326505198, 0.343620551, 0.441396171, 0.475386025, 0.400242695, 
0.374735446, 0.386719965, 0.312585107, 3.261716671, 3.292011578, 
0.767037605, 1.651888689, 1.193666161, 1.934104802, 2.286671289, 
1.43194983, 1.611017108)

> dput(dt$r501b3grange1)
c(1.09698951244354, 3.16094923019409, 2.79072117805481, 3.07730960845947, 
0.744107246398926, 1.22431325912476, 1.53918492794037, 1.52391850948334, 
0.751307666301727, 0.827914595603943, 1.34772574901581, 1.02658534049988, 
0.776823580265045, 0.762321770191193, 1.20416533946991, 0.986024975776672, 
1.5238938331604, 1.06575691699982, 1.04620349407196, 1.24733889102936, 
0.65419340133667, 0.705933749675751, 0.824617326259613, 0.979621589183807, 
1.15843141078949, 0.93364292383194, 0.818296492099762, 0.993945062160492, 
0.955138683319092, 0.853289723396301, 0.850569367408752, 0.850764274597168, 
0.663198113441467, 0.516988277435303, 0.539037942886353, 0.751503467559814, 
0.677738070487976, 0.792271614074707, 0.792271614074707, 0.627113521099091, 
0.584987282752991, 0.684955239295959, 0.77651035785675, 0.729957222938538, 
0.848376214504242, 0.74308979511261, 0.549346268177032, 0.6502605676651, 
2.18274283409119, 2.94228267669678, 0.680980920791626, 1.51205325126648, 
0.92967689037323, 1.24754667282104, 1.08400869369507, 0.809762358665466, 
0.843399584293365)

推荐答案

你应该用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))

R相关问答推荐

如何将Rmarkdown中包含图像和文本的行的两个单元格与.PDF输出垂直对齐?

Facet_wrap具有不同bin宽度值的图表

图片中令人惊讶的行为

高质量地将R格式的图表从Word中输出

使用预定值列表将模拟数量(n)替换为rnorm()

geom_Ribbon条件填充创建与数据不匹配的形状(ggplot 2 r)

单击 map 后,将坐标复制到剪贴板

如何使用`ggplot2::geom_segment()`或`ggspatial::geom_spatial_segment()`来处理不在格林威治中心的sf对象?

更改编号列表的 colored颜色

手动打印线型gplot

如何编辑ggplot的图例字使用自定义对象(gtable)?'

在R中为马赛克图中的每个字段着色

您是否可以折叠R中的重复行,同时保留基于所选列的值?

以字符格式导入的ExcelElectron 表格日期列标题

如何在PDF格式的kableExtra表格中显示管道字符?

在纵向数据集中创建新行

自动STAT_SUMMARY统计与手动标准误差之间的差异

如何在R中改变fviz_pca_biplot中圆的边界线的 colored颜色 ?

在R中,如何将误差条放置在堆叠的每个条上?

R-使用stri_trans_General()将其音译为德语字母