R语言 以更清晰的方式在散点图上显示置信区间

djmepvbi  于 2023-05-20  发布在  其他
关注(0)|答案(1)|浏览(103)

我有一个数据集,看起来像这样:

> dput(dt)
structure(list(Odds.Ratio = c(0.34, 0.85, 0.38, 1.34, 0.98, 0.55, 
0.34, 0.25), Lower.Bound.CI = c(0.12, 0.34, 0.33, 0.8, 0.67, 
0.34, 0.22, 0.13), Upper.Bound.CI = c(0.66, 0.98, 0.67, 1.55, 
1.42, 0.77, 0.5, 0.43), Cluster = c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 
2L)), class = "data.frame", row.names = c(NA, -8L))

我想绘制每个聚类的比值比,并在图中理想地指示置信区间的宽度。以下是我一直在做的:

library(plotly)
dt$Variance <- (dt$Upper.Bound.CI - dt$Lower.Bound.CI)
dt$Cluster <- as.factor(dt$Cluster)
fig <- plot_ly(
  dt,
  x = ~ Cluster,
  y = ~ Odds.Ratio,
  type = 'scatter',
  mode = 'markers',
  color = ~ Cluster,
  size = ~ Variance
) 

fig %>%
  layout( xaxis = list(title = 'Cluster'), 
          yaxis = list(title = 'Odds Ratio'), 
          legend = list(title=list(text='<b> Cluster </b>')))

我的真实的数据集有更多的行,所以将点的大小与CI的方差/宽度对应起来有点困难。有更好的选择吗?谢谢!

pjngdqdw

pjngdqdw1#

我们可以尝试用不同的符号来显示聚类,并用颜色来显示方差:

library(plotly)

dt$Variance <- (dt$Upper.Bound.CI - dt$Lower.Bound.CI)
tv_cluster <- sort(unique(dt$Cluster)) ## DO NOT convert Cluster to factors

plot_ly(dt, x = ~Cluster, y = ~ Odds.Ratio) %>% 
  add_markers(symbol = ~ as.factor(Cluster),
              color = ~Variance, 
              size = 1) %>% 
  layout( xaxis = list(title = 'Cluster',
                       tickvals = as.list(tv_cluster),
                       ticktext = as.list(tv_cluster)), 
          yaxis = list(title = 'Odds Ratio'), 
          legend = list(title=list(text='<b> Cluster </b>')))

创建于2023-05-12带有reprex v2.0.2

数据:

dt <- structure(list(Odds.Ratio = c(0.34, 0.85, 0.38, 1.34, 0.98, 0.55, 0.34, 0.25), 
                     Lower.Bound.CI = c(0.12, 0.34, 0.33, 0.8, 0.67, 0.34, 0.22, 0.13), 
                     Upper.Bound.CI = c(0.66, 0.98, 0.67, 1.55, 1.42, 0.77, 0.5, 0.43), 
                     Cluster = c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L)), 
                class = "data.frame", row.names = c(NA, -8L))

相关问题