简体   繁体   English

将拟合回归线添加到 R plotly 小提琴

[英]Add fitted regression line to R plotly violins

I have xy data for three group s across three integer age s:我有跨三个integer age的三个groupxy数据:

set.seed(1)
df <- data.frame(value = c(rnorm(500,8,1),rnorm(600,6,1.5),rnorm(400,4,0.5),rnorm(500,2,2),rnorm(400,4,1),rnorm(600,7,0.5),rnorm(500,3,1),rnorm(500,3,1),rnorm(500,3,1)),
                 age = c(rep(3,500),rep(8,600),rep(24,400),rep(3,500),rep(8,400),rep(24,600),rep(3,500),rep(8,500),rep(24,500)),
                 group = c(rep("A",1500),rep("B",1500),rep("C",1500)))

My purpose is to use R 's plotly to plot the value s as violin s, like this:我的目的是使用Rplotly到 plot 的value s 作为violin s,像这样:

library(plotly)
library(dplyr)
df$age <- factor(df$age)
plot_ly(x=df$group,y=df$value,type='violin',name=df$age,color=df$age,box=list(visible=T)) %>%
  layout(violinmode='group')

在此处输入图像描述

And add to each group the lm fitted line, and if possible also the standard error lines.并在group中添加lm拟合线,如果可能的话,还添加标准误差线。

So I first add to df the fitted lines per each group :所以我首先将group的拟合线添加到df中:

df$age <- as.integer(df$age)
df$fitted.value <- unlist(lapply(c("A","B","C"),function(g) lm(value ~ age,dplyr::filter(df,group == g)) %>% fitted.values()))

Then I tried to only do a single group 's violin adding it's fitted line using:然后我尝试只做一个group的 violin 添加它的拟合线使用:

df <- df %>% dplyr::filter(group == "A")
plot_ly(x=df$age,y=df$value,type='violin',color=df$age,box=list(visible=T)) %>%
  add_trace(x=df$age,y=df$fitted.value,mode="lines")

But this gives this long list of warnings :但这给出了一长串warnings

Warning messages:
1: 'violin' objects don't have these attributes: 'mode'
Valid attributes include:
'type', 'visible', 'showlegend', 'legendgroup', 'opacity', 'uid', 'ids', 'customdata', 'meta', 'selectedpoints', 'hoverinfo', 'hoverlabel', 'stream', 'transforms', 'uirevision', 'y', 'x', 'x0', 'y0', 'name', 'orientation', 'bandwidth', 'scalegroup', 'scalemode', 'spanmode', 'span', 'line', 'fillcolor', 'points', 'jitter', 'pointpos', 'width', 'marker', 'text', 'hovertext', 'hovertemplate', 'box', 'meanline', 'side', 'offsetgroup', 'alignmentgroup', 'selected', 'unselected', 'hoveron', 'xaxis', 'yaxis', 'idssrc', 'customdatasrc', 'metasrc', 'hoverinfosrc', 'ysrc', 'xsrc', 'textsrc', 'hovertextsrc', 'hovertemplatesrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'
 
2: 'violin' objects don't have these attributes: 'mode'
Valid attributes include:
'type', 'visible', 'showlegend', 'legendgroup', 'opacity', 'uid', 'ids', 'customdata', 'meta', 'selectedpoints', 'hoverinfo', 'hoverlabel', 'stream', 'transforms', 'uirevision', 'y', 'x', 'x0', 'y0', 'name', 'orientation', 'bandwidth', 'scalegroup', 'scalemode', 'spanmode', 'span', 'line', 'fillcolor', 'points', 'jitter', 'pointpos', 'width', 'marker', 'text', 'hovertext', 'hovertemplate', 'box', 'meanline', 'side', 'offsetgroup', 'alignmentgroup', 'selected', 'unselected', 'hoveron', 'xaxis', 'yaxis', 'idssrc', 'customdatasrc', 'metasrc', 'hoverinfosrc', 'ysrc', 'xsrc', 'textsrc', 'hovertextsrc', 'hovertemplatesrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'
 
3: 'violin' objects don't have these attributes: 'mode'
Valid attributes include:
'type', 'visible', 'showlegend', 'legendgroup', 'opacity', 'uid', 'ids', 'customdata', 'meta', 'selectedpoints', 'hoverinfo', 'hoverlabel', 'stream', 'transforms', 'uirevision', 'y', 'x', 'x0', 'y0', 'name', 'orientation', 'bandwidth', 'scalegroup', 'scalemode', 'spanmode', 'span', 'line', 'fillcolor', 'points', 'jitter', 'pointpos', 'width', 'marker', 'text', 'hovertext', 'hovertemplate', 'box', 'meanline', 'side', 'offsetgroup', 'alignmentgroup', 'selected', 'unselected', 'hoveron', 'xaxis', 'yaxis', 'idssrc', 'customdatasrc', 'metasrc', 'hoverinfosrc', 'ysrc', 'xsrc', 'textsrc', 'hovertextsrc', 'hovertemplatesrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'

And this undesired plot:而这个不受欢迎的 plot: 在此处输入图像描述

Any idea how to add the trend lines for all group s and optimally also their standard error lines?知道如何为所有group添加趋势线以及最佳的标准误差线吗?

Maybe you can use this solution.也许你可以使用这个解决方案。 It starts by making a new df with an additional variable describing the group and the subgroup .它首先创建一个新的 df ,其中包含一个描述 group 和subgroup的附加变量。

Another DF builds the mean values of group_by(group, moreA) .另一个 DF 构建group_by(group, moreA)的平均值。

The plot is made with ggplot and the geom_violin is filled with data from the first DF and geom_point from the second DF sum_res as well as the geom_smooth does. plot 是用ggplot制作的, geom_violin填充了来自第一个 DF 的数据和来自第二个 DF geom_pointsum_res以及geom_smooth This geom_smooth takes the mean values from sum_res to make the fit.这个geom_smooth采用sum_res的平均值来进行拟合。

After this the ggplot object is brought into ggplotly .在此之后, ggplot object 被带入ggplotly

Unfortunately the ggplotly behaviour like hovering does not come out in this reprex.不幸的是,像悬停这样的ggplotly行为并没有出现在这个reprex中。 But it is working in RStudio但它在 RStudio 中工作


library(plotly)
library(tidyverse)


ddf <- df %>% #glimpse()
  mutate(moreA = case_when(
    age == 3 ~ paste0(group,'3'),
    age == 8 ~ paste0(group,'8'),
    age == 24 ~ paste0(group,'24'),
  )) 

# this will bring the right order
ddf$moreA <- factor(ddf$moreA, levels = unique(ddf$moreA))

sum_res <- ddf %>% #
  group_by(group, moreA) %>% 
  summarise(meanA = mean(value))
    
p <- ggplot(ddf, aes(x = moreA, y = value, color = moreA)) +
  geom_violin() +
  geom_point(data = sum_res, mapping = aes(x = moreA, y = meanA), size = 1) +
  geom_smooth(data = sum_res, 
              mapping = aes(x = moreA, y = meanA, group = group, color = group),
              method='lm', size = 1, se =F) +
  theme_bw() +
  theme(legend.position = 'none')
#p

ggplotly(p)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM