Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Truncate highest bar in ggplot

Tags:

r

ggplot2

Please consider the following

library(ggplot)
data <- data.frame(qnt=c(10,20,22,12,14,9,1000),lbl=c("A","B","C","D","E","F","G"))
ggplot(data=data, aes(x=lbl, y=qnt)) + geom_histogram(stat="identity")

which produces

enter image description here

Which options should I consider to truncate the highest bar G in the plot? (of course explaining to the viewer what I did)

like image 644
CptNemo Avatar asked Nov 27 '13 06:11

CptNemo


1 Answers

If you want to fiddle with it, you can use the gridExtra package, and plot 2 (or more) trimmed out sections of the graph. I've tinkered with the margins to make it line up, but a better plan would probably be to format the axis labels to the same text width,

enter image description here

require(ggplot2)
require(gridExtra)
data <- data.frame(qnt=c(10,20,22,12,14,9,1000),lbl=c("A","B","C","D","E","F","G"))

g1<-ggplot(data=data, aes(x=lbl, y=qnt)) + 
  geom_histogram(stat="identity")+  
  coord_cartesian(ylim=c(-10,50)) + 
  labs(x=NULL, y=NULL)+
  theme(plot.margin=unit(c(2,2,6,3),"mm")) 


g2<-ggplot(data=data, aes(x=lbl, y=qnt)) + 
  geom_histogram(stat="identity") +  
  coord_cartesian(ylim=c(990,1010)) +
  theme(axis.text.x = element_blank(),
        axis.title.x = element_blank(),
        axis.title.y = element_blank(),
        axis.ticks.x = element_blank()) + 
  labs(x=NULL, y=NULL) + 
  theme(plot.margin=unit(c(5,2,0,0),"mm")) 

grid.arrange(g2,g1, heights=c(1/4, 3/4), ncol=1, nrow=2)
like image 109
Troy Avatar answered Nov 02 '22 18:11

Troy