Independent control over axis lines in 0.9.2

527 views
Skip to first unread message

Sandy Muspratt

unread,
Sep 6, 2012, 4:46:55 AM9/6/12
to ggp...@googlegroups.com

I'm not able to modify one or the other axis lines using version 0.9.2. 
Using the following base plot:

      library(ggplot2)
      (baseplot = ggplot(data.frame(x = 1:10, y = 1:10), aes(x,y)) + geom_point() )

I can modify both axis lines using:

     baseplot +  theme(axis.line = element_line(colour = "red", linetype = "dashed", size = 1))

But I can't modify the x axis line only using:

     baseplot + theme(axis.line.x = element_line(colour = "red", linetype = "dashed", size = 1))
 
The results appears to be the same as the base plot (i.e., no modification to the x axis line).  Am I doing something wrong?


> sessionInfo()
R version 2.15.1 (2012-06-22)
Platform: i386-pc-mingw32/i386 (32-bit)

locale:
[1] LC_COLLATE=English_Australia.1252  LC_CTYPE=English_Australia.1252  
[3] LC_MONETARY=English_Australia.1252 LC_NUMERIC=C                     
[5] LC_TIME=English_Australia.1252   

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base    

other attached packages:
[1] ggplot2_0.9.2

loaded via a namespace (and not attached):
 [1] colorspace_1.1-1   dichromat_1.2-4    digest_0.5.2       grid_2.15.1      
 [5] gtable_0.1.1       labeling_0.1       MASS_7.3-21        memoise_0.1      
 [9] munsell_0.3        plyr_1.7.1         proto_0.3-9.2      RColorBrewer_1.0-5
[13] reshape2_1.2.1     scales_0.2.2       stringr_0.6.1 

Dennis Murphy

unread,
Sep 6, 2012, 5:03:14 AM9/6/12
to Sandy Muspratt, ggp...@googlegroups.com
Hi:

Because of the inheritance structure built into the new theming system, the way to do it is to modify axis.line and then use element_blank() on axis.line.y:

baseplot +  theme(axis.line = element_line(colour = "red", linetype = "dashed", size = 1),
                           axis.line.y = element_blank())

axis.line.x will inherit from axis.line and you'll get the behavior you expect.

Dennis

--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

Sandy Muspratt

unread,
Sep 6, 2012, 7:15:58 PM9/6/12
to ggp...@googlegroups.com, Sandy Muspratt

Hi Dennis,
Thank you so much for you reply. You code works, but only if one axis line is to be modified and the other is to be removed.
But what if I want to modify both axis lines: x-axis line coloured red and y-axis line coloured blue. It seems I can't do that.

Version 0.9.2 gives me independent control over other x and y elements. For instance I can independently modify the tick marks:

      baseplot + theme(axis.ticks.x  = element_line(colour = "red", size = 2),
                       axis.ticks.y = element_line(colour = "blue", size = 2))

Or I can independently modify the grid lines:

      baseplot + theme(panel.grid.major.x = element_line(colour = "red", size = 1),
                       panel.grid.major.y = element_line(colour = "blue", size = 1))

But it seems the same style of code will not work to independently modify the axis lines:

      baseplot + theme(axis.line.x = element_line(colour = "red", size = 1),
                       axis.line.y = element_line(colour = "blue", size = 1))

Is there a way to apply different modifications to the axis lines?

Thanks
Sandy


Winston Chang

unread,
Sep 6, 2012, 11:15:03 PM9/6/12
to Sandy Muspratt, ggp...@googlegroups.com
This is one area where the theme inheritance combined with the default
theme isn't an improvement on the old system...

The problem is that in the default theme_grey(), axis.line is set to
element_blank(), and the two elements that inherit from axis.line,
axis.line.x and axis.line.y, inherit the blank element. Perhaps it
would be a little easier the default for axis.line was this:
theme(axis.line = element_line(colour=NA))

At any rate, this should do the trick - you just need to set axis.line
to something that's not element_blank():

qplot(1:3, 1:3) +
theme(axis.line = element_line(),
axis.line.x = element_line(colour = "red", size = 1),
axis.line.y = element_line(colour = "blue", size = 1))

-Winston

Sandy Muspratt

unread,
Sep 7, 2012, 12:17:26 AM9/7/12
to ggp...@googlegroups.com, Sandy Muspratt
Hi Winston,

Thanks for the reply. I think I understand now.  Also, I think the theme inheritance is a big improvement on the old system. In the old system, we had to use the grid package to get this sort of independent control over a few of the x and y elements. So I'm saying "Thank you, thank you, thank you".

Sandy

Reply all
Reply to author
Forward
0 new messages