ggplot2如何将行绘制到多个x轴数据点

时间:2016-10-17 20:33:14

标签: r ggplot2

我试图从左边的数据创建这种类型的图表(为简单起见,任意值):

enter image description here

目标是在x轴上绘制变量X,在Y轴上绘制平均值,误差线等于标准误差se。

问题是值1-10应各自单独表示(蓝色曲线),A和B的值应分别绘制在1-10个值(绿色和红色线)上。

如果我手动保存数据并手动将A和B的值复制到X的每个值,我可以绘制曲线,但这不是非常节省时间。有更优雅的方式吗?

提前致谢!

编辑:正如建议的代码:

    df <- structure(list(X = structure(c(1L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 
          10L, 2L, 11L, 12L), .Label = c("1", "10", "2", "3", "4", "5", 
          "6", "7", "8", "9", "A", "B"), class = "factor"), mean = c(1, 
2, 3, 4, 5, 6, 7, 8, 9, 10, 5.5, 6.5), sd = c(1L, 1L, 1L, 1L, 
      1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), se = c(1L, 1L, 1L, 1L, 1L, 1L, 
          1L, 1L, 1L, 1L, 1L, 1L)), .Names = c("X", "mean", "sd", "se"), class = "data.frame", row.names = c(NA,-12L))                                                                           
df<-as.data.frame(df)
df$X<-factor(df$X)
plot <- ggplot(df, aes(x=df$X, y=df$mean)) + geom_point() + geom_errorbar(aes(ymin=mean-se, ymax=mean+se), width=.1)
plot 

3 个答案:

答案 0 :(得分:1)

我害怕我不知道ggplot,但希望这是你想要的(它也可以帮助其他人理解你的问题)。

你想要一个有三行的ggplot,  1. df $ X,df $ mean  2. df $ X,df $ row_A_mean  3. df $ X,df $ row_B_mean  4. SE列的误差条

df <- structure(list(X = structure(c(1L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 
                                     10L, 2L, 11L, 12L), .Label = c("1", "10", "2", "3", "4", "5", 
                                                                    "6", "7", "8", "9", "A", "B"), class = "factor"), mean = c(1, 
                                                                                                                               2, 3, 4, 5, 6, 7, 8, 9, 10, 5.5, 6.5), sd = c(1L, 1L, 1L, 1L, 
                                                                                                                                                                             1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), se = c(1L, 1L, 1L, 1L, 1L, 1L, 
                                                                                                                                                                                                                     1L, 1L, 1L, 1L, 1L, 1L)), .Names = c("X", "mean", "sd", "se"), class = "data.frame", row.names = c(NA,-12L))                                                                           
df<-as.data.frame(df)
df$X<-factor(df$X)
plot <- ggplot(df, aes(x=df$X, y=df$mean)) + geom_point() + geom_errorbar(aes(ymin=mean-se, ymax=mean+se), width=.1)
plot 
 
#row A mean
df$row_A_mean<-rep(df[11,]$mean,nrow(df))# note that this could also be replaces by a horizontal line, unless the mean changes
#row A sd
df$row_A_sd<-rep(df[11,]$sd,nrow(df))

plot(as.numeric(df$X),df$mean,type="p",col="red")
lines(as.numeric(df$X),df$row_A_mean,col="green")

enter image description here

答案 1 :(得分:1)

如果我们使用子集来定义data的{​​{1}}元素,我们可以使用ggplot提出一个解决方案:

geom_hline

enter image description here

答案 2 :(得分:1)

将数据重新调整为长格式非常有用,这样您才能真正利用ggplot的aesthetic部分。一般情况下,我会使用reshape2::melt,但您的数据目前的格式并不适合它。我会通过长篇大论向您展示我的意思,您可以了解我们正在拍摄的内容:

#setting variables for your classes so it's a bit more scalable - reset as applicable
x.seriesLength <- 10
x.class.name <- "X" #name of the main series class; X in your example
a.vec <- c(5.5, 1, 1, "A")
b.vec <- c(6.5, 1, 1, "B")

#trimming df so we can reshape
df <- df[1:x.seriesLength, 2:4]
df$class <- x.class.name #adding class column

#converting your static A and B values to long form, sending to a data.frame and adding to df
add <- matrix(c(rep(a.vec, times = x.seriesLength),
                rep(b.vec, times = x.seriesLength)),
              byrow = T,
              ncol = 4)
colnames(add) <- c("mean", "sd", "se", "class")
df <- rbind(df, add)
print(df)

然后我们需要做更多的清洁工作:

df$rownum <- rep(1:x.seriesLength, times = 3)
df[,1:3] <- sapply(df[,1:3], as.numeric) #casting as numeric 
df$barmin <- df$mean - df$sd
df$barmax <- df$mean + df$sd

现在我们有一个包含所需数据的长格式数据框。然后,我们可以使用新的class列来绘制和着色多个系列。

#use class column to tell ggplot which points belong to which series
g <- ggplot(data = df) +
        geom_point(aes(x = rownum, y = mean, color = class)) +
        geom_errorbar(aes(x = rownum, ymin=barmin, ymax=barmax, color = class), width=.1)
g

enter image description here

修改:如果您想要线条而不是点数,只需将geom_point替换为geom_line