0

I am trying to change the colour bars using ggplot2 on a bar graph with frequency data. However, my plot is still appearing in greyscale.

Data (400 observations that take values between 1 to 5) and code:

library(ggplot2)
library(RColorBrewer)

# Sample data
complete_data <- data.frame(education = sample(1:5, 400, replace = TRUE)) 

ggplot(complete_data, aes(x=education)) + 
  geom_bar(stat="count") + 
  scale_x_discrete(labels=c("1" = "Level 1", "2" = "Level 2","3" = "Level 3","4" = "Level 4")) +
  geom_point(aes(color = education)) +
  scale_fill_brewer(palette = "PuBu")

I'm getting the following error:

Error in check_required_aesthetics(): ! geom_point requires the following missing aesthetics: y Run rlang::last_error() to see where the error occurred.

What am I doing wrong? I also want to make the background white but that doesn't seem to work either

Andrea M
  • 2,314
  • 1
  • 9
  • 27
Rnovice
  • 59
  • 6
  • 3
    It's easier to help you if you provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input that can be used to test and verify possible solutions. Are you trying to plot both bars *and* points? Note that the color= scale and fill= scale are two different scales. – MrFlick Aug 01 '22 at 14:22
  • Only bars. I just want the frequency of each level of education (1-5). Edited the question to add a sample of data. – Rnovice Aug 01 '22 at 14:34
  • 1
    as @MrFlick said, data to reproduce the problem would help allot. General tips: ```geom_point``` is used to make scatterplots, so it will need a x and y coordinate. If you want to make the background white, consider using ```theme_bw()``` between your ggplot() and geom_bar(). – Omniswitcher Aug 01 '22 at 14:36
  • 1
    You should **never** use `attach()`. It creates problems. Like here, when you do `edu <- as.factor(edu)`, that modifies an object named `edu`, but if you look at the class of the column in your `data`, `class(data$edu)`, it hasn't changed. ggplot is looking in your data, so it doesn't know about the `factor` at all. – Gregor Thomas Aug 01 '22 at 14:46

1 Answers1

1

First of all, you can remove the geom_point because that is used for scatterplots. You can use the scale_fill_brewer if you use fill in your aesthetics of your ggplot. You change the background with theme and panel.background like this:

complete_data <- read.table(text = "      education
         1
         2
         1
         3
         3
         4", header = TRUE)

library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 4.1.2
library(RColorBrewer)
#> Warning: package 'RColorBrewer' was built under R version 4.1.2

complete_data$education <- as.character(complete_data$education)

ggplot(complete_data, aes(x=education, fill = education)) + 
  geom_bar() + 
  scale_x_discrete(labels=c("1" = "Level 1", "2" = "Level 2","3" = "Level 3","4" = "Level 4")) +
  scale_fill_brewer(palette = "PuBu") +
  theme(panel.background = element_rect(fill = "white", colour = "white"))

Created on 2022-08-01 by the reprex package (v2.0.1)

Quinten
  • 35,235
  • 5
  • 20
  • 53