Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: remove extra x-axis value (ggplot2)

Tags:

r

ggplot2

axis

I am using bar graph with ggplot2 library and when number of values (dates in my case) on x-axis is high, additional blank value ticks appear on the beginig and the end of axis. Please, is there a way how to remove these? Reproducible code and image below. enter image description here

library(ggplot2)
a <- runif(28, 2.0, 7.5)
b <- seq(as.Date("1910/1/1"), as.Date("1910/1/28"), "days")
ds = data.frame(a, b)

p <- 
ggplot(data=ds, aes(b, a), environment = environment()) +  
theme(panel.grid.major.x =  element_blank(), panel.grid.minor =  element_blank(),
      panel.grid.major.y = element_line(color="grey"), 
      panel.background = element_blank(), panel.border = element_rect(fill= NA, colour = "grey")) +
geom_bar(width=.4,stat="identity") + 
xlab(" ") + ylab(" ") +
theme(text = element_text(size=20), axis.text.x = element_text(angle=90), axis.text = element_text(color="black"),
      legend.key = element_rect(fill="white")) +
scale_x_date(breaks = date_breaks("1 day"), labels = date_format("%d.%B %y")) 

print(p)
like image 758
Ollaws Avatar asked Aug 14 '14 11:08

Ollaws


1 Answers

When I put breaks = b instead of breaks = date_breaks("1 day"), it solves the problem. Also you need library(scales) to run your example now.

like image 173
Mahbubul Majumder Avatar answered Nov 27 '22 02:11

Mahbubul Majumder