r - Show customised X-axis ticks in ggplot2 -


i have large data frame (2 columns) in terms of records. have plotted graph in ggplot2. x axis time , y axis values. on specific interval time 50 60, want make ticks increments smaller , clear in graph such (50,51,51,53,...59,60). rest of axis, fine have ticks incremented 10. so,i expect have x-axis values :

10,20,30,40,50,51,52,53,54,55,56,57,58,58,60,70,80,90,..190,200. 

below code(with mwe) producing following graph.

x<-seq(1:200) y<-seq(51,250,by=1) df<-data.frame(x=x,y=y)  ggplot(data=df, aes(x,y))+geom_line(size=1.6)+  scale_x_continuous( breaks=c(10,20,30,40,seq(50,60,by=2),seq(70,200,10))                    ,minor_breaks=seq(50,60,by=2) )+ theme(axis.text.x=element_text(size=16),axis.text.y=element_text(size=16))+ theme(axis.title.x=element_text(size=16),axis.title.y=element_text(size=16))+  theme(axis.ticks.x = element_line(size = 1))+xlab("time")+ylab("value")+ theme(axis.ticks.length=unit(0.8,"cm")) 

graph

is there anyway make clear.????

it seems tight squeeze in more labels every 10. may try drop labels @ tickmark 52 58, labelling these 4 positions ""

ggplot(data = df, aes(x = x, y = y)) +   geom_line() +    scale_x_continuous(breaks = c(seq(from = 10, = 200, = 10),                                 seq(from = 52, = 58, = 2)),                      labels = c(seq(from = 10, = 200, = 10), rep("", 4))) 

enter image description here

alternatively, can zoom in on relevant x-range using coord_cartesian. underlying data unchanged, , magnify small section of original data. zoomed-in plot can added original plot subplot. there many ways arrange subplots. here 1 example:

# original plot on full range of x g1 <- ggplot(data = df, aes(x = x, y = y)) +   geom_line()   # zoom in relevant section of x  g2 <- ggplot(data = df, aes(x = x, y = y)) +   geom_line() +   coord_cartesian(xlim = c(49, 61)) +   scale_x_continuous(breaks = seq(from = 50, = 60, = 2))  # print g1, , add g2 on top using viewport package grid g1 print(g2, vp = viewport(x = 0.75, y = 0.3, width = 0.35, height = 0.35)) 

enter image description here


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -