0

I have a very simple question. I need to add a legend and change the colour of the bars in a stacked barchart.

I managed to split my barchart into three different colour groups according to the year my data come from, but the colours are automatically set by R while I need specific ones (as seen in my vector "colours"). Plus, I'm not able to set the legend (again, I need it to say that each shade of grey is related to a different year: #C7C7C7 for year 2020, #8A8A8A for 2021 and #585858 for 2022).

Can you help me?

This is my dataset:

dput(broodstog)
structure(list(`Nest-box.number.(x.brood)` = structure(1:47, .Label = c(" 5 (a)", 
"5 (b)", "7", "10", "11 (a)", "12", "27", "31", "37 (a)", "40", 
"41", "200", "202", "205", "1 west", "5 west", "A3", "B7", "B8", 
"B11", "C18", "C21", "3 west", "7 west", "43", "B3", "104", ".10.", 
"A1", "33", "C20", "C16", ".205.", ".B7.", "B9", "22", "203", 
"..10..", "14", ".1 west.", "4 west", "C31", "81", "82", "52", 
"C42", ".12."), class = "factor"), `N.(Carnus)` = c(4, 3, 13, 
36, 10, 8, 8, 17, 6, 3, 13, 8, 8, 9, 5, 10, 22, 28, 8, 8, 1, 
3, 1, 5, 1, 1, 2, 4, 1, 1, 1, 1, 6, 1, 1, 1, 1, 4, 1, 1, 2, 1, 
1, 1, 1, 3, 1), Year = c(2020, 2020, 2020, 2020, 2020, 2020, 
2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 
2020, 2020, 2020, 2020, 2020, 2021, 2021, 2021, 2021, 2021, 2021, 
2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2022, 2022, 
2022, 2022, 2022, 2022, 2022, 2022, 2022, 2022)), row.names = c(NA, 
47L), class = "data.frame")

This is my code so far:

broodstog$"Nest-box.number.(x.brood)" <- as.character(broodstog$"Nest-box.number.(x.brood)")
broodstog$"Nest-box.number.(x.brood)" <- factor(broodstog$"Nest-box.number.(x.brood)", levels=unique(broodstog$"Nest-box.number.(x.brood)"))
colours <- c("2020"="#C7C7C7","2021"="#8A8A8A","2022"="#585858")
ggplot(broodstog, aes(x=broodstog$"Nest-box.number.(x.brood)",y=broodstog$"N.(Carnus)")) + geom_col() +
geom_bar(fill=broodstog$"Year", stat="identity") +
scale_fill_manual(" ", values = c("2020" = "#C7C7C7", "2021" = "#8A8A8A", "2022"="#585858")) +
xlab("Nest box") + ylab("Collected Carnus hemapterus (N)") +
ggtitle("PARASITIZED BROODS") +
theme(axis.line.x = element_line(colour = 'black', size=0.5, linetype='solid'),
    axis.line.y = element_line(colour = 'black', size=0.5, linetype='solid')) +
theme(title = element_text(face="plain", size = 12)) +
theme(plot.title = element_text(hjust = 0.5, vjust =2)) +
theme(axis.text.x = element_text(angle = 45, hjust=1)) +
 theme(panel.background = element_blank())

Thanks :)

Quinten
  • 35,235
  • 5
  • 20
  • 53
Devon
  • 17
  • 3

1 Answers1

0

Some notes:

  • It is not recommended to use $ to assign columns in aes of ggplot
  • Removed the geom_col or geom_bar
  • Made fill column to factor
  • Make sure to assign fill in aes if you want to have it as legend.

Here is some reproducible code:

broodstog$"Nest-box.number.(x.brood)" <- as.character(broodstog$"Nest-box.number.(x.brood)")
broodstog$"Nest-box.number.(x.brood)" <- factor(broodstog$"Nest-box.number.(x.brood)", levels=unique(broodstog$"Nest-box.number.(x.brood)"))
colours <- c("2020"="#C7C7C7","2021"="#8A8A8A","2022"="#585858")

library(ggplot2)
ggplot(broodstog, aes(x=`Nest-box.number.(x.brood)`, y=`N.(Carnus)`, fill = factor(Year))) + 
  geom_bar(stat="identity") +
  scale_fill_manual(" ", values = c("2020" = "#C7C7C7", "2021" = "#8A8A8A", "2022"="#585858")) +
  xlab("Nest box") + ylab("Collected Carnus hemapterus (N)") +
  ggtitle("PARASITIZED BROODS") +
  theme(axis.line.x = element_line(colour = 'black', size=0.5, linetype='solid'),
        axis.line.y = element_line(colour = 'black', size=0.5, linetype='solid')) +
  theme(title = element_text(face="plain", size = 12)) +
  theme(plot.title = element_text(hjust = 0.5, vjust =2)) +
  theme(axis.text.x = element_text(angle = 45, hjust=1)) +
  theme(panel.background = element_blank())

Created on 2022-12-23 with reprex v2.0.2

Quinten
  • 35,235
  • 5
  • 20
  • 53