r - 使用 plotly 创建色带

标签 r ggplot2 plotly scatter-plot r-plotly

我需要在散点图上绘制两个加速度与 mpg 的斜率, 轻型汽车一个坡度,重型汽车一个坡度。我创建了这个:

cars_light <- cars_log[cars_log$log.weight. <= log(mean(cars$weight)), ]
cars_heavy <- cars_log[cars_log$log.weight. > log(mean(cars$weight)),]
cars_log$wt_cat <- ifelse(cars_log$log.weight. > log(mean(cars$weight)), 'heavy', 'light')

到目前为止,我已经通过这样做创建了散点图:

    plot_ly(
  data = cars_log,
  type = "scatter",
  x = ~log.acceleration.,
  y = ~ log.mpg.,
  color = ~ factor(wt_cat),
  colors = c("#8bc34a", "#ff5722"),
  marker = list(size = 10, opacity = 0.6)
) %>%
  layout(title = "Heavy cars VS light cars")

这给了我这个结果:

enter image description here

现在,我想为重型汽车创建一个斜坡,为轻型汽车创建另一个斜坡,我知道我需要使用 add_ribbons trace of plotly 但我不知道如何生成它。我在用 plotly 计算 lm 时遇到问题。 我可以用 ggplot 做同样的事情,但我不知道如何用 plotly 来做。

    ggplot(cars_log, aes_string('log.acceleration.', 'log.mpg.')) +
  geom_point(aes(color = factor(wt_cat))) +
  geom_smooth(method = 'lm', aes(color = factor(wt_cat)))

这是我的数据样本:

    > cars_log[1:5,]
  log.mpg. log.cylinders. log.displacement. log.horsepower. log.weight. log.acceleration. model_year origin
1 2.890372       2.079442          5.726848        4.867534    8.161660          2.484907         70      1
2 2.708050       2.079442          5.857933        5.105945    8.214194          2.442347         70      1
3 2.890372       2.079442          5.762051        5.010635    8.142063          2.397895         70      1
4 2.772589       2.079442          5.717028        5.010635    8.141190          2.484907         70      1
5 2.833213       2.079442          5.710427        4.941642    8.145840          2.351375         70      1

最佳答案

您可以使用 add_ribbons() 函数。它的主要三个参数是:

  • data 数据
  • x x 值
  • ymin 色带下界
  • ymax 色带上界

由于缺少最小数据集,我采用了这个:plotly regression line R这是在一个封闭的问题中使用的(即在 R Plotly 中绘制回归线)。使用此数据集,这是一个在 Plotly 外部完成回归的示例,其输出使用 broom::augment() 格式化,然后用于创建功能区:

library(plotly)
library(broom)
data(airquality)
airq <- airquality %>% 
      filter(!is.na(Ozone))
fit <- lm(Ozone ~ Wind, data = airq)
airq %>% 
  plot_ly(x = ~Wind, name = 'Scatter') %>% 
  add_markers(y = ~Ozone) %>% 
  add_ribbons(data = augment(fit,se_fit = TRUE),
              ymin = ~.fitted - 1.96 * .se.fit,
              ymax = ~.fitted + 1.96 * .se.fit,
              line = list(color = 'rgba(7, 164, 181, 0.05)'),
              fillcolor = 'rgba(7, 164, 181, 0.2)',
              name = '95% ribbon')

给出了以下 plotly : enter image description here

关于r - 使用 plotly 创建色带,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50307948/

相关文章:

r - 如何使用 tidyverse 根据日期/时间时间戳列删除重复记录?右

r - 如何使用 facet_wrap 将 y 轴设置为等于 x 轴 geom_line

r - ggplot2 ggsave : keep embedded fonts in exported . png

python - 如何以 png 格式保存 Plotly 离线图?

r - r 中阿姆斯壮数的程序

r - ggplot2:仅显示一组中的文本标签

r - 在 ggplot 中对数据进行分组之前添加 geom_smooth()

node.js - JupyterLab plotly 扩展错误 : Cannot find module jupyter\lab\staging\node_modules\ejs\postinstall. js

r - 使用动态 UI 绘制 Shiny 的 3D 绘图

r - 有效地组合几个 dcast data.table(共享相同的 key )