r - Manipulating x axis in panel plot when a factor is used -
the code i've used is:
mcgc <- ggplot(sam, aes(x = person,y = m, colour = x)) + geom_point(size = 0.75) + scale_colour_gradient2(high="red", mid="green", limits=c(0,1), guide = "colourbar") + geom_hline(aes(yintercept = mad, linetype = "mad"), colour = "blue", size=0.75, show_guide = true) + geom_hline(aes(yintercept = mmad, linetype = "mmad"), colour = "black", size=0.75, show_guide = true) + facet_wrap(~ plan, scales = "free", ncol = 4) + scale_linetype_manual(name = "plan of health care", values = c("mad" = 1, "mmad" = 1),guide = "legend")
for data:
plan person x m mad mmad 1 1 95 0.323000 0.400303 0.12 1 2 275 0.341818 0.400303 0.12 1 3 2 0.618000 0.400303 0.12 1 4 75 0.320000 0.400303 0.12 1 5 13 0.399000 0.400303 0.12 1 6 20 0.400000 0.400303 0.12 2 7 219 0.393000 0.353350 0.45 2 8 50 0.060000 0.353350 0.45 2 9 213 0.390000 0.353350 0.45 2 15 204 0.496100 0.353350 0.45 2 19 19 0.393000 0.353350 0.45 2 24 201 0.388000 0.353350 0.45 3 30 219 0.567 0.1254 0.89 3 14 50 0.679 0.1254 0.89 3 55 213 0.1234 0.1254 0.89 3 18 204 0.6135 0.1254 0.89 3 59 19 0.39356 0.1254 0.89 3 101 201 0.300 0.1254 0.89
i'm trying manipulate x axis using:
scale_x_continuous(breaks = c(min(person), median(person), max(person)), labels = c(min(person), median(person), max(person)))
however, given had change person
factor order data properly, above code not work. errors, depending on how fiddle around code:
error: discrete value supplied continuous scale error in summary.factor(c(1l, 2l, 3l, 4l, 5l, 6l, 7l, 8l, 9l, 10l, 11l, : min not meaningful factors
changing person
numeric not work, accumulated person
entire dataset on each plan figure panel, opposed scale specific each plan.
is there workaround this?
build plots separately , group them functions gridextra
package.
require(ggplot2) #set breaks , labels (min, median , max) in panel 1 sam1 = subset(sam,sam$plan==1) breaks1=c(min(sam1$person),median(sam1$person),max(sam1$person)) plan1 = ggplot(sam1, aes(x = person,y = m, colour=m)) + geom_point(size = 2.5) + scale_colour_gradient(limits=c(0,1), low="green", high="red", guide="none") + geom_hline(aes(yintercept = mad, linetype = "solid"), colour = "blue", size=0.75, show_guide = true) + geom_hline(aes(yintercept = mmad, linetype = "solid"), colour = "black", size=0.75, show_guide = true) + scale_y_continuous(limits=c(0,1)) + scale_x_continuous("",breaks=breaks1,labels=breaks1) + ggtitle("1") + theme_bw() #set breaks , labels (min, median , max) in panel 2 sam2=subset(sam,sam$plan==2) breaks2=c(min(sam2$person),median(sam2$person),max(sam2$person)) plan2 = ggplot(sam2, aes(x = person,y = m, colour = m)) + geom_point(size = 2.5) + scale_colour_gradient(limits=c(0,1), low="green", high="red", guide="none") + geom_hline(aes(yintercept = mad, linetype = "solid"), colour = "blue", size=0.75, show_guide = true) + geom_hline(aes(yintercept = mmad, linetype = "solid"), colour = "black", size=0.75, show_guide = true) + scale_y_continuous("",limits=c(0,1)) + scale_x_continuous("person",breaks=breaks2,labels=breaks2) + ggtitle("2") + theme_bw() #set breaks , labels (min, median , max) in panel 3 sam3=subset(sam,sam$plan==3) breaks3=c(min(sam3$person),median(sam3$person),max(sam3$person)) plan3 = ggplot(sam3, aes(x = person,y = m, colour = m)) + geom_point(size = 2.5) + scale_colour_gradient(limits=c(0,1), low="green", high="red", guide="colourbar") + geom_hline(aes(yintercept = mad, linetype = "mad"), colour = "blue", size=0.75, show_guide = true) + geom_hline(aes(yintercept = mmad, linetype = "mmad"), colour = "black", size=0.75, show_guide = true) + scale_linetype_manual(name = "plan of health care", values = c("mad" = 1, "mmad" = 1),guide = "legend") + scale_y_continuous("",limits=c(0,1)) + scale_x_continuous("",breaks=breaks3,labels=breaks3) + ggtitle("3") + theme_bw() #using function form user user971102, posted here: http://stackoverflow.com/questions/14920662/arrange-many-plots-using-gridextra g_legend<-function(a.gplot){ tmp <- ggplot_gtable(ggplot_build(a.gplot)) leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box") legend <- tmp$grobs[[leg]] return(legend)} mylegend = g_legend(plan3) require(gridextra) #adjusting panel widths code adapted user971102 , hrbrmstr's code, posted here: http://stackoverflow.com/questions/14920662/arrange-many-plots-using-gridextra grid.arrange(arrangegrob(plan1 + theme(legend.position="none"), plan2 + theme(legend.position="none"), plan3 + theme(legend.position="none"), ncol=3),mylegend, widths=unit(c(900,90),"pt"),nrow=1)
Comments
Post a Comment