Home > Mobile >  Remove some of the X axis labels in ggplot bar chart
Remove some of the X axis labels in ggplot bar chart

Time:08-20

I have the following code for a stacked bar graph for the period 1970-2020, which is the X-axis label. The graph is generated from a dataframe with 3 columns and 51 rows representing various years.

Year     Active     New
1970     1          1
......
2020     268        60

enter image description here

df %>%
    mutate(Old = Active - New) %>%
    select(-Active) %>%
    pivot_longer(-Year, names_to = "Type", values_to = "Count") %>%
    ggplot()  
    geom_col(aes(x = Year, y = Count, fill = forcats::fct_rev(Type))) 
    ggtitle("example graph") 
    scale_fill_discrete(name="Cases",
                        breaks=c("Old", "New"),
                        labels=c("Ongoing", "New"))

As my X-axis is not readable, I would like to remove most of the Years in the X-axis, and keep eg. 1970, 1980, 1990, 2000, 2020 - at their corresponding positions. I'm not sure that scale_x_discrete can do this?

CodePudding user response:

The obvious answer in this specific case is to convert Year to a numeric variable, which will make the breaks pretty by default.

This sample data allows us to run your code and reproduce your issue:

set.seed(1)

df <- data.frame(Year = factor(1970:2020),
                 Active = cumsum(rnorm(51, 4, 2)),
                 New = cumsum(rnorm(51, 1, 1)))

Using your exact plotting code produces a similar plot with unreadable axis labels:

df %>%
    mutate(Old = Active - New) %>%
    select(-Active) %>%
    pivot_longer(-Year, names_to = "Type", values_to = "Count") %>%
    ggplot()  
    geom_col(aes(x = Year, y = Count, fill = forcats::fct_rev(Type))) 
    ggtitle("example graph") 
    scale_fill_discrete(name="Cases",
                        breaks=c("Old", "New"),
                        labels=c("Ongoing", "New"))

But if we simply convert Year to numeric values, we get the same plot with pretty breaks:

df %>%
    mutate(Old = Active - New) %>%
    select(-Active) %>%
    pivot_longer(-Year, names_to = "Type", values_to = "Count") %>%
    ggplot()  
    geom_col(aes(x = as.numeric(as.character(Year)), y = Count, 
                 fill = forcats::fct_rev(Type))) 
    ggtitle("example graph") 
    xlab("Year")  
    scale_fill_discrete(name="Cases",
                        breaks=c("Old", "New"),
                        labels=c("Ongoing", "New"))

enter image description here

If for whatever reason it needs to be a factor, you can leave specific years blank using a labeling function.

df %>%
    mutate(Old = Active - New) %>%
    select(-Active) %>%
    pivot_longer(-Year, names_to = "Type", values_to = "Count") %>%
    ggplot()  
    geom_col(aes(x = Year, y = Count, fill = forcats::fct_rev(Type))) 
    ggtitle("example graph") 
    scale_fill_discrete(name="Cases",
                        breaks=c("Old", "New"),
                        labels=c("Ongoing", "New"))  
    scale_x_discrete(labels = function(x) ifelse(as.numeric(x) %% 10, "", x))  
    theme(axis.ticks.length.x = unit(0, "mm"))

enter image description here

Created on 2022-08-19 with reprex v2.0.2

  • Related