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 Runrlang::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
CodePudding user response:
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)