thiagoveloso
thiagoveloso

Reputation: 2763

R ggplot2 - Add ticks on top and right sides of all facets

I am using ggplot2 to create the following plot:

library(ggplot2)

df <- data.frame(site=rep(c("CA-Oas","US-Oho","CA-Obs","CA-TP1"), each=124),
                 exper=rep(c("bberry","medlyn"), each=62, 4),
                 sm=runif(496, min=0, max=100),
                 lh=runif(496, min=0, max=400),
                 month=rep(c("Jul","Aug"), each=31, 8))

ggplot(df, aes(x=sm, y=lh, colour=exper, shape=month)) + 
  geom_point(size=2.5, alpha=0.9) +
  facet_wrap(~site, nrow=2) +
  theme_bw(base_size=18) +
  ylab(bquote('Latent heat ('*W~m^-2*')')) +
  xlab(bquote('Soil water content (%)')) +
  scale_color_manual(values=c("bberry"="tomato2", "medlyn"="dodgerblue"),
                     labels=c("bberry"="Ball-Berry", "medlyn"="Medlyn")) +
  theme(panel.grid.minor=element_blank(),
        panel.grid.major=element_blank(),
        legend.title=element_blank(),
        legend.box="horizontal",
        legend.position=c(0.4, 0.6),
        legend.spacing = unit(-0.2,"cm"))

enter image description here

However, the journal I am writing a manuscript to requires all plots with ticks on both axis (top/bottom and left/right). I am struggling to achieve that on ggplot2.

Basically, what I need is to:

  1. Add ticks (either internal or external) to both the top and right axis on all facets;
  2. Keep the x-axis labels only on the bottom and the y-axis labels only on the left sides of the facets.

This is roughly what I need to achieve (notice the axis ticks and labels): enter image description here

Any hints on how to get there?

Upvotes: 1

Views: 2702

Answers (2)

Z.Lin
Z.Lin

Reputation: 29085

facet_rep_wrap from the lemon package is perfect for this, as it preserves axis lines (& optionally labels) in all facets.

See inline comments below for other tweaks to the code:

library(lemon)

ggplot(df, aes(x=sm, y=lh, colour=exper, shape=month)) + 
  geom_point(size=2.5, alpha=0.9) +
  facet_rep_wrap(~site, nrow = 2) +              # instead of facet_wrap
  theme_bw(base_size=18) +
  ylab(bquote('Latent heat ('*W~m^-2*')')) +
  xlab(bquote('Soil water content (%)'))  +
  scale_x_continuous(sec.axis = dup_axis()) +    # add duplicated secondary axis to get 
  scale_y_continuous(sec.axis = dup_axis()) +    # axes on top / right sides
  scale_color_manual(values=c("bberry"="tomato2", "medlyn"="dodgerblue"),
                     labels=c("bberry"="Ball-Berry", "medlyn"="Medlyn")) +
  theme(panel.grid.minor=element_blank(),
        panel.grid.major=element_blank(),
        legend.title=element_blank(),
        legend.box="horizontal",
        legend.position=c(0.4, 0.6),
        legend.spacing = unit(-0.2,"cm"),
        strip.placement = "outside",             # place facet strips outside axis
        axis.ticks.length = unit(-2.75, "pt"),   # point axis ticks inwards (2.75pt is 
                                                 # the default axis tick length here)
        axis.text.x.top = element_blank(),       # do not show top / right axis labels
        axis.text.y.right = element_blank(),     # for secondary axis
        axis.title.x.top = element_blank(),      # as above, don't show axis titles for
        axis.title.y.right = element_blank())    # secondary axis either

result

(Note: It's possible to achieve the same result with just ggplot2 & some grob hacking, but that approach is rather more complicated, so I wouldn't go there unless installing new packages is really cumbersome in a computing environment...)

Upvotes: 5

Jon Spring
Jon Spring

Reputation: 66490

(This gets ticks along the top and right edges, but not for all facets. And adding scales = "free" to the facet_wrap call will add ticks for all facets, but it also adds labels to all of them, which is redundant.)

ggplot(df, aes(x=sm, y=lh, colour=exper, shape=month)) + 
  geom_point(size=2.5, alpha=0.9) +
  facet_wrap(~site, nrow=2) +
  theme_bw(base_size=18) +
  ylab(bquote('Latent heat ('*W~m^-2*')')) +
  xlab(bquote('Soil water content (%)')) +
  scale_color_manual(values=c("bberry"="tomato2", "medlyn"="dodgerblue"),
                     labels=c("bberry"="Ball-Berry", "medlyn"="Medlyn")) +
  scale_x_continuous(sec.axis = dup_axis(labels = NULL)) +   # NEW
  scale_y_continuous(sec.axis = dup_axis(labels = NULL)) +   # NEW
  theme(panel.grid.minor=element_blank(),
        panel.grid.major=element_blank(),
        strip.placement = "outside",                         # NEW
        legend.title=element_blank(),
        legend.box="horizontal",
        legend.position=c(0.4, 0.6),
        legend.spacing = unit(-0.2,"cm"))

Upvotes: 1

Related Questions