1

Need to plot a graph bar

Output error : Mapping should be created with aes() oraes_()`.

Code:

cbPalette <- c("#CC79A7", "#D55E00", "#56B4E9", "#F0E442", "#009E73", "#0072B2", "#999999", "#E69F00")

mydata %>%
    group_by(workclass) %>%
    summarise(mean = mean(education.num, na.rm = TRUE)) %>% 
    ggplot(new_data,aes(workclass, education.num, fill = workclass)) +
    geom_bar(stat = "identity") +
    labs(title = "Average Education Num vs workclass",
       x = "Workclass",
       y = "Average Education Num") +
    theme(axis.text.x = element_text(size = 10, angle = 90, hjust = 1))+
    scale_fill_manual(values = cbPalette[1]) +
    theme(axis.text = element_text(size = 10),
        axis.title = element_text(size = 10), 
        legend.title = element_text(size = 10)) +
    scale_fill_manual(values = alpha(cbPalette)) 

Any suggestions

Expected Output:

enter image description here

1
  • Try looking at your data after your call to summarise. Commented Mar 24, 2020 at 23:33

1 Answer 1

3

You have several issues in your code.

1) When you use summarise, your column education.num will be replaced by mean in your dataset as you can see below:

library(dplyr)

mydata %>%
  group_by(workclass) %>%
  summarise(mean = mean(education.num, na.rm = TRUE))

# A tibble: 9 x 2
  workclass         mean
  <chr>            <dbl>
1 ?                 9.26
2 Federal-gov      11.0 
3 Local-gov        11.0 
4 Never-worked      7.43
5 Private           9.88
6 Self-emp-inc     11.1 
7 Self-emp-not-inc 10.2 
8 State-gov        11.4 
9 Without-pay       9.07

2) then, in your ggplot, you are calling an another dataframe new_data and try to reuse education.num instead of mean. You can correct it by doing:

library(dplyr)
library(ggplot2)

mydata %>%
  group_by(workclass) %>%
  summarise(mean = mean(education.num, na.rm = TRUE)) %>% 
  ggplot(aes(workclass, mean, fill = workclass)) +
  geom_bar(stat = "identity") +
  labs(title = "Average Education Num vs workclass",
       x = "Workclass",
       y = "Average Education Num") +
  theme(axis.text.x = element_text(size = 10, angle = 90, hjust = 1),
        axis.text = element_text(size = 10),
        axis.title = element_text(size = 10), 
        legend.title = element_text(size = 10)) 

enter image description here

3) Finally, you are trying to replace filling values by cbPalette, however, you are providing only 8 values whereas you have 9 different classes, so you need to either add a new color and remove ? like that:

library(dplyr)
library(ggplot2)

mydata %>%
  group_by(workclass) %>%
  summarise(mean = mean(education.num, na.rm = TRUE)) %>% 
  filter(workclass != "?") %>%
  ggplot(aes(workclass, mean, fill = workclass)) +
  geom_bar(stat = "identity") +
  labs(title = "Average Education Num vs workclass",
       x = "Workclass",
       y = "Average Education Num") +
  theme(axis.text.x = element_text(size = 10, angle = 90, hjust = 1),
        axis.text = element_text(size = 10),
        axis.title = element_text(size = 10), 
        legend.title = element_text(size = 10)) +
  scale_fill_manual(values = cbPalette) 

enter image description here

Does it answer your question ?

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.