How to create line chart with Margins of Error in R

孤人 提交于 2019-12-11 03:46:19

问题


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



来源:https://stackoverflow.com/questions/12083387/how-to-create-line-chart-with-margins-of-error-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!