2

How can I remove the first y axis label, here $0 but keep the default labels otherwise ?

library(ggplot2)
library(scales)
  
y <- data.frame(
  group = c("GROUP A", "GROUP A", "GROUP A", "GROUP B", "GROUP B", "GROUP B"),
  individual = c("Individual 1", "Individual 2", "Individual 3", 
                 "Individual 1", "Individual 2", "Individual 3"),
  value = c(2000, 9000, 45000, 3500, 11500, 38000)
)
  
ggplot(data = y, aes(x = group, y = value)) +
  geom_bar(stat = "identity") +
  scale_y_continuous(labels = scales::dollar_format(accuracy = 100L)) +
  facet_grid(rows = vars(individual), scales = "free")

enter image description here

qfazille
  • 1,643
  • 13
  • 27

1 Answers1

1

You could make use of a custom breaks function like so:

EDIT: And thanks to @LMc if you want to stick with the default breaks you could replace breaks_pretty with breaks_extended following this post.

library(ggplot2)
library(scales)

y <- data.frame(
  group = c("GROUP A", "GROUP A", "GROUP A", "GROUP B", "GROUP B", "GROUP B"),
  individual = c("Individual 1", "Individual 2", "Individual 3", 
                 "Individual 1", "Individual 2", "Individual 3"),
  value = c(2000, 9000, 45000, 3500, 11500, 38000)
)

mybreaks <- function(x) {
  x <- breaks_pretty()(x)
  x[x<=0] <- NA
  x
}

ggplot(data = y, aes(x = group, y = value)) +
  geom_bar(stat = "identity") +
  scale_y_continuous(breaks = mybreaks, labels = scales::dollar_format(accuracy = 100L)) +
  facet_grid(rows = vars(individual), scales = "free")

stefan
  • 90,330
  • 6
  • 25
  • 51
  • 1
    Good idea -- do you have a flexible idea for if you also wanted to remove the corresponding tick mark as well? – LMc Feb 19 '21 at 16:29
  • I was afraid you ask for that (:. In that case make use of a custom breaks function. See my edit. Unfortunately I had to change the breaks function to `breaks_pretty` which gives different breaks than the default. I will have a look, maybe I can come up with an edit to fix that. – stefan Feb 19 '21 at 16:41
  • Brilliant ! I've tried also with my own break function but I was not aware of `breaks_pretty()` to rebuilt default ones. Thanks ! – qfazille Feb 19 '21 at 16:44
  • 1
    I had to make you work for your "+1" :). It looks like this [SO](https://stackoverflow.com/questions/38486102/how-does-ggplot-calculate-its-default-breaks/41454471) question might help in conjunction with your answer if @qfazille needs to use the default breaks. – LMc Feb 19 '21 at 16:51
  • Thanks @LMc. That saved my a lot time for searching. I already started to dive into the ggplot2 source code . :D – stefan Feb 19 '21 at 16:54
  • both solutions are good to me. Thanks both – qfazille Feb 20 '21 at 12:45