R ggplot with two series: points and errorbars with legends

前端 未结 2 627
鱼传尺愫
鱼传尺愫 2021-01-17 02:02

If I have a dataframe like this:

obs<-rnorm(20)
d<-data.frame(year=2000:2019,obs=obs,pred=obs+rnorm(20,.1))
d$pup<-d$pred+.5
d$plow<-d$pred-.5
d$         


        
2条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-17 02:42

    Here is a ggplot solution that does not require melting and grouping.

    set.seed(1)      # for reproducible example
    obs <- rnorm(20)
    d   <- data.frame(year=2000:2019,obs,pred=obs+rnorm(20,.1))
    d$obs[20]<-NA
    library(ggplot2)
    ggplot(d,aes(x=year))+
      geom_point(aes(y=obs,color="obs",shape="obs"))+
      geom_point(aes(y=pred,color="pred",shape="pred"))+
      geom_errorbar(aes(ymin=pred-0.5,ymax=pred+0.5))+
      scale_color_manual("Legend",values=c(obs="red",pred="blue"))+
      scale_shape_manual("Legend",values=c(obs=19,pred=3))
    

    This creates a color and shape scale wiith two components each ("obs" and "pred"). Then uses scale_*_manual(...) to set the values for those scales ("red","blue") for color, and (19,3) for scale.

    Generally, if you have only two categories, like "obs" and "pred", then this is a reasonable way to go use ggplot, and avoids merging everything into one data frame. If you have more than two categories, or if they are integral to the dataset (e.g., actual categorical variables), then you are much better off doing this as in the other answer.

    Note that your example left out the column year so your code does not run.

提交回复
热议问题