-5

I hope to make graph like this: image from a journal

My data is like:

temp<-data.frame(agecat=c("40-49","40-49","50-59","50-59","60+","60+"),
                  ANY=c("NO","YES","NO","YES","NO","YES"),
                  median=c(0.81,0.83,0.78,0.83,0.84,0.89),
                  up=c(1.2,1.25,1.25,1.31,1.44,1.34),
                  down=c(0.53,0.41,0.41,0.47,0.56,0.69))

(the HIV in graph is like ANY in my figures)

I tried the code below, it doesn't work.

ggplot(temp,aes(agecat,median,group=ANY,color=ANY,LABEL=round(median)))+geom_point()+facet_grid(~ANYPLAQ+agecat, scales="free", space="free")+geom_errorbar(aes(ymin=down,ymax=up),width=0.2)

I could only get figure like this: enter image description here

I hope the width between each column are different for within age group and between age groups.

Could someone give me some suggestions, thanks!!

  • Start by getting rid of the `facet_grid`, which is a tool for splitting one chart into several charts. When I try `ggplot(temp, aes(agegroup,median,group=ANY,color=ANY,LABEL=round(median))) + geom_point() + geom_errorbar(aes(ymin=down,ymax=up),width=0.2)`, I get something which is starting to resemble your desired graph. I would suggest changing your question to omit any mention of `facet_grid`, and include the actual picture of the chart in the question. – lebelinoz Apr 29 '17 at 22:11
  • Also, your `temp` data uses variable `agegroup` while your code uses `agecat`. Make them consistent, please. – lebelinoz Apr 29 '17 at 22:12
  • @lebelinoz thanks for your suggestion!, but I really don't know how to add actual picture in the post, instead of the picture link. – Zhenyu Zhang Apr 30 '17 at 01:41

1 Answers1

0

It's hard to tell what's going on, as the variables in the data frame you gave are different from the variables used in ggplot() call, but many faceting errors in ggplot can be resolved by calling as.factor(your_variable). For example facet_grid(~as.factor(ANYPLAQ) + as.factor(agecat) hope this helps, next time be more specific.