0

I have a ggplot2 theme that I like for facetted plots, but want to add axis tickmarks as described in these threads, but this time for the multipanel case:

As an example, I include the following code:

## load libraries
library(ggplot2)
library(grid)
library(reshape2)
library(dplyr)

## ggplot settings
theme_update(strip.text=element_text(size=14),
             strip.text.x=element_text(vjust=1),
             strip.text.y=element_text(vjust=0,angle=90),
             strip.background=element_rect(color=NA,fill=NA,
               linetype=0),
             axis.text=element_text(size=12),
             axis.text.y=element_text(angle=90),             
             axis.ticks.length = unit(-0.3, "lines"),
             axis.ticks.margin = unit(0.5, "lines"),
             panel.border=element_rect(color=1,fill=NA),             
             panel.grid.major = element_blank(), 
             panel.grid.minor = element_blank())

## transform the iris dataset
iris.long <- melt(add_rownames(iris,"Sample"),id.vars=c("Sample","Species"))
iris.long$Component <- sub("(.+)\\.(.+)","\\1",iris.long$variable)
iris.long$Dimension <- sub("(.+)\\.(.+)","\\2",iris.long$variable)
iris.wide <- dcast(iris.long,Sample+Species+Component~Dimension)

## make the plot
ggplot(iris.wide)+
  geom_point(aes(Length,Width))+
    facet_grid(Species~Component)

which gives the following figure:

multipanel figure generated with ggplot2

Ticks on the top and right (and also bottom and left for panels that don't have the tick labels) would be an improvement. I am not as familiar with the inner workings with the internals of ggplot/grid graphics - but is this something that can be implemented in a formulaic way? ggplot2::annotation_logticks() seems to have implemented such a feature, but I cannot use it with linear scale plots.

Community
  • 1
  • 1
hatmatrix
  • 42,883
  • 45
  • 137
  • 231
  • It ain't pretty, but this might serve: http://stackoverflow.com/questions/13297155/add-floating-axis-labels-in-facet-wrap-plot/13316126#13316126 – Shawn Mehan Aug 10 '15 at 22:21
  • @jeremycg, thanks. fixed now. – hatmatrix Aug 10 '15 at 22:24
  • @ShawnMehan, thanks for the suggestion but I think that's too far away from my application to adapt with minor modifications (that I can see). – hatmatrix Aug 10 '15 at 22:26
  • There was some work done on something like this (see the files guide-axis.r and coord-.r in the package source) but it has never been "hooked up", probably because it turned out to be trickier than it seemed. – joran Aug 10 '15 at 22:31

1 Answers1

0

An alternative would be to use facet_wrap, specifying scales = "free" so scales vary across rows and columns and all axes are shown:

ggplot(iris.wide)+
  geom_point(aes(Length,Width))+
  facet_wrap(Species~Component, scales ="free", ncol = 2)

enter image description here

mpalanco
  • 12,960
  • 2
  • 59
  • 67