Why does xlim cut off border values of continuous scale?

axis-labels, ggplot2, plot, r

Solution

A side effect of a feature. Since you've binned the data yourself, you probably want a discrete scale, and a factor for `x`:

ggplot(d) +
    geom_histogram(aes(x=factor(bin_start), y=bin_count), stat="identity", colour="black", fill="white") +
    scale_x_discrete(limit=as.character(1:6))

Problem

Given data frame: ``` d = structure(list(bin_start = 1:12, bin_count = c(12892838L, 1921261L, 438219L, 126650L, 41285L, 15948L, 6754L, 3274L, 1750L, 992L, 703L, 503L)), .Names = c("bin_start", "bin_count"), class = "data.frame", row.names = c(NA, 12L)) ``` I can build histogram with `stat="identity"`: ``` ggplot(d) + geom_histogram(aes(x=bin_start, y=bin_count), stat="identity", colour="black", fill="white") + scale_x_continuous(breaks=1:12) ``` that looks like this: Not being happy with long tail I limit x scale (equivalent to `xlim=c(1,6)`): ``` ggplot(d) + geom_histogram(aes(x=bin_start, y=bin_count), stat="identity", colour="black", fill="white") + scale_x_continuous(breaks=1:12, limits=c(1,6)) ``` but I get border points `x=1` and `x=6` disappear: Note, that y axis still scales like border points belong to the plot aesthetics. Is it a feature or a bug?

Original source