在绘图图中添加水平显着条(和星)的R plot_ly命令是什么? "How to draw the boxplot with significant level?"和"Put stars on ggplot barplots and boxplots - to indicate the level of significance (p-value)"的答案为ggplot提供了良好的代码。
首先,确定意义:
library(lsmeans)
lsmeans(lm(data = iris, Sepal.Width ~ Species), pairwise ~ Species)
$contrasts
contrast estimate SE df t.ratio p.value
setosa - versicolor 0.658 0.06793755 147 9.685 <.0001
setosa - virginica 0.454 0.06793755 147 6.683 <.0001
versicolor - virginica -0.204 0.06793755 147 -3.003 0.0088
以下是绘图的示例R代码。如何添加线条和星星?
library(plotly)
p <- plot_ly()
p <- add_boxplot(p, data = iris, x = ~Species, y = ~Sepal.Width,
color = ~Species, boxpoints = "all", jitter = 0.3, pointpos = 0)
# p <- add_paths(p, data = iris, ???)
# p <- add_line(p, data = iris, ???)
p
答案 0 :(得分:0)
可怕的hacky解决方案,提供所需的输出
layout
使用注释的问题在于无法将星号放在正确的位置,三个箱图表示三个分类x值。新的x值通过条形图添加。
library(plotly)
p <- plot_ly()
p <- add_bars(p,
x = c('setosa', 'setosa0', 'versicolor', 'versicolor0', 'virginica'),
y = c(3.5, 4.6, 2.5, 4.1, 3),
opacity=1,
showlegend = F,
marker=list(line = list(color='rgba(0,0,0,0'),
color = 'rgba(0,0,0,0'),
text = c('', '**', '', '*', ''),
textposition = 'outside',
legendgroup = "1"
)
p <- add_lines(p,
x = c('setosa', 'setosa', 'versicolor', 'versicolor'),
y = c(4.5, 4.6, 4.6, 4.5),
showlegend = F,
line = list(color = 'black'),
legendgroup = "1",
hoverinfo = 'none'
)
p <- add_lines(p,
x = c('versicolor', 'versicolor', 'virginica', 'virginica'),
y = c(4.0, 4.1, 4.1, 4.0),
showlegend = F,
line = list(color = 'black'),
legendgroup = "1",
hoverinfo = 'none'
)
p <- add_boxplot(p, data = iris, x = ~Species, y = ~Sepal.Width,
color = ~Species, boxpoints = "all", jitter = 0.3, pointpos = 0,
legendgroup="1")
p <- layout(p,
xaxis = list(tickmode = 'array',
tickvals = c('setosa', 'sf', 'versicolor', 'vet', 'virginica'),
ticktext = c('setosa', '', 'versicolor', '', 'virginica')),
yaxis = list(range = c(0, 5))
)
p
下图显示了用于获取图表的所有隐藏痕迹: