繁体   English   中英

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

[英]ggplot2 how to plot rows to multiple x-axis datapoints

我正在尝试根据左侧的数据创建这种类型的图表(为简单起见,使用任意值):

在此处输入图片说明

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

问题在于,应该分别代表值1-10(蓝色曲线),并且应在1-10值(绿线和红线)上分别绘制A和B的值。

如果我手动保存数据并手动将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 

恐怕我不知道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")

在此处输入图片说明

如果使用子集定义ggplotdata元素,则可以使用geom_hline提出一个解决方案:

theme_set(theme_bw())
ggplot(data = df[1:10,])+
    geom_errorbar(aes(x = X, ymin = mean - se, ymax = mean + se))+
    geom_point(aes(x = X, y = mean))+
    geom_line(aes(x = X, y = mean), group = 1)+
    geom_hline(data = df[11,], aes(yintercept = mean, colour = 'A'))+
    geom_hline(data = df[12,], aes(yintercept = mean, colour = 'B'))

在此处输入图片说明

将数据重新定向为长格式很有帮助,这样您就可以真正利用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

在此处输入图片说明

编辑:如果要用线代替点,只需将geom_point替换为geom_point geom_line

暂无
暂无

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

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