开发者

ggplot and errorbars

I'm trying to create a plot with errorbars from the following data (dput of the dataframe in the end). I'd like to create errorbars for the "est A" and "est B" variables for each "loc", but I cannot figure out the right way to melt/cast the data so that each "loc" would have two rows with several columns.

I.e. I'd like to convert the dataframe into

loc est value lb ub

a A 0.56 0.26 1.20

a B 0.26 0.11 0.60

b A 0.13

b B 0.03

c A

c B

ggplot(test,aes(x=Loc,y=value,color=est))+geom_point()+geom_errorbar(aes(ymax=ub,ymin=lb))


> dput(test)
structure(list(Loc = c("a", "b", "c", "d", "e", "f", "g", "h", 
"i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s"), `est A` = c(0.563270934055709, 
0.137109873453407, 0.0946514679398302, 0.185103062070327, 0.0322566231880829, 
0.122509922923046, 0.120243043198876, 0.496530499385046, 0.469200921538171, 
0.0605252215342125, 0.102364314964088, 0.115500654388976, 0.226320301860998, 
0.553299923910439, 0.0284461712437990, 0.161231809656951, 0.129270683014873, 
0.0691902563736868, 4.22775563209408e-10), `est B` = c(0.261222076585074, 
0.0361169058045732, 0.0389295009573361, 0.0341489915400374, 0.00722206105211422, 
0.0756192543690879, 0.0638910584951127, 0.112923798991901, 0.169215410079679, 
0.0235142527703486, 0.0347964005083873, 0.0485649792804626, 0.057534910753763, 
0.142573116854277, 0.0221769276223588, 0.104198059736499, 0.0234801538140553, 
0.072563099770018, 1.93211916678252e-10), `A lb` = c(0.262976608413878, 
0.0724138165552355, 0.0496821188642757, 0.083935851825787, 0.00630380846921399, 
0.0490562081838664, 0.0467393615329014, 0.20284436584412, 0.178474018619480, 
0.0171506568665928, 0.0503363150211533, 0.0600281091658352, 0.0747597162489745, 
0.276929613768523, 0.00665167561787145, 0.0580348444006193, 0.0516432163190537, 
0.0228220757228112, 0), `A ub` = c(1.20647287629726, 0.259606775235633, 
0.180324442434493, 0.408206300913301, 0.165057321233572, 0.305948661143090, 
0.309340756132258, 1.21542708762748, 1.23351010121895, 0.213595459944227, 
0.208168853315217, 0.222235904972905, 0.685140094216929, 1.10548237017075, 
0.121651250740107, 0.447932560408103, 0.323583825296458, 0.209765826526966, 
Inf), `B lb` = c(0.111866193520147, 0.0169169666997800, 0.0186761266424921, 
0.开发者_StackOverflow中文版0121471488870159, 0.00126380823047679, 0.0279532804037691, 
0.0225442254744942, 0.0369377660459994, 0.0509571176650792, 0.00592607390737984, 
0.0157043256006381, 0.0231925392578512, 0.0153998561264800, 0.0612241780775462, 
0.00507024995333768, 0.0364091610491575, 0.00745586930827693, 
0.0243798413908829, 0), `B ub` = c(0.609987442570209, 0.0771078472899858, 
0.0811466999446844, 0.0960022499146318, 0.0412706331409062, 0.204565315724654, 
0.181069310198491, 0.345223486522794, 0.561920617187038, 0.0933029341161755, 
0.0770991075408484, 0.101694652159027, 0.214954342966326, 0.332010886676128, 
0.0970003694676995, 0.298200654450461, 0.0739441103828987, 0.215973654783605, 
Inf)), .Names = c("Loc", "est A", "est B", "A lb", "A ub", "B lb", 
"B ub"), row.names = c(NA, -19L), class = "data.frame")


I called your dataframe Data, then:

test <- with(Data,
   data.frame(
     Loc = rep(Loc,2),
     est = rep(c("A","B"),each=nrow(Data)),
     value = c(get("est A"),get("est B")),
     lb = c(get("A lb"),get("B lb")),
     ub = c(get("A ub"),get("B ub"))
   )
)
ggplot(test,aes(x=Loc,y=value,color=est))+geom_point()+
     geom_errorbar(aes(ymax=ub,ymin=lb))

And please, never ever use variable names with a space if possible. If you didn't, you could just have done:

test <- with(Data,
   data.frame(
     Loc = rep(Loc,2),
     est = rep(c("A","B"),each=nrow(Data)),
     value = c(estA,estB),
     lb = c(Alb,Blb),
     ub = c(Aub,Bub)
   )
)
ggplot(test,aes(x=Loc,y=value,color=est))+geom_point()+
    geom_errorbar(aes(ymax=ub,ymin=lb))


If you like to use the reshape package, I found following solution (assuming that your data frame's variables are named w/o spaces):

head(df)

  Loc       estA        estB         Alb       Aub         Blb        Bub
1   a 0.56327093 0.261222077 0.262976608 1.2064729 0.111866194 0.60998744
2   b 0.13710987 0.036116906 0.072413817 0.2596068 0.016916967 0.07710785
3   c 0.09465147 0.038929501 0.049682119 0.1803244 0.018676127 0.08114670
4   d 0.18510306 0.034148992 0.083935852 0.4082063 0.012147149 0.09600225
5   e 0.03225662 0.007222061 0.006303808 0.1650573 0.001263808 0.04127063
6   f 0.12250992 0.075619254 0.049056208 0.3059487 0.027953280 0.20456532

test <- data.frame(cbind(melt(df[,1:3]), lb=c(df$Alb, df$Blb), ub=c(df$Aub, df$Bub)))
ggplot(test2,aes(x=Loc,y=value,color=variable)) + geom_point() + geom_errorbar(aes(ymax=ub,ymin=lb))

You just melt the est-columns and bind the melted data frame with catenated lower/upper bounds columns.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜