简体   繁体   English

如何以置信区间绘制宽数据文件

[英]How to plot a wide data file with confidence intervals

Sample data: 样本数据:

DF <- data.frame("TIME"=c(1:10), 
"COEF1"=c(0.15,0.93,0.80,0.59,0.89,0.18,0.95,0.90,0.75,0.61), 
"COEF1LOWER"=c(0.05,0.83,0.70,0.49,0.79,0.08,0.85,0.80,0.65,0.51),
"COEF1UPPER"=c(0.25,1.03,0.90,0.69,0.99,0.28,1.05,1.00,0.85,0.71),
"COEF2"=c(0.21,0.29,0.95,0.80,0.34,0.10,0.40,0.47,0.54,0.67), 
"COEF2LOWER"=c(0.11,0.19,0.85,0.70,0.24,0.00,0.30,0.37,0.44,0.57),
"COEF2UPPER"=c(0.31,0.39,1.05,0.90,0.44,0.20,0.50,0.57,0.64,0.77),
"COEF3"=c(0.44,0.44,0.40,0.62,0.24,0.05,0.75,0.03,0.42,0.21), 
"COEF3LOWER"=c(0.34,0.34,0.30,0.52,0.14,-0.05,0.65,-0.07,0.32,0.11),
"COEF3UPPER"=c(0.54,0.54,0.50,0.72,0.34,0.15,0.85,0.13,0.52,0.31))

My goal is to make a plot using ggplot() and this is the starting data I have. 我的目标是使用ggplot()作图,这是我拥有的起始数据。 I have made plots before with a long data file but am not sure how to do this with the wide file. 我以前用长数据文件作图,但不确定如何用宽文件。 I want to plot COEF1 , COEF2 , COEF3 in different colors with their confidence limits. 我想用它们的置信度极限以不同的颜色绘制COEF1COEF2COEF3

p <- ggplot(DF, aes(x=time, y=COEF, ymin=LOWER, ymax=UPPER)) +
  geom_ribbon(aes(fill=COEF_N), alpha=0.3) +
  geom_line(aes(color=COEF_N))
print(p)

Is this the most efficient approach for data re shape? 这是最有效的数据整形方法吗? @Uwe @Z.lin @Uwe @ Z.lin

DF1 <- data.table(DF)
col1 <- c("COEF1", "COEF2", "COEF3") 
col2 <- c("COEF1LOWER", "COEF2LOWER", "COEF3LOWER")
col3 <- c("COEF1UPPER", "COEF2UPPER", "COEF3UPPER")
DFNEW <- data.table::melt(DF1, measure = list(col1,col2,col3), 
                          value.name = c("EST_EST","COEF_LOWER","COEF_UPPER"))

The OP has asked for the most efficient approach to reshape the data with multiple value columns from wide to long format. OP要求使用最有效的方法来重塑具有从宽到长格式的多个值列的数据。

So, here is a way to improve OP's already working answer by using the patterns() function to specify the column names in measure.vars : 因此,这是一种通过使用patterns()函数在measure.vars指定列名称来改善OP已经有效的答案的方法:

library(data.table)
long <- melt(setDT(DF), measure.vars = patterns("COEF\\d$", "LOWER$", "UPPER$"),
     value.name = c("COEF_EST", "COEF_LOWER", "COEF_UPPER"))

Note that setDT() coerces a data frame to class data.table by reference , ie, without copying the object. 请注意, setDT() 通过引用将数据帧强制为data.table类,即不复制对象。

For the sake of completeness, here is also the plot: 为了完整起见,这也是情节:

library(ggplot2)
ggplot(long, aes(x = TIME, y = COEF_EST, ymin = COEF_LOWER, ymax = COEF_UPPER)) +
  geom_ribbon(aes(fill = variable), alpha = 0.3) +
  geom_line(aes(color = variable))

在此处输入图片说明

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

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