带有误差条的点图,两个系列,光抖动 [英] Dotplot with error bars, two series, light jitter

查看:86
本文介绍了带有误差条的点图,两个系列,光抖动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收集了一些研究的数据.对于每项研究,我都会对性别变量的平均值以及是否存在显着差异感兴趣.对于每项研究,我都有男性和女性的平均值和95%的置信区间.

I have a collection of data over several studies. For each study I am interested about the mean of a variable by gender, and if this significantly differs. For each study I have the mean and 95% confidence intervals for both males and females.

我想做的事情与此类似:

What I would like to do is something similar to this:

我使用了几种样式的点图(dotplot,dotplot2,Dotplot),但并没有完全实现.

I have used several flavours of dotplots (dotplot, dotplot2, Dotplot) but did not quite get there.

使用Hmisc中的Dotplot,我设法拥有一个系列及其错误栏,但是我对如何添加第二个系列感到困惑.

Using Dotplot from Hmisc I managed to have one series and its errorbars, but I am at a loss on how to adding the second series.

我使用Dotplot并按照这是我正在使用的代码的有效示例

Here is a working example of the code I am using

data<-data.frame(ID=c("Study1","Study2","Study3"),avgm=c(2,3,3.5),avgf=c(2.5,3.3,4))
data$lowerm <- data$avgm*0.9 
data$upperm <- data$avgm*1.1
data$lowerf <- data$avgf*0.9
data$upperf <- data$avgf*1.1

# Create the customized panel function
mypanel.Dotplot <- function(x, y, ...) {
  panel.Dotplot(x,y,...)
  tips <- attr(x, "other")
  panel.arrows(x0 = tips[,1], y0 = y, 
               x1 = tips[,2], y1 = y, 
               length = 0.05, unit = "native",
               angle = 90, code = 3)
}

library(Hmisc)
Dotplot(data$ID ~ Cbind(data$avgm,data$lowerm,data$upperm), col="blue", pch=20, panel = mypanel.Dotplot,
        xlab="measure",ylab="study")

这将绘制三列数据,分别是男性的平均值(avgm)和95%置信区间的下限和上限(lowerm和upperm).我还有其他三个系列,用于相同的研究,对女性受试者(avgf,lowerf,upperf)做相同的工作.

This plots three columns of data, the average for males (avgm), and the lower and upper bound of the 95% confidence interval (lowerm and upperm). I have other three series, for the same studies, that do the same job for the female subjects (avgf, lowerf, upperf).

结果如下:

简而言之,缺少了什么:

What is missing, in a nutshell:

  1. 为同一研究添加第二个系列(avgf),其均值和置信区间定义在其他三个变量上

  1. adding a second series (avgf) with means and confidence intervals defined on three other variables for the same studies

添加一些垂直抖动,以使它们不是一个在另一个之上,但是即使它们重叠,读者也可以看到两者.

adding some vertical jitter so that they are not one on top of the other but the reader can see both even when they overlap.

推荐答案

不幸的是,我对Dotplot帮不上忙,但是我发现使用ggplot相当简单.您只需要稍微重新排列数据即可.

Unfortunately I can't help you with Dotplot, but I find it fairly straightforward using ggplot. You just need to rearrange the data slightly.

library(ggplot2)
# grab data for males
df_m <- data[ , c(1, 2, 4, 5)]
df_m$sex <- "m"
names(df_m) <- c("ID", "avg", "lower", "upper", "sex")
df_m

# grab data for females
df_f <- data[ , c(1, 3, 6, 7)]
df_f$sex <- "f"
names(df_f) <- c("ID", "avg", "lower", "upper", "sex")
df_m

# bind the data together
df <- rbind(df_m, df_f)

# plot
ggplot(data = df, aes(x = ID, y = avg, ymin = lower, ymax = upper, colour = sex)) +
  geom_point(position = position_dodge(width = 0.2)) +
  geom_errorbar(position = position_dodge(width = 0.2), width = 0.1) +
  coord_flip() +
  scale_colour_manual(values = c("blue", "red")) +
  theme_classic()

  # if you want horizontal grid lines you may change the last line with:
  theme_bw() +
  theme(panel.grid.major.y = element_line(colour = "grey", linetype = "dashed"),
        panel.grid.major.x = element_blank(),
        panel.grid.minor.x = element_blank()) 

这篇关于带有误差条的点图,两个系列,光抖动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆