-2

I have a code like this

# two data frames
sf =  data.frame(days = c('05-06', '05,08'), Frequency = c(0, 2))
normal = data.frame(days = c('05-06', '05,07'), Frequency = c(1, 3))

# plot
ggplot() + 
  geom_bar(aes(y = Frequency, x = days), data = normal, 
           stat="identity", colour = 'black') +
  geom_bar(aes(y = Frequency, x = days), data = sf, 
           stat="identity", colour = 'red')

I want to add legend to it with red and black color, I tried this one which is the most related solution to my question, but it just shows the legend for the first barplot. How can I have a barplot legend with black and red color?

Notice: it is not one data frame, they are two separate data frames, with different number of rows, so I can not merge them together. So I plot two bar plots. The above code fill the plots, what I want is to add a legend that shows the first bar plot is black and the second one is red.

Z.Lin
  • 28,055
  • 6
  • 54
  • 94
Alex
  • 1,914
  • 6
  • 26
  • 47
  • please provide reproducible example. – Adam Quek Nov 01 '17 at 02:53
  • Can you provide a reproducible code example (most importantly a dataset)? I'm not 100% sure what you are trying to achieve. Do you want a red legend with a black border? – Michael Harper Nov 01 '17 at 02:57
  • No I just want to have a legend, that shows a red box and a label for the first bar and another black box for the second bar. – Alex Nov 01 '17 at 02:59
  • Possible duplicate of [Change bar plot colour in geom\_bar with ggplot2 in r](https://stackoverflow.com/questions/38788357/change-bar-plot-colour-in-geom-bar-with-ggplot2-in-r) – Michael Harper Nov 01 '17 at 03:04
  • No it is not a duplicate, here we have two geom_bar! – Alex Nov 01 '17 at 03:08
  • I still don't truly see what you are trying to do from the code. You may want to add a annotated photo of what you are trying to achieve. – Michael Harper Nov 01 '17 at 03:18
  • 1
    But I still don't think you need to have to separate geoms. You should ideally have the data in a single datagrams, and then have a column for each observation of `sf/normal`. This column can then be passed to the `aes(col =colname) ` within the geom_barplot. You can then refer to my duplicate post on how to change the colour – Michael Harper Nov 01 '17 at 03:22
  • See if my answer [here](https://stackoverflow.com/a/46821590/8449629) works for you? Use `scale_fill_manual` & define the fill inside each `geom_bar()`'s aesthetic mapping. – Z.Lin Nov 01 '17 at 03:42
  • Why do you need two gems and what `05,07` means? – pogibas Nov 01 '17 at 06:49
  • Related: https://stackoverflow.com/questions/35255234/combined-frequency-histogram-using-two-attributes – Jaap Nov 01 '17 at 12:39

1 Answers1

2

As with many ggplot questions, the problem you are facing stems from the data not being in the correct format. You should have a single dataframe, with the columns days, Frequency and group.

Set the colour within the aesthetic group of the ggplot, and then specify an manual colour scale to set the colours and black.

library(ggplot2)

df =  data.frame(days = c('05-06', '05,08', '05-06', '05,07'),
                 Frequency = c(0, 2, 1, 3),
                 group = c("sf", "sf", "normal", "normal"))


# plot
ggplot(df, aes(y = Frequency, x = days, fill = group)) + 
  geom_bar(stat="identity") +
  scale_fill_manual(values = c("red", "black"))

enter image description here

I would encourage reading the basics of ggplot reference guidance to get a bit more of an understanding about how to use the data: http://ggplot2.tidyverse.org/reference/. In addition, the ggplot cheatsheet is a useful reference: https://www.rstudio.com/wp-content/uploads/2015/03/ggplot2-cheatsheet.pdf

Michael Harper
  • 14,721
  • 2
  • 60
  • 84