简体   繁体   中英

How to create line chart with Margins of Error in R

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!

在此处输入图片说明

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)

在此处输入图片说明

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

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