Skip to content

How to customize x-axis labels for a stacked bar plot graph? #1617

@lfaller

Description

@lfaller

Hi all,

I am trying to create a stacked area plot and label the x axis with the variables using scale_x_discrete.

However, as soon as I add scale_x_discrete to my code, the x axis gets wiped out:

screen shot 2016-04-19 at 12 01 55 pm

My code looks as follows:

library(ggplot2)

set.seed(11)
df <- data.frame(a = rlnorm(30), b = 1:10, c = rep(LETTERS[1:3], each = 10))

ggplot(df, aes(x = b, y = a, fill = c)) + 
  geom_area(position = 'stack') +
  scale_x_discrete(labels = levels(as.factor(df$b))) +
  theme(axis.text.x = element_text(angle = 90, hjust = 1))

Has this been a recent change? I have definitely been able to do this before and it didn't seem tricky, but I recently upgraded packages, so I'm not sure if that's what happened. Is there another function I can use to add x-axis labels to a stacked area plot?

Thanks for any advice you might have!
~Lina

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions