简体   繁体   中英

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

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

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.

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 :

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.

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

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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