Adding a Legend to an Overlay Bar and Line Plot!

I am trying to add a legend to my graph. I made a bar plot with an overlay line plot. I was under the impression that if you put col, fill, ect. in aes(), however, I'ed tried this many different times and many different ways, and it does not work for me.
If someone could help me, I would be so grateful!
My code is as follows:

load("H:\\Personal\\anti.csv")
d=read.csv("anti.csv", header=T)
library(ggplot2)
Day <- d$Ordering
Tests<- d$Amount
Positive<- d$Positive
Rate<- d$Rate
now = data.frame(type=c("Positive", "Tests"))
now
coeff<- 0.33
ggplot(d, aes(x=Day, legend)) +
geom_bar(aes(y = Tests), stat="identity", size = 1, color = "#0072B5FF", fill="#6F99ADFF") +
geom_line(aes(y=Rate*coeff), size = 1.2, color="#E18727FF", group=1) + 
scale_y_continuous(sec.axis = sec_axis(~.*3, name = "Rate of Positive Tests")) +
labs(title="Tests", x="Day", y="Tests Ordered") +  
theme_minimal() + 
theme(axis.title.y=element_text(color="#6F99ADFF"), 
axis.title.y.right=element_text(color="#E18727FF")) + 
theme(plot.title=element_text(hjust=0.5))

Welcome! I'm afraid you'll need to supply some more info in order for helpers to be able to understand your problem.

The best thing would be if you can make your question into a reproducible example (follow the link for instructions and explanations). To include your data, you'll want to follow one of the methods discussed here.

If you try all that and get stuck, here's a fallback option...
  1. Edit your post and add in some of the code you have tried. It's OK it doesn't work! It's really helpful to see what you've been attempting. Be sure to format your code as code (it's hard to read unformatted code, and it can get garbled by the forum software)
  2. Include sample data:
    • If your data set is OK to share, run the following line and paste the output into your post. Again, be sure to format it as code :sparkles:
    dput(head(your_dataframe_name, 10))
    
    • If your data set can't be shared, run this line instead and paste the output into your post (and yes, format as code!) This will still share some information about your data. If it's truly confidential, I'm afraid you'll need to make a fake sample dataset to share.
    str(your_dataframe_name)
    

I cannot share my dataset, but basically it is as follows:
Day .......1 .. 2....3
Amount 25 .15...30
Rate ....50...20....66

and so on.... I don't know if this helps, but it's pretty simple

What do you want the legend to indicate? In general, you need to add fill or color aesthetics inside aes() to create a legend.

library(tidyverse)

df <- tibble(
  Day = c("1", "2", "3"),
  Tests = c(25, 15, 30),
  Rate = c(50, 20, 66)
)

df %>% 
  ggplot() +
  geom_col(aes(x = Day, y = Tests, fill = Day)) +
  geom_line(aes(x = Day, y = Rate * 0.33), group = 1) +
  scale_y_continuous(sec.axis = sec_axis(~.*3, name = "Rate of Positive Tests"))

Created on 2020-06-10 by the reprex package (v0.3.0)

Sorry, I should have clarified. I have all of the bars the same color and the line is a different color and I want a legend to represent that. So say, orange for the line and blue for the bars.

I think this is what you are looking for? Because in general ggplot2 doesn't really encourage secondary axes, I'm not sure if there is a cleaner way to do it.

library(tidyverse)

df <- tibble(
  Day = c("1", "2", "3"),
  Tests = c(25, 15, 30),
  Rate = c(50, 20, 66)
)

df %>% 
  ggplot() +
  geom_col(aes(x = Day, y = Tests, fill = "Bars")) +
  geom_line(aes(x = Day, y = Rate * 0.33, color = "Line"), group = 1) +
  scale_y_continuous(sec.axis = sec_axis(~.*3, name = "Rate of Positive Tests")) +
  scale_fill_manual(name = NULL, values = c("Bars" = "blue")) +
  scale_color_manual(name = NULL, values = c("Line" = "orange"))

Created on 2020-06-10 by the reprex package (v0.3.0)

Thank you!! Just what I needed!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.