Home > Software design >  customize x axis label placement in ggplot coord_polar
customize x axis label placement in ggplot coord_polar

Time:05-28

I'm using coord_polar to show changes in species abundance over time as a clock plot as shown below

year<-c(2017, 2018, 2019) # observation dates
sp1<-c(39,55,85)
sp2 <-c(100, 75, 65)
sp3 <-c(32,57,42)

df <-data.frame(year, sp1, sp2, sp3) 
d <- df %>% 
  pivot_longer(!year, names_to = "species", values_to = "cover") 

plot <-ggplot(d, aes(year, cover, color = species))  
  geom_line(size = 2)   coord_polar()   theme_classic()  
  theme(text = element_text(size = 18), 
        legend.text = element_text(face = "italic"), 
        #axis.text.x=element_blank(), # remove all x axis labels in polar_coord
        #axis.text.x=element_text(angle = 25), # rotates angle, but does not increase distance between labels. 
        legend.title=element_blank(), 
        legend.position="right")  
  scale_x_continuous(breaks=c(2019, 2018, 2017)) # this customizes the x-axis labels to be my 
            # exact observation dates. List order places 2019 at the 11:59 "clock" position, 
            # and 2017 at the 12:01 "clock" position)
plot

The issue that I see is that the 2017 and 2019 labels are so close it's visually hard to interpret these as the start and stop of the clock. Is there a way to increase the spacing between them? (see example pic below)

I've been trying to use theme(axis.text.x = ...) as commented above to customize, but so far it's not doing what I want. Any other suggestions? Thanks!

plot I want

CodePudding user response:

You can simply adjust the breaks and labels on the x axis. I think it also improves the appearance to have curved labels and perhaps some year dividers:

library(geomtextpath)

ggplot(d, aes(year, cover, color = species))  
  geom_rect(data = data.frame(y = c(2017, 2017   2/3, 2018   1/3)),
            aes(ymax = Inf, ymin = -Inf, xmin = y, xmax = y   2/3),
            inherit.aes = FALSE, alpha = 0.05, color = "gray75")  
  geom_line(size = 2)   
  coord_curvedpolar()   
  theme_classic()  
  scale_x_continuous(breaks = c(2017.33, 2018, 2018.67), labels = 2017:2019)  
  theme(text = element_text(size = 18), 
        legend.text = element_text(face = "italic"),
        legend.title=element_blank(), 
        legend.position="right",
        axis.line.x.bottom = element_blank()) 

enter image description here

CodePudding user response:

Perhaps easier with an annotation?

ggplot(d, aes(year, cover, color = species))  
  geom_line(size = 2)   
  annotate("text", size = 6,  label = year,
           x = c(2017.1, 2018, 2018.9),
           y = c(110, 90, 110))  
  coord_polar()   
  theme_classic()  
  theme(text = element_text(size = 18), 
        legend.text = element_text(face = "italic"), 
        #axis.text.x=element_blank(), # remove all x axis labels in polar_coord
        #axis.text.x=element_text(angle = 25), # rotates angle, but does not increase distance between labels. 
        legend.title=element_blank(), 
        legend.position="right")  
  scale_x_continuous(labels = NULL) 

enter image description here

  • Related