scale_discrete

scale_x_discrete(name=NULL, expand=c(0.05, 0.55), limits=NULL, breaks=NULL, labels=NULL, formatter=identity, drop=FALSE, ...)
scale_y_discrete(name=NULL, expand=c(0.05, 0.55), limits=NULL, breaks=NULL, labels=NULL, formatter=identity, drop=FALSE, ...)
scale_z_discrete(name=NULL, expand=c(0.05, 0.55), limits=NULL, breaks=NULL, labels=NULL, formatter=identity, drop=FALSE, ...)

Discrete position scale

This page describes scale_discrete, see layer and qplot for how to create a complete plot from individual components.

What do you think of the documentation? Please let me know by filling out this short online survey.

Parameters

Parameters control the appearance of the scale. In addition to the parameters listed below (if any), any aesthetic can be used as a parameter, in which case it will override any aesthetic mapping.

Returns

This function returns a scale object.

Examples

> qplot(cut, data=diamonds, stat="bin") 
  
> qplot(cut, data=diamonds, geom="bar") 
  
>  
> # The discrete position scale is added automatically whenever you 
> # have a discrete position. 
>  
> (d <- qplot(cut, clarity, data=subset(diamonds, carat > 1), geom="jitter")) 
  
>  
> d + scale_x_discrete("Cut") 
  
> d + scale_x_discrete("Cut", labels=c("F","G","VG","P","I")) 
  
>  
> d + scale_y_discrete("Clarity") 
  
> d + scale_x_discrete("Cut") + scale_y_discrete("Clarity") 
  
>  
> # Use limits to adjust the which levels (and in what order) 
> # are displayed 
> d + scale_x_discrete(limits=c("Fair","Ideal")) 
Warning: Removed 11189 rows containing missing values (geom_point).
  
>  
> # you can also use the short hand functions xlim and ylim 
> d + xlim("Fair","Ideal", "Good") 
Error: Continuous variable (cut) supplied to discrete scale_discrete.
  
> d + ylim("I1", "IF") 
Error: Continuous variable (clarity) supplied to discrete scale_discrete.
  
>  
> # See ?reorder to reorder based on the values of another variable 
> qplot(manufacturer, cty, data=mpg) 
  
> qplot(reorder(manufacturer, cty), cty, data=mpg) 
  
> qplot(reorder(manufacturer, displ), cty, data=mpg) 
  
>  
> # Use abbreviate as a formatter to reduce long names 
> qplot(reorder(manufacturer, cty), cty, data=mpg) + 
+   scale_x_discrete(formatter = "abbreviate") 
  

What do you think of the documentation? Please let me know by filling out this short online survey.