Skip to content

Commit

Permalink
Ensure all geoms have correct legend type. Add set_scale_default func…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
hadley committed Feb 20, 2008
1 parent 4f909fd commit 483779b
Show file tree
Hide file tree
Showing 26 changed files with 147 additions and 68 deletions.
61 changes: 37 additions & 24 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,33 +1,46 @@
TODO for this release
* Complete rdoc documentation
* Profiling and some optimisations
* Multicolumn legends (and specification)

ggplot 0.6
----------------------------------------

* fixed bug in combination of stat_bin and geom_area that made it difficult to draw frequency polygons
* coord_trans now deals with groups appropriately, at the cost of decreased speed
* it's now easy to change the default scales (and their arguments) with the set_default_scale function, see ?set_default_scale for more details (thanks to Bob Muenchen for the suggestion)
* new order aesthetic which controls the order in which elements are plotted
* min and max are now scaled the same way as y
* functions are silently dropped (e.g. aes(colour=col))
* all size measurements (now lines and text as well) are measured in mm, lines/paths default to paths 0.5mm wide
* size legends resize to avoid overlaps
* preliminary legend merging code:
* consequence: legend type no longer specified in scale (e.g. size, identity, manual), but derived from geoms used
* fix scale adjustment bug in geom_density
* new geom_rug to add marginal rug plots
* all discrete scales now have labels argument which you can use to override the factor levels
* new stat_spoke to make it possible to use geom_segment parameterised by angle and radius (thanks to Jiho for the suggestion)
* new stat_function makes it easy to superimpose a function on the plot
* the middle line geom_crossbar is now display thicker than the other lines, controlled by the parameter fatten (thanks to Heike Hofmann for the suggestion)
* geom_vline, geom_hline and geom_abline should now behave better in a wider variety of settings
* convenient auto wrapping of simple summary functions for stat_sum
* better documentation for stat_sum
* na.rm added to the following statistics: stat_boxplot, stat_contour, stat_density2d, stat_qq, stat_density (thanks to Leena Choi for suggesting this)
* fixed bug in stat_bin which resulted in increased counts when the x axis was a categorical variable with a single level (thanks to Bob Muenchen for pointing this out!)
* fixed bug in geom_smooth where if se=FALSE x axis always includes 0

Speed and documentation. Profiling and complete rdoc documentation
* coord_polar: add start and direction parameters, giving more control over the layout of the polar coords
* coord_polar: added my favourite pie chart example
* coord_trans now deals with groups appropriately, at the cost of decreased speed
* geom_abline, geom_vline, geom_hline: should now behave better in a wider variety of settings
* geom_crossbar: the middle line is now display thicker than the other lines, controlled by the parameter fatten (thanks to Heike Hofmann for the suggestion)
* geom_density: fix scale adjustment bug in geom_density
* geom_line, geom_text: all size measurements (now lines and text as well) are measured in mm, lines/paths default to paths 0.5mm wide
* geom_rug: new to add marginal rug plots
* geom_smooth: fixed bug where if se=FALSE x axis always includes 0
* qplot: allow qplot to use computed aesthetics too
* scale_discrete: all discrete scales now have labels argument which you can use to override the factor levels
* scale_size: legends resize to avoid overlaps
* scale_x_continuous, scale_y_continuous: new convenience functions xlim and ylim (and zlim) that make it even easier to adjust the limits of the x, y, and z axes
* stat_bin, geom_area: fixed bug in combination of stat_bin and geom_area that made it difficult to draw frequency polygons
* stat_bin: fixed bug which resulted in increased counts when the x axis was a categorical variable with a single level (thanks to Bob Muenchen for pointing this out!)
* stat_bin: no longer incorrectly warns that binwidth is unspecified when breaks are set
* stat_bin: now takes origin argument to manually specify origin of first bin (default is round_any(min(range), bin_width, floor))
* stat_boxplot, stat_contour, stat_density2d, stat_qq, stat_density: na.rm parameter added to the following statistics (thanks to Leena Choi for suggesting this)
* stat_function: new, makes it easy to superimpose a function on the plot
* stat_spoke: new, to make it possible to use geom_segment parameterised by angle and radius (thanks to Jiho for the suggestion)
* stat_summary: better documentation
* stat_summary: convenient auto wrapping of simple summary functions

Legends:

* preliminary legend merging code:
* consequence: legend type no longer specified in scale (e.g. size, identity, manual), but derived from geoms used
* better legends for smooth and pointrange
* fixed some examples where grob used instead of guide

* allow qplot to use computed aesthetics too
* add start and direction parameters to coord_polar, giving more control over the layout of the polar coords
* added my favourite pie chart example to coord_polar
* fixed some examples where grob used instead of guide

Examples

Expand Down
1 change: 1 addition & 0 deletions R/geom-abline.r
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ GeomAbline <- proto(Geom, {
geom_hline = "for horizontal lines",
geom_vline = "for vertical lines"
)
guide_geom <- function(.) "path"

default_stat <- function(.) StatIdentity
default_aes <- function(.) c(GeomPath$default_aes(), aes(intercept = 0, slope = 1))
Expand Down
1 change: 1 addition & 0 deletions R/geom-hline.r
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ GeomHline <- proto(Geom, {

default_stat <- function(.) StatIdentity
default_aes <- function(.) c(GeomPath$default_aes(), aes(intercept=0))
guide_geom <- function(.) "tile"

seealso <- list(
geom_vline = "for vertical lines",
Expand Down
2 changes: 1 addition & 1 deletion R/geom-interval-bar.r
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ GeomBar <- proto(GeomInterval, {
} else {
with(data,
ggname(.$my_name(), rectGrob(x, max, width=width, height=max-min, default.units="native", just=c("centre", "top"),
gp=gpar(col=colour, fill=fill, lwd=size * .pt, lty=linetype))
gp=gpar(col=colour, fill=fill, lwd=size * .pt, lty=linetype, lineend="butt"))
))
}

Expand Down
3 changes: 2 additions & 1 deletion R/geom-interval-crossbar.r
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ GeomCrossbar <- proto(GeomInterval, {

default_stat <- function(.) StatIdentity
default_pos <- function(.) PositionIdentity
default_aes = function(.) aes(colour="black", fill="NA", width=resolution(x) * 0.9, size=1, linetype=1, min=y, max=y)
default_aes = function(.) aes(colour="black", fill="NA", width=resolution(x) * 0.9, size=0.5, linetype=1, min=y, max=y)
guide_geom <- function(.) "path"

draw <- function(., data, scales, coordinates, fatten = 2, ...) {
ggname(.$my_name(), gTree(children=gList(
Expand Down
2 changes: 1 addition & 1 deletion R/geom-interval-error.r
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ GeomErrorbar <- proto(GeomInterval, {

default_stat <- function(.) StatIdentity
default_aes <- function(.) aes(colour = "black", size=0.5, linetype=1, width=0.9)
guide_geom <- function(.) "line"
guide_geom <- function(.) "path"


seealso <- list(
Expand Down
3 changes: 2 additions & 1 deletion R/geom-interval-line.r
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ GeomLinerange <- proto(GeomInterval, {
)

default_stat <- function(.) StatIdentity
default_aes <- function(.) aes(colour = "black", size=1, linetype=1)
default_aes <- function(.) aes(colour = "black", size=0.5, linetype=1)
guide_geom <- function(.) "path"

draw <- function(., data, scales, coordinates, ...) {
munched <- coordinates$transform(data)
Expand Down
11 changes: 11 additions & 0 deletions R/geom-interval-point.r
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ GeomPointrange <- proto(GeomInterval, {
)
default_stat <- function(.) StatIdentity
default_aes <- function(.) aes(colour = "black", size=1, linetype=1, shape=19)
guide_geom <- function(.) "pointrange"

draw <- function(., data, scales, coordinates, ...) {
if (is.null(data$y)) return(GeomLinerange$draw(data, scales, coordinates, ...))
Expand All @@ -25,6 +26,16 @@ GeomPointrange <- proto(GeomInterval, {
GeomPoint$draw(data, scales, coordinates, ...)
)))
}

draw_legend <- function(., data, ...) {
data <- aesdefaults(data, .$default_aes(), list(...))

gTree(children = gList(
GeomPath$draw_legend(data, ...),
GeomPoint$draw_legend(data, ...)
))
}


examples <- function(.) {
# See geom_linerange for examples
Expand Down
6 changes: 1 addition & 5 deletions R/geom-path-.r
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ GeomPath <- proto(Geom, {

draw <- function(., data, scales, coordinates, ...) {
if (nrow(data) < 2) return()

if (is.null(data$order)) data$order <- data$group
data <- data[order(data$order), ]

munched <- coordinates$munch(data)

Expand All @@ -18,8 +15,7 @@ GeomPath <- proto(Geom, {
n <- nrow(munched)
group_diff <- munched$group[-1] != munched$group[-n]
start <- c(TRUE, group_diff)
end <- c(group_diff, TRUE)

end <- c(group_diff, TRUE)

solid_lines <- all(sapply(g, function(df) identical(unique(df$linetype), 1)))
constant <- all(sapply(g, function(df) nrow(unique(df[, c("colour","size","linetype")])) == 1))
Expand Down
1 change: 1 addition & 0 deletions R/geom-polygon.r
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ GeomPolygon <- proto(Geom, {
default_stat <- function(.) StatIdentity
default_aes <- function(.) aes(colour="NA", fill="grey60", size=0.5, linetype=1)
required_aes <- c("x", "y")
guide_geom <- function(.) "tile"

seealso <- list(
geom_path = "an unfilled polygon",
Expand Down
2 changes: 2 additions & 0 deletions R/geom-quantile.r
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ GeomQuantile <- proto(GeomPath, {
advice <- "<p>This can be used as a continuous analogue of a geom_boxplot.</p>\n"
default_stat <- function(.) StatQuantile
default_aes <- function(.) defaults(aes(weight=1, colour="grey50"), GeomPath$default_aes())
guide_geom <- function(.) "path"


icon <- function(.) {
ggname(.$my_name(), gTree(children=gList(
Expand Down
2 changes: 1 addition & 1 deletion R/geom-ribbon-.r
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ GeomRibbon <- proto(GeomInterval, {
}

draw <- function(., data, scales, coordinates, ...) {
data <- subset(data, !is.na(x) & !is.na(y))
data <- data[complete.cases(data[, c("x","min","max")]), ]
tb <- with(data,
coordinates$munch(data.frame(x=c(x, rev(x)), y=c(max, rev(min))))
)
Expand Down
1 change: 1 addition & 0 deletions R/geom-rug.r
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ GeomRug <- proto(Geom, {

default_stat <- function(.) StatIdentity
default_aes <- function(.) aes(colour="black", size=0.5, linetype=1)
guide_geom <- function(.) "path"

examples <- function(.) {
p <- ggplot(mtcars, aes(x=wt, y=mpg))
Expand Down
1 change: 1 addition & 0 deletions R/geom-segment.r
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ GeomSegment <- proto(Geom, {
default_stat <- function(.) StatIdentity
required_aes <- c("x", "y", "xend", "yend")
default_aes <- function(.) aes(colour="black", size=0.5, linetype=1)
guide_geom <- function(.) "path"

examples <- function(.) {
require("maps")
Expand Down
9 changes: 9 additions & 0 deletions R/geom-smooth.r
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,20 @@ GeomSmooth <- proto(GeomInterval, {
}

adjust_scales_data <- function(., scales, data) data
guide_geom <- function(.) "smooth"

default_stat <- function(.) StatSmooth
required_aes <- c("x", "y")
default_aes <- function(.) aes(colour="grey50", fill=alpha("black", 0.2), size=0.5, linetype=1, weight=1)


draw_legend <- function(., data, ...) {
data <- aesdefaults(data, .$default_aes(), list(...))
gTree(children = gList(
GeomTile$draw_legend(data, ...),
GeomPath$draw_legend(data, ...)
))
}
examples <- function(.) {
# See stat_smooth for examples
}
Expand Down
8 changes: 2 additions & 6 deletions R/geom-tile.r
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,9 @@ GeomTile <- proto(Geom, {
}

draw_legend <- function(., data, ...) {
data <- aesdefaults(data, list(colour=ggopt()$grid.fill, fill=NA), list(...))
if (all(is.na(data$fill))) {
data$fill <- data$colour
data$colour <- "grey50"
}
data <- aesdefaults(data, .$default_aes(), list(...))

rectGrob(gp=gpar(col=alpha(data$colour, 1), fill=alpha(data$fill, 1)))
rectGrob(gp=gpar(col=NA, fill=data$fill))
}

adjust_scales_data <- function(., scales, data) {
Expand Down
1 change: 1 addition & 0 deletions R/geom-vline.r
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ GeomVline <- proto(Geom, {

default_stat <- function(.) StatIdentity
default_aes <- function(.) c(GeomPath$default_aes(), aes(intercept=0))
guide_geom <- function(.) "path"

seealso <- list(
geom_hline = "for horizontal lines",
Expand Down
3 changes: 3 additions & 0 deletions R/layer.r
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ Layer <- proto(expr = {

check_required_aesthetics(.$geom$required_aes, c(names(data), names(.$geom_params)), paste("geom_", .$geom$objname, sep=""))

if (is.null(data$order)) data$order <- data$group
data <- data[order(data$order), ]

do.call(.$geom$draw_groups, c(
data = list(as.name("data")),
scales = list(as.name("scales")),
Expand Down
11 changes: 11 additions & 0 deletions R/scale-continuous-.r
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,14 @@ ScaleContinuous <- proto(Scale, {
}
})

xlim <- function(min=NA, max=NA) {
scale_x_continuous(limits = c(min, max))
}

ylim <- function(min=NA, max=NA) {
scale_y_continuous(limits = c(min, max))
}

zlim <- function(min=NA, max=NA) {
scale_z_continuous(limits = c(min, max))
}
26 changes: 26 additions & 0 deletions R/scale-defaults.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Set default scale
# Overrides the default scale with one of your choosing.
#
# @argument
# @argument type of variable (discrete, continuous, date)
# @argument name of new default scale
#X qplot(mpg, wt, data=mtcars, colour=factor(cyl))
#X set_default_scale("colour","discrete", "grey")
#X qplot(mpg, wt, data=mtcars, colour=factor(cyl))
#X set_default_scale("colour","discrete", "hue")
set_default_scale <- function(aesthetic, type, scale, ...) {
default <- paste("scale", aesthetic, type, sep="_")
settings <- list(...)

new_scale <- get(paste("Scale", firstUpper(scale), sep=""))
new_call <- function(...) {
do.call(new_scale$new, c(settings, list(..., variable=aesthetic)))
}
scale <- paste("scale", aesthetic, scale, sep="_")

# For development
if (exists(default, 1, inherits=FALSE)) {
assign(default, new_call, 1)
}
assignInNamespace(default, new_call, "ggplot2")
}
2 changes: 1 addition & 1 deletion R/scale-discrete-.r
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ ScaleDiscrete <- proto(Scale, expr={

train <- function(., x) {
if (is.numeric(x)) {
warning("Numeric variable supplied to discrete scale ", .$name, ".", call.=FALSE)
warning("Continuous variable (", .$name , ") supplied to ", .$my_name(), ", when a discrete variable was expected. ", call.=FALSE)
.$.frange <- range(c(.$.frange, x), na.rm=TRUE)
}
.$.domain <- union(.$.domain, levels(x))
Expand Down
28 changes: 14 additions & 14 deletions R/scale-discrete-colour.r
Original file line number Diff line number Diff line change
Expand Up @@ -44,27 +44,27 @@ ScaleHue <- proto(ScaleColour, expr={
(d <- qplot(carat, price, data=diamonds, colour=clarity))

# Change scale label
d + scale_colour_discrete()
d + scale_colour_discrete("clarity")
d + scale_colour_discrete(expression(clarity[beta]))
d + scale_colour_hue()
d + scale_colour_hue("clarity")
d + scale_colour_hue(expression(clarity[beta]))

# Adjust luminosity and chroma
d + scale_colour_discrete(l=40, c=30)
d + scale_colour_discrete(l=70, c=30)
d + scale_colour_discrete(l=70, c=150)
d + scale_colour_discrete(l=80, c=150)
d + scale_colour_hue(l=40, c=30)
d + scale_colour_hue(l=70, c=30)
d + scale_colour_hue(l=70, c=150)
d + scale_colour_hue(l=80, c=150)

# Change range of hues used
d + scale_colour_discrete(h=c(0, 90))
d + scale_colour_discrete(h=c(90, 180))
d + scale_colour_discrete(h=c(180, 270))
d + scale_colour_discrete(h=c(270, 360))
d + scale_colour_hue(h=c(0, 90))
d + scale_colour_hue(h=c(90, 180))
d + scale_colour_hue(h=c(180, 270))
d + scale_colour_hue(h=c(270, 360))

# Vary opacity
# (only works with pdf, quartz and cairo devices)
d + scale_colour_discrete(alpha = 0.9)
d + scale_colour_discrete(alpha = 0.5)
d + scale_colour_discrete(alpha = 0.2)
d + scale_colour_hue(alpha = 0.9)
d + scale_colour_hue(alpha = 0.5)
d + scale_colour_hue(alpha = 0.2)
}
})
ScaleColourDiscrete <- proto(ScaleHue, objname="discrete", doc=FALSE, examples=function(.) {})
Expand Down
4 changes: 2 additions & 2 deletions R/scale-discrete-grey.r
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
ScaleGrey <- proto(ScaleColour, expr={
doc <- TRUE

new <- function(., name=NULL, variable, start = 0.3, end = 0.9, labels=NULL) {
.$proto(name=name, .input=variable, .output=variable, start=start, end=end, .lables = labels)
new <- function(., name=NULL, variable, start = 0.2, end = 0.8, labels=NULL) {
.$proto(name=name, .input=variable, .output=variable, start=start, end=end, .labels = labels)
}

breaks <- function(.) {
Expand Down
Loading

0 comments on commit 483779b

Please sign in to comment.