Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
lfaller opened this issue Apr 19, 2016 · 6 comments
Closed

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

lfaller opened this issue Apr 19, 2016 · 6 comments

Comments

@lfaller
Copy link

lfaller commented Apr 19, 2016

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

@cderv
Copy link
Contributor

cderv commented Apr 19, 2016

I think there are no problems with the package.

df$b is integer in your example. So x-axis should be continuous not discrete. Then I think you want to control the breaks not the labels. Note that you could get each integer with unique

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_continuous(breaks = unique(df$b)) +
    theme(axis.text.x = element_text(angle = 90, hjust = 1))

rplot

@lfaller
Copy link
Author

lfaller commented Apr 19, 2016

Ah, this makes sense -- thanks for the hints!

I have some more dataframes I want to plot similarly where b is not an integer but a factor.

I figured out that I can plot it using aes(x = as.numeric(b) ... as in this code:

library(ggplot2)
set.seed(11)
df <- data.frame(a = rlnorm(30), b = as.factor(1:10), c = rep(LETTERS[1:3], each = 10))
ggplot(df, aes(x = as.numeric(b), y = a, fill = c)) + 
    geom_area(position = 'stack')

Since I am now dealing with factors, I assume I need to go back to scale_x_discrete but using scale_x_discrete(labels = levels(df$b)) to get the labels on the x-axis didn't work.

If anyone else has any suggestions, I'd appreciate it!

Best,
~Lina

@cderv
Copy link
Contributor

cderv commented Apr 19, 2016

It is not a good idea to as.numeric with factor as explained in the help page ?factor.

And since you map on x a numeric variable, as.numeric(b), then you scale on x is still continuous, not discrete so it is the same : use scale_x_continous and breaks argument.
However if you map a factor variable on x, it should work.

By the way, I think you should post your questions on website like Stackoverflow rather than on the issues of a package. It is not the right place to get help as it is dedicated to report issues and feature request.
However, I tried to give you some new hints, hope it is clearer.

@lfaller
Copy link
Author

lfaller commented Apr 20, 2016

Thanks for the help, I appreciate it!

@thomasp85
Copy link
Member

Related to #1589

@hadley
Copy link
Member

hadley commented Jul 28, 2016

Closing as duplicate

@hadley hadley closed this as completed Jul 28, 2016
@lock lock bot locked as resolved and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants