下面是我的代码的一个小示例,它显示了这个问题.

我希望生成的点大小是从offset中的值派生出来的. 为了得到合理的值,我会在计算之前进行计算. 这些值的范围约为2-20.

但是,打印中的点大小完全不正确.

此外,图例中的点大小不一致.

library(plotly)

offset_plot_colors <- c('#845EC2', '#D65DB1', '#FF6F91', '#FF9671', '#FFC75F', '#F9F871')

df <- data.frame(
  insert_position = c(1, 2, 3, 4, 5, 6, 
                      1, 2, 3, 4, 5, 6, 
                      1, 2, 3, 4, 5, 6, 
                      1, 2, 3, 4, 5, 6),
  experiment = c(100, 100, 100, 100, 100, 100, 
                 101, 101, 101, 101, 101, 101, 
                 102, 102, 102, 102, 102, 102,
                 103, 103, 103, 103, 103, 103),
  sensor_position = c("s1", "s2", "s3", "s4", "s5", "s6", 
                      "s1", "s2", "s3", "s4", "s5", "s6", 
                      "s6", "s1", "s2", "s3", "s4", "s5", 
                      "s5", "s6", "s1", "s2", "s3", "s4"),
  offset = c( -0.10187856, -0.11539658, -0.01525474,  0.06469351, -0.13144734,  0.10990038
            , -0.11939423, -0.12415050, -0.01897634,  0.06963700, -0.12348225,  0.11077662
            ,  0.09999526, -0.11111048, -0.13619725, -0.03287019,  0.05424941, -0.12881697
            , -0.12830725,  0.09734471, -0.11105168, -0.12477904, -0.02409957,  0.06817639)
)

# calc size of dot
df$size <- abs(df$offset) * 150

offset_plot1 <- plot_ly(df, 
  x = ~experiment, y = ~insert_position, 
  color = ~sensor_position, 
  colors = offset_plot_colors,
  marker = list(size = ~size, opacity=1)
  ) %>%
  add_markers(
    text = ~paste("Sensor:", sub(".*_", "", sensor_position), "<br>Offset:", round(df$offset, digits = 2), "<br>size:", size), 
    hoverinfo = "text"
  )

offset_plot1 <- offset_plot1 %>% layout(
  title = "Offset vs Insert Position vs Experiment",
  xaxis = list(title = "Experiment"),
  yaxis = list(title = "Insert Position"),
  legend = list(title = "Offset")
)

offset_plot1

generated plot

推荐答案

为了解决尺寸问题,我对您的绘图 routine 做了一些细微的调整.

Explanation of Changes

  1. 使用plotly::plot_ly而不是add_markers调用添加了所有数据.
  2. offset_plot_colors向量添加了关键点名称,以确保标记正确着色.此参数必须传递到plot_ly函数中,而不是add_markers函数中,否则它将无法工作.
  3. 更新了layout,现在x轴刻度步长为1(dtick = 1),并且我确保包括了您的图例标题(title = "Offset"->;title = list(text = "Offset")).

Code

offset_plot_colors <- c(
  "s1" = '#845EC2',
  "s2" = '#D65DB1',
  "s3" = '#FF6F91',
  "s4" = '#FF9671',
  "s5" = '#FFC75F',
  "s6" = '#F9F871'
)

offset_plot1 <-
  plotly::plot_ly(
    df,
    x = ~ experiment,
    y = ~ insert_position,
    color = ~ sensor_position,
    colors = offset_plot_colors,
    size = ~ size,
    hoverinfo = "text",
    text = ~ paste(
      "Sensor:",
      sub(".*_", "", sensor_position),
      "<br>Offset:",
      round(df$offset, digits = 2),
      "<br>size:",
      size
    ),
    type = "scatter",
    mode = "markers"
  ) %>%
  plotly::layout(
    title = "Offset vs Insert Position vs Experiment",
    xaxis = list(title = "Experiment", dtick = 1),
    yaxis = list(title = "Insert Position"),
    legend = list(title = list(text = "Offset"))
  )

offset_plot1

Image

enter image description here

R相关问答推荐

如何创建具有总计列和ggplot 2所有条线的百分比标签的堆叠条形图?

如何从其他前面列中减go 特定列的平均值?

derrr summarise每个组返回多行?

如何使用按钮切换轨迹?

使用case_match()和char数组重新编码值

将数据集中的值增加到当前包含的最大值

par函数中的缩写,比如mgp,mar,mai是如何被破译的?

未识别时区

如何在所有绘图中保持条件值的 colored颜色 相同?

在不丢失空值的情况下取消列出嵌套列表

合并后返回列表的数据帧列表

从圆到R中的多边形的标绘雷达图

如何识别倒排的行并在R中删除它们?

如何在科学记数法中显示因子

使用RSelenium在R中抓取Reddit时捕获多个标签

优化从每个面的栅格中提取值

R代码,用于在线条图下显示观测表

如果满足条件,则替换列的前一个值和后续值

具有自定义仓位限制和计数的GGPLATE直方图

附加中缀操作符