Home > Mobile >  Hide a specific y axis label in ggplot in R
Hide a specific y axis label in ggplot in R

Time:07-22

I have a plot in R and I would like to hide a specific label without manipulating the data used to produce the plot. Is this possible? I would like to hide the y label denoted as 5000. I tried using the element blank in my code but it did not work as desired.

My code is as follows:

library(ggplot2)
ggplot(diamonds) 

diamonds %>% 
   ggplot(aes(table))  
   geom_histogram()  
   labs(
    x = "scores",
    y = "Frequency")  
    (themes = list(theme(axis.text.y = element_blank())))

CodePudding user response:

Not sure exactly what you're going for, but scale_y_continuous gives you some control.

diamonds %>% 
       ggplot(aes(table))  
       geom_histogram()  
       labs(
             x = "scores",
             y = "Frequency")   
  scale_y_continuous(labels = c(10000, 15000), breaks=c(10000, 15000))

CodePudding user response:

Based on your comment "I would like to hide a specific x-axis label on my plot without manipulating the data", here is an example.

ggplot(iris, aes(Species, Sepal.Length))   
    geom_boxplot()   
    scale_x_discrete(labels = c("setosa", "", "virginica"))

enter image description here

This will hide the x-axis label for the second entry. Mind you, the order of labels is (by default) given by lexicographical ordering. So you will need to know the ordering first, before you can hide a specific label.

  • Related