[R] ggplot qplot bar removing bars when truncating scale

Hadley Wickham hadley at rice.edu
Sat Jul 3 15:03:19 CEST 2010


This is possible in ggplot2, but it's an not appropriate use of a bar
chart - because length is used to convey value, chopping the bottoms
of the bars of will give a misleading impression of the data.
Instead, use a dot plot:

data$Q <- unlist(lapply(data$Q, function(x) paste(strwrap(x, 20),
collapse = "\n")))

qplot(mean, Q, data = data, colour = variable, xlab = NULL, ylab = NULL)

Hadley

On Wed, Jun 30, 2010 at 10:12 AM, ml692787 <matthew.lester.mdl at gmail.com> wrote:
>
> I'm having problems with this example, it is posted with reproduceable code
> below, both with the normal 0-6 scale and the desired 3-6 scale (with bars
> removed). How can I get the graph to have the desired 3-6 scale without
> removing the bars. Thanks!
>
> #Data
> mean=as.numeric(c(5.117647059,5,4.947368421,4.85,4.6875,4.545454545,4.473684211,4.470588235,4.428571429,4.083333333,3.421052632,3.235294118))
> data=as.data.frame(cbind(mean,c("Achievement","Achievement","Achievement","Impact","Achievement","Achievement","Achievement","Impact","Impact","Impact","Impact","Impact"),c("Update
> knowledge and skills","Meet requirements for current position","Discover new
> job opportunities","Discover new job opportunities","Transition to a new
> job","Meet requirements for certificaiton","Personal enrichment","Update
> knowledge and skills","Meet requirements for current position","Meet
> requirements for certificaiton","Personal enrichment","Transition to a new
> job")))
> colnames(data)=c("mean","variable","Q")
> data[,1]=mean
>
> #Plot
> p=qplot(data=data,data$Q,data$mean,fill=data$variable,geom="bar",stat="identity",position="dodge",binwidth=2,ylab=NULL,xlab=NULL,width=.75)
>
> #With 0-6 Scale
> p + scale_x_discrete(expand=c(0,0)) +
> scale_y_continuous(limits=c(0,7),breaks=seq(from=0,to=6,by=.5),expand=c(0,0))
> +
> coord_flip() +
> scale_fill_manual(values=c("darkmagenta","lightgoldenrod1")) +
>                opts(
>                        panel.background = theme_rect(colour = NA),
>                        panel.background = theme_blank(),
>                        panel.grid.minor = theme_blank(),
>                        axis.title.x= theme_blank(),
>                        axis.title.y= theme_blank(),
>                        axis.text.y=theme_text(size=12,hjust=1),
>                        legend.text=theme_text(size=14)
>                        )
>
> #With 3-6 Scale (Bars Deleted)
> p + scale_x_discrete(expand=c(0,0)) +
> scale_y_continuous(limits=c(3,6),breaks=seq(from=3,to=6,by=.5),expand=c(0,0))
> +
> coord_flip() +
> scale_fill_manual(values=c("darkmagenta","lightgoldenrod1")) +
>                opts(
>                        panel.background = theme_rect(colour = NA),
>                        panel.background = theme_blank(),
>                        panel.grid.minor = theme_blank(),
>                        axis.title.x= theme_blank(),
>                        axis.title.y= theme_blank(),
>                        axis.text.y=theme_text(size=12,hjust=1),
>                        legend.text=theme_text(size=14)
>                        )
>
> There is probably an option I'm missing or maybe my data should be set up
> differently, any help would be much appreciated!!
> --
> View this message in context: http://r.789695.n4.nabble.com/ggplot-qplot-bar-removing-bars-when-truncating-scale-tp2272735p2272735.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/



More information about the R-help mailing list