4

I am using the following code to create a plot with three subplots using facet_grid() within ggplot2:

day <- c('5-Aug', '5-Aug','5-Aug','10-Aug','10-Aug','10-Aug','17-Aug','17-Aug','17-Aug')
station <- c(1:3,1:3,1:3)
Mean <- c(382, 1017, 1519, 698, 5398, 2458, 346, 5722, 6253)
StErr<- c(83, 100, 73, 284, 3417, 689, 53, 1796, 732)
df <- data.frame(day,station,Mean,StErr)

library(ggplot2)
library(scales)
ggplot(df, aes(x=station, y=Mean)) + 
geom_errorbar(aes(ymin=Mean-StErr, ymax=Mean+StErr), colour="black", width=.1) +
geom_point(size=2)+
xlab(NULL) +
ylab(expression(paste('Copepods,'~'#/m'^3))) + 
theme_bw() +
theme(
panel.grid.major = element_blank(),
panel.grid.minor = element_blank()
) + 
scale_x_continuous(expand=c(.3,0), breaks=c(1:3), labels=c("In", "FL", "Off")) + 
annotation_logticks(sides = "l") + 
scale_y_log10(limit=c(100,10000)) +
theme(axis.text.x=element_text(size=12)) +
theme(axis.text.y=element_text(size=12)) +
facet_grid(.~day)

However, the log axis ticks I've created using annotation_logticks() appear on all three subplots. Question: Does anyone know how to control which subplots within facet_grid() annotation_logticks() appear on?

I added data to the code and hope that this is a more acceptable and useful way to include sample data. I copied this code into a new R session and it appears to do what I expect. Thank you very much for the helpful link!

wonderoustree
  • 87
  • 2
  • 11
  • 1
    Hi and welcome to stackoverflow! Please have a look [here on how you easily can create a minimal, reproducible data set](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610). See e.g. the `dput` function. Cheers. – Henrik Nov 21 '13 at 18:17

2 Answers2

5

Currently the function annotation_logticks hard codes the data object in the created layer.

You can create your own function that allows you to pass a data.frame containing the faceting variable and levels you wish to add the annotation

add_logticks  <- function (base = 10, sides = "bl", scaled = TRUE, 
   short = unit(0.1, "cm"), mid = unit(0.2, "cm"),  long = unit(0.3, "cm"), 
   colour = "black",  size = 0.5, linetype = 1, alpha = 1, color = NULL, 
   data =data.frame(x = NA),... )   {
  if (!is.null(color)) 
    colour <- color
  layer(geom = "logticks", geom_params = list(base = base, 
          sides = sides, raw = raw, scaled = scaled, short = short, 
          mid = mid, long = long, colour = colour, size = size, 
          linetype = linetype, alpha = alpha, ...), 
        stat = "identity", data =data , mapping = NULL, inherit.aes = FALSE, 
        show_guide = FALSE)
}
# The plot without logticks
overall <- ggplot(df, aes(x=station, y=Mean)) + 
  geom_errorbar(aes(ymin=Mean-StErr, ymax=Mean+StErr), colour="black", width=.1) +
  geom_point(size=2)+
  xlab(NULL) +
  ylab(expression(paste('Copepods,'~'#/m'^3))) + 
  theme_bw() +
  theme(
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank()
  ) + 
  scale_x_continuous(expand=c(.3,0), breaks=c(1:3), labels=c("In", "FL", "Off")) + 
  scale_y_log10(limit=c(100,10000)) +
  theme(axis.text.x=element_text(size=12)) +
  theme(axis.text.y=element_text(size=12)) +
  facet_grid(.~day)

overall + add_logticks(side = 'l', data = data.frame(x= NA, day = '5-Aug'))

enter image description here

mnel
  • 113,303
  • 27
  • 265
  • 254
  • `ggplot2::annotation_logticks` has changed a little since this answer. This approach still works fine, but you will want to change `geom_params` to `params`, change `show_guide` to `show.legend`, remove `raw = raw` (what was that for anyway?), add `position = "identity"`, and if you want add `outside` to the list of params and to the function args. https://ggplot2.tidyverse.org/reference/annotation_logticks.html https://github.com/tidyverse/ggplot2/blob/master/R/annotation-logticks.r – solarchemist Apr 22 '20 at 14:34
1

An updated version of the function mnel posted above based on solarchemist comments. There have been some updates since with was last answered in 2013:

add_logticks  <- function (base = 10, sides = "bl", scaled = TRUE, 
                           short = unit(0.1, "cm"), mid = unit(0.2, "cm"),  long = unit(0.3, "cm"), 
                           colour = "black",  size = 0.5, linetype = 1, alpha = 1, color = NULL, 
                           data =data.frame(x = NA),... )   {
  if (!is.null(color)) 
    colour <- color
  layer(geom = "logticks", params = list(base = base, 
                                              sides = sides, scaled = scaled, short = short, 
                                              mid = mid, long = long, colour = colour, size = size, 
                                              linetype = linetype, alpha = alpha, ...), 
        stat = "identity", data = data , mapping = NULL, inherit.aes = FALSE, position = "identity",
        show.legend = FALSE)
}

Data:

day <- c('5-Aug', '5-Aug','5-Aug','10-Aug','10-Aug','10-Aug','17-Aug','17-Aug','17-Aug')
station <- c(1:3,1:3,1:3)
Mean <- c(382, 1017, 1519, 698, 5398, 2458, 346, 5722, 6253)
StErr<- c(83, 100, 73, 284, 3417, 689, 53, 1796, 732)
df <- data.frame(day,station,Mean,StErr)

Plot:

overall <- ggplot(df, aes(x=station, y=Mean)) + 
  geom_errorbar(aes(ymin=Mean-StErr, ymax=Mean+StErr), colour="black", width=.1) +
  geom_point(size=2)+
  xlab(NULL) +
  ylab(expression(paste('Copepods,'~'#/m'^3))) + 
  theme_bw() +
  theme(
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank()
  ) + 
  scale_x_continuous(expand=c(.3,0), breaks=c(1:3), labels=c("In", "FL", "Off")) + 
  scale_y_log10(limit=c(100,10000)) +
  theme(axis.text.x=element_text(size=12)) +
  theme(axis.text.y=element_text(size=12)) +
  facet_grid(.~day)

overall + add_logticks(side = 'l', data = data.frame(x= NA, day = '5-Aug'))

example

neuron
  • 1,949
  • 1
  • 15
  • 30