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

时间:2019-01-17 23:57:44

标签: r ggplot2 reshape

样本数据:

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))

我的目标是使用ggplot()进行绘制,这是我拥有的起始数据。我以前用长数据文件作图,但不确定如何用宽文件。我想用其置信度限制以不同的颜色绘制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)

2 个答案:

答案 0 :(得分:2)

这是最有效的数据整形方法吗? @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"))

答案 1 :(得分:1)

Sample,这是一种最有效的方法,可以使用从宽到长格式的多个值列来重塑数据。

因此,这是一种通过使用patterns()函数在measure.vars中指定列名称来改进The OP has asked的方法:

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

请注意,setDT()强制数据帧通过引用对data.table 进行分类,即不复制对象。

为完整起见,这也是情节:

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))

OP's already working answer