1

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
Sandy Muspratt
  • 31,719
  • 12
  • 116
  • 122
Jens
  • 35
  • 1
  • 6
  • Welcome to Stack Overflow! Your question does not contain a [reproducible example](http://stackoverflow.com/q/5963269/4303162). It is therefore hard to understand your problem and give you an appropriate answer. Please make your data available (e.g. by using `dput()`) or use one of the example data sets in R. Also, add the minimal code required to reproduce your problem to your post. – Stibu Mar 06 '16 at 11:57
  • Thanks for feedback. I added data and MWE. Hopefully its helpful and correct? – Jens Mar 06 '16 at 13:18
  • Thanks for adding the data and your code! That is exactly what is needed in order to make your question answerable. One small complaint remains: it is useful to make your example minimal in the sense that you leave out anything that is not relevant to the problem at hand. I think you should have left out `theme_hc()` because it seems not to be a standard function and has no connection to your problem. But all in all you did a good job improving your question. – Stibu Mar 06 '16 at 13:29
  • Thanks for your helpful feedback! – Jens Mar 06 '16 at 13:46

1 Answers1

1

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.

Stibu
  • 15,166
  • 6
  • 57
  • 71
  • Very intelligent solution! I really like it. One last thing: How can I change legend in following way: remove "before 1980" above, set "before 1980" after red mark and "after 1980" after green mark? – Jens Mar 06 '16 at 13:51