How to create line chart with Margins of Error in

2019-07-12 03:49发布

问题:

I am looking for a way to plot data on population proportion over time, with showing margins or error, similar to this example: http://goo.gl/dbrbu. But could not find any instructions on that. Thanks!

回答1:

A ggplot2 solution:

I'm going to use the US population dataset in R:

population <- data.frame(Year=seq(1790, 1970, length.out=length(uspop)), 
                         Population=uspop, 
                         Error=rnorm(length(uspop), 5))

library(ggplot2)
ggplot(population, aes(x=Year, y=Population, 
                       ymin=Population-Error, ymax=Population+Error))+
  geom_line(colour="red", size=1.2)+
  geom_point(pch=2)+
  geom_errorbar(width=0.9)



回答2:

The plotrix package has plotCI:

 require(plotrix)
 y<-runif(10)
 err<-runif(10)
 plotCI(1:10,y,err,2*err,lwd=2,col="red",scol="blue", 
                  main="Add colors to the points and error bars")
 lines(1:10, y)

(A very minor tweak to the example code is to add lines connecting the midpoints.)