Katherine Drummond
Katherine Drummond

Reputation: 85

How to change the position of labels in scale_x_continuous in ggplot2?

I was hoping to receive some guidance on how to reduce the space between labels specified in scale_x_continuous and the x axis. The empty space I would like to remove is indicated in the text in red in the following diagram.

Thank you for your help - it is much appreciated!

enter image description here

I have attached the code to reproduce this plot.

i <- structure(list(wd = c(0, 112.5, 135, 180, 202.5, 22.5, 225, 247.5, 
270, 292.5, 337.5, 45, 67.5, 90), co2 = c(389.82, 376.82, 386.06, 
392.04, 392.55, 387.97, 391.45, 389.87, 390.12, 389.68, 391.39, 
390.1, 386.89, 383.05), ci1 = c(388.37, 367.67, 378.98, 381.76, 
388.63, 386.65, 388.32, 388.5, 389.03, 387.25, 389.05, 388.65, 
385.64, 381.1), ci2 = c(391.26, 385.98, 393.15, 402.31, 396.46, 
389.28, 394.58, 391.23, 391.21, 392.12, 393.73, 391.55, 388.15, 
385.01)), row.names = c(NA, -14L), class = "data.frame")


library(ggplot2)
ggplot(data = i, aes(x = wd, y = co2)) +
  geom_point(size=4, colour = "red")+
  geom_linerange(aes(ymax =ci2, ymin=ci1), colour = "red", size = 2)+
  coord_polar()+
  
  geom_hline(yintercept = seq(365, 405, by = 5), colour = "grey", size = 0.2) +
  geom_vline(xintercept = seq(0, 360, by = 22.5), colour = "grey", size = 0.2) +
  scale_x_continuous(limits = c(0, 360), expand = c(0, 0), 
                     breaks = seq(0, 359.99, by = 22.5), 
                     labels=c("N","NNE","NE","ENE","E","ESE","SE","SSE",
                              "S","SSW","SW","WSW","W","WNW","NW","NNW"),position = "top") +
  scale_y_continuous(limits = c(365, 405), breaks = seq(365, 405, by = 10)) +
  
  theme_bw() +
  theme(panel.border = element_blank(),
        panel.grid  = element_blank(),
        legend.key = element_blank(),
        axis.ticks = element_line(colour = "grey"),
        axis.ticks.length = unit(-1, "lines"),
        axis.ticks.margin = unit(1.3,"lines"),
        axis.text =  element_text(size=24),
        axis.text.x = element_text(vjust = -10),
        axis.text.y = element_text(size=24),
   
        axis.title = element_blank(),
        axis.line=element_line(),
        axis.line.x=element_blank(),
        axis.line.y = element_line(colour = "grey"),
        plot.title = element_text(hjust = 0, size = 20))

Upvotes: 5

Views: 1496

Answers (1)

Allan Cameron
Allan Cameron

Reputation: 174258

This is pretty easy to do with coord_curvedpolar from geomtextpath. The vjust argument will work as expected, pushing or pulling the text away from or towards the center of the plot. Here is axis.text.x = element_text(vjust = 2)

library(ggplot2)
library(geomtextpath)

ggplot(data = i, aes(x = wd, y = co2)) +
  geom_point(size=4, colour = "red")+
  geom_linerange(aes(ymax =ci2, ymin=ci1), colour = "red", size = 2)+
  coord_curvedpolar()+
  
  geom_hline(yintercept = seq(365, 405, by = 5), colour = "grey", size = 0.2) +
  geom_vline(xintercept = seq(0, 360, by = 22.5), colour = "grey", size = 0.2) +
  scale_x_continuous(limits = c(0, 360), expand = c(0, 0), 
                     breaks = seq(0, 359.99, by = 22.5), 
                     labels=c("N","NNE","NE","ENE","E","ESE","SE","SSE",
                              "S","SSW","SW","WSW","W","WNW","NW","NNW"),position = "top") +
  scale_y_continuous(limits = c(365, 405), breaks = seq(365, 405, by = 10)) +
  theme_bw() +
  theme(panel.border = element_blank(),
        panel.grid  = element_blank(),
        legend.key = element_blank(),
        axis.ticks = element_line(colour = "grey"),
        axis.ticks.length = unit(-1, "lines"),
        axis.text =  element_text(size = 12),
        axis.text.x = element_text(vjust = 2),
        axis.text.y = element_text(size = 12),
        axis.title = element_blank(),
        axis.line=element_line(),
        axis.line.x=element_blank(),
        axis.line.y = element_line(colour = "grey"),
        plot.title = element_text(hjust = 0, size = 20))

enter image description here

The same plot with axis.text.x = element_text(vjust = 5)

enter image description here

The same plot with axis.text.x = element_text(vjust = -1)

enter image description here

Upvotes: 6

Related Questions