Darren
Darren

Reputation: 25

How do I remove specific tick labels in ggplot2?

I have the following bubble plot that shows the abundance percentage of microbes across different samples. However, I want to remove the tick labels called "Archaea" and "Other taxa" (located at either ends of the bubble plot) since the labels for both can be placed in the x-axis strip text instead. I used the following code to produce the plot:

ggplot(En.TaxMisc.NoC.RelAb.filtered.tidy$CombinedMisc, 
       aes(x = factor(Taxonomy, levels = En.TaxMisc.order$Taxonomy), 
           y = SampleSource, size = RelAb)) + 
  geom_point(colour = '#abd9e9') + 
  facet_grid(SampleType ~ Level, 
             labeller = labeller(SampleType = SampleType.NoC.labels),
             scale = 'free', space = 'free') +
  scale_x_discrete(name = NULL) +
  scale_y_discrete(position = 'left', name = NULL) +
  scale_size_continuous(name = str_wrap('Relative abundances (%)', width = 10),                                        
                        breaks = c(1:8), range = c(0.75, 20)) +
  guides(size = guide_legend(nrow = 1)) + 
  theme(legend.position = 'bottom', 
        legend.background = element_rect(colour = 'grey70'),
        legend.title = element_text(size = 8, hjust = 1),
        legend.text = element_text(size = 7, hjust = 0),
        legend.spacing.x = unit(2.5, 'mm'),
        legend.box = 'horizontal',
        strip.background = element_rect(colour = 'grey55'),
        strip.text.x = element_text(size = 8),
        strip.text.y = element_text(size = 8), 
        axis.text.x.bottom = element_text(angle = 90, hjust = 1, 
                                          vjust = 0.3, size = 8),
        axis.text.y.left = element_text(size = 8),
        axis.ticks = element_blank(),
        panel.grid.major.x = element_line(linetype = 1),
        panel.border = element_rect(linetype = 1, fill = NA),
        panel.background = element_blank())

I had tried to use scale_x_discrete(labels = c("Archaea" = NULL, "Other taxa" = NULL) but this resulted in all the tick labels being removed. I had also looked into using the rremove() function and the axis_ticks theme components, but neither appear to possess arguments for specifying tick labels.

I'd appreciate suggestions or advice anyone can give me!

Upvotes: 1

Views: 3307

Answers (1)

smarchese
smarchese

Reputation: 530

There's a fair bit of extraneous detail in the question, but if you're just looking to remove (or customize!) tick labels, all you need is to add a labels argument to scale_x_discrete.

Self-contained example:

library(ggplot2)
ds = data.frame(
  xVar = as.factor(rep(LETTERS[1:5],10)),
  y = rnorm(50)
)
my_custom_labels = c("","level B","level C","level D!","")

ggplot(data = ds) +
  geom_point(aes(x = xVar,y = y)) +
  scale_x_discrete(labels = my_custom_labels)

Upvotes: 3

Related Questions