遍历列时校正密度图

已解决,只需要纠正二进制宽度

我有一个数据库,其中包含将要遍历的200个变量,并绘制了2个图:1个箱形图和一个密度图。密度图应在1个变量的两组之间区分颜色 但是,如果您查看所附的密度图,似乎是将两个组的计数一起使用来进行计算,而不是分别使用它们。至少密度曲线远低于指示问题的条。

请在下面的代码中找到示例

rm(list=ls())
library(ggplot2)
library(purrr)
library(ggbeeswarm)
library(cowplot)
library(plotly)

group <- c("Control","PAD","Control","PAD")
b <- round(runif(15,1,7)) 
c <- round(runif(15,3)) 
d <- round(runif(15,3,8)) 
e <- round(runif(15,5))
event <- c("no event","event","no event","event")

df <- data.frame(group,b,c,d,e,event)
df

rm(group,event)

将颜色定义为箱线图使用的组

df$color <- "color"
for (i in 1:dim(df)[1]){
  if (df$group[i]=="Control") {
    df$color[i] <- "Control" # in de column PAD,if the control is control give the color the string "control"
  }
}
for (i in 1:dim(df)[1]){
  if (df$group[i] == "PAD" && df$event[i] == "event") {
    df$color[i] <- "PAD with event" # in de column PAD,if the PAD has event give the color the string "event"
  }
}
for (i in 1:dim(df)[1]){
  if (df$group[i] == "PAD" && df$event[i] == "no event") {
    df$color[i] <- "PAD without event"
  }
}
rm(i)

用于循环浏览列

expl = names(df[1]) 
response = names(df[2:5]) 

response = set_names(response)
response

expl = set_names(expl)
expl

子集PAD和用于图2的控件

PADonly <- subset(df,group == "PAD")
Controlonly <- subset( df,group =="Control")
for (i in names(df)[2:5]){
  # used for adding the normal curve in plot 2 (PAD vs Control)
  xpad <- PADonly[,i]
  xpadmean <- mean(xpad,na.rm=TRUE)  
  xpadSD <- sd(xpad,na.rm = TRUE)

  xctrl <- Controlonly[,i]
  xctrlmean <- mean(xctrl,na.rm=TRUE)  
  xctrlSD <- sd(xctrl,na.rm = TRUE)



  p1 <- ggplot(df,aes_string("group",i)) + 
    geom_boxplot(show.legend = F) + 
    geom_beeswarm(aes(color = color),size=2) +
    scale_color_manual(values= c("Control"="#D85622","PAD with event" = "red","PAD without event"="#2D416D"))

#这是直方图,其中计数似乎有偏差

p2 <- ggplot(df,aes_string(x= i,fill="group")) + 
    geom_bar(binwidth = 0.1,aes(y=(..count..)/sum(..count..)),position = "identity",alpha=0.2,color="black") +
    scale_color_manual(values= c("Control"="#D85622","PAD" = "red")) + 
    stat_function(fun=dnorm,args = c(mean=xpadmean,sd=xpadSD),color="#D85622",size=1) +
    stat_function(fun=dnorm,args = c(mean=xctrlmean,sd=xctrlSD),size=1) +
    stat_density(aes_string(color= "group",i),na.rm=TRUE,bw = "SJ",geom = "line",color="#5785DF",size=1,linetype=2) +    geom_rug(aes_string(color= "group",x=i),col="#D85622")
    geom_vline(xintercept=xpadmean,linetype=2,color="red")+
  geom_vline(xintercept=xctrlmean,color="green")

  p_all <- plot_grid(p1,p2)
  p_all

}

遍历列时校正密度图

bieuosu 回答:遍历列时校正密度图

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3104832.html

大家都在问