Jens
Jens

Reputation: 35

ggplot2 - separating barplot with two colors

I got data about frequencies each year. I barplotted these data (x - years; y - frequencies) with ggplot2. Now I want to separate the barplot diagram by coloring all bars below of 1980 with red and all bars equal or higher than 1980 with green. How can I do this - below is my MWE with trying to set these borders with scale_fill_manual which is not working because I don't do it correctly.

Data

dput(allePubs2)
structure(list(Jahr = 1922:2015, Publikationen = c(1L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 
0L, 0L, 0L, 1L, 0L, 1L, 0L, 0L, 2L, 0L, 0L, 0L, 1L, 0L, 1L, 4L, 
2L, 3L, 2L, 2L, 4L, 1L, 4L, 1L, 3L, 6L, 1L, 2L, 2L, 3L, 1L, 2L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L, 12L, 4L, 5L, 14L, 9L, 6L, 5L, 7L, 
5L, 6L, 4L, 7L, 3L, 7L, 6L, 7L, 8L, 6L, 4L, 14L)), .Names = c("Jahr", 
"Publikationen"), row.names = c(NA, -94L), class = "data.frame")

MWE attempt

cutoff80 <- 
  ggplot(data=allePubs2, aes(x=Jahr, y=Publikationen)) +
  geom_bar(stat="identity", width = 0.5)+
  scale_y_continuous(name="Anzahl Publikationen", breaks = 0:14, limits = c(0, 15))+
  scale_x_continuous(name="Jahr", breaks = c(1920, 1925, 1930, 1935, 1940, 1945, 1950, 1955, 1960, 1965, 1970, 1975, 
                                             1980, 1985, 1990, 1995, 2000, 2005, 2010, 2015), 
                     limits = c(1920, 2016))+
  ggtitle("Cut-Off bei 1980")+
  theme_hc() +
  geom_vline(xintercept = 1979.5)+ 
  scale_fill_manual(breaks = c("1922", "1979","1980","2016"), 
                  values=c("red", "red", "green", "green"))
cutoff80

Upvotes: 1

Views: 431

Answers (1)

Stibu
Stibu

Reputation: 15897

You can add a variable to your data frame that can be used to control the colour of the bars:

allePubs2$before1980 <- factor(allePubs2$Jahr < 1980,
                           levels = c(TRUE, FALSE),
                           labels = c("vor 1980", "nach 1980"))

Here, I have used factor to convert the logical variable to a factor. The labels will later be used in the legend, so it is important to chose them appropriately here.

And now you can map before1980 to colour much like you mapped Jahr to the x-coordinate:

library(ggplot2)
ggplot(data=allePubs2, aes(x=Jahr, y=Publikationen, fill = before1980)) +
  geom_bar(stat="identity", width = 0.5)+
  scale_y_continuous(name="Anzahl Publikationen", breaks = 0:14, limits = c(0, 15))+
  scale_x_continuous(name="Jahr", breaks = seq(1920, 2015, by = 5), 
                     limits = c(1920, 2016))+
  ggtitle("Cut-Off bei 1980")+
  geom_vline(xintercept = 1979.5)+ 
  scale_fill_manual(name = "", values = c("vor 1980" = "red", "nach 1980" = "green"))

enter image description here

I have also defined a manual scale that makes sure that the colours are the way you want them to have. Note how you can link a certain value to a colour as "vor 1980" = "red". The name argument can be used to set the title of the legend, and I have set it to empty, as you requested.

I have also simplified the breaks of scale_y_continuous() using seq and removed theme_hc() because I don't know where that function comes from.

Upvotes: 1

Related Questions