简体   繁体   中英

Adding a legend to a layered ggplot

I made a ggplot for qPCR biological data, and was having a hard time adding the legend onto the geom_smooth plot. I would like to add the legend onto the image of the graph itself. I tried a bunch of the scale_color_ functions to no avail. If anyone has any tips, please let me know what you suggest.

Tried scale_color_continuous and scale_color_discrete


ggplot(plasmid_EphA2_8_2_19, aes(x = Sample, y = Cq)) + geom_boxplot()+ theme_classic()

primer_1 <- plasmid_EphA2_8_2_19 %>% filter(Sample == "Primer 1")
primer_2 <- plasmid_EphA2_8_2_19 %>% filter(Sample == "Primer 2")
primer_3 <- plasmid_EphA2_8_2_19 %>% filter(Sample == "Primer 3")
primer_4 <- plasmid_EphA2_8_2_19 %>% filter(Sample == "Primer 4")

primer_results <- ggplot() + geom_smooth(data = primer_1, mapping = aes(x = `Concentration (ng/mL)`, y = Cq), level = 0.05 , color='red') + geom_smooth(data = primer_2, mapping = aes(x = `Concentration (ng/mL)`, y = Cq), level = 0.05 , color ='blue') + geom_smooth(data = primer_3, mapping = aes(x = `Concentration (ng/mL)`, y = Cq), level = 0.05 , color ='darkgreen') + geom_smooth(data = primer_4, mapping = aes(x = `Concentration (ng/mL)`, y = Cq), level = 0.05 , color ='purple') + theme_classic()

scale:

Dark Red - primer 1 Blue - primer 2 Dark Green - primer 3 Purple - primer 4

I don't quite understand why you're dividing your data, but in principle doing

plasmid_EphA2_8_2_19 %>%
ggplot(aes(x= `Concentration (ng/mL)`, y = Cq, color = Sample))+
geom_smooth()

should create the output you want

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM