1

I am trying to combine two different bar graphs into one using GGPLOT. Seperately the code is `

ggplot(data=by_div_salaries, aes(x=women_salary, y=salaries.ClassificationName)) + 
  geom_bar(stat="identity") +labs(title = "Women Head Coach Salaries")

ggplot(data=by_div_salaries, aes(x=men_salary, y=salaries.ClassificationName)) + 
  geom_bar(stat="identity") +labs(title = "Men Head Coach Salaries")

and I am trying to get the mens salary next to the womens salary by having two bars for each ClassificationName.

I tried this:

ggplot(data=by_div_salaries, aes()) + 
  geom_bar(mapping = aes(x=women_salary, y=salaries.ClassificationName), position=position_nudge(x = 0.2), width=0.2,stat = "identity",color="green" ) +
  geom_bar(mapping = aes(x=men_salary, y=salaries.ClassificationName), position=position_nudge(x = -0.2), width=.2, stat = "identity", color = "blue") + 
  scale_y_continuous(name = "Division") +
  xlab("Salary")

But am getting this error: Error: Discrete value supplied to continuous scale


I want the two grpahs next to each other.
1
  • Try with scale_y_discrete instead of scale_y_continuous. My guess is that salaries.ClassificationName is a catagorical column. Commented Nov 9, 2022 at 6:05

1 Answer 1

1

This sounds like a situation where reshaped ("longer") data would probably be easier to work with.

library(tidyverse)
by_div_salaries %>%
  pivot_longer(c(women_salary, men_salary)) %>%
  ggplot(aes(x=value, y=salaries.ClassificationName, fill = name)) + 
  geom_col() 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.