Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does xlim cut off border values of continuous scale?

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:

enter image description here

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:

enter image description here

Note, that y axis still scales like border points belong to the plot aesthetics. Is it a feature or a bug?

like image 441
topchef Avatar asked Aug 13 '13 13:08

topchef


1 Answers

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))

enter image description here

like image 90
Peyton Avatar answered Oct 04 '22 15:10

Peyton