Unable to add appropriate legend to ggplot2

Good afternoon everyone.

I have an issue with producing an appropriate legend in my ggplot2 boxplot. Unfortunately, after adding scale_linetype_manual('Legend',values='solid')+ scale_shape_manual('',values = 18)+ theme(legend.spacing.y = unit(0.01, "cm"))
to my line codes, it does not produce the legend at all. I have been trying to add legend in many different ways, but none of them seemed to work.
This is the entire code to produce the following graphs

PhospholipaseA2NOPo1<-structure(list(Drug = structure(c(1L, 1L, 5L, 5L, 5L, 5L, 5L, 
                                       5L, 4L, 4L, 4L, 4L, 4L, 4L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 
                                       2L, 2L, 2L), .Label = c("Blank", "SP.N", "SP.V", "V.N", "V.V"
                                       ), class = "factor"), Absorbance = c(0.178155098, 0.281888047, 
                                                                            0.315468607, 0.395427286, 0.443773761, 0.274327232, 0.126408569, 
                                                                            0.160375886, 0.332019768, 0.328779358, 0.31918001, 0.202597307, 
                                                                            0.241052828, 0.332138474, 0.356656925, 0.329787758, 0.291912398, 
                                                                            0.264823437, 0.167194165, 0.176451481, 0.406376062, 0.28900749, 
                                                                            0.298281464, 0.181890255, 0.245723697, 0.350279692)), class = "data.frame", row.names = c(NA, 
                                                                                                                                                                      -26L))

library(tidyverse)
library(ggplot2)
library(ggpubr)
library(RColorBrewer)
ggplot(PhospholipaseA2NOPo1, aes( x = Drug, y = Absorbance))+
  labs(x = expression(paste("Treatment")))+
  ylab(expression(paste("Absrobance " ~ "(",   A[425], " - ", A[600] ~ nm, ")"))) +
  geom_line()+
  labs(title = "Comparison of the effect of Blank, Vehicle - Vehicle, Vehicle - Nicotinic Acid, SP - Vehicle, and SP - Nicotinic Acid on microglia ",
       caption = "Phospholipase A2 Assay")+
  geom_point() +
  scale_x_discrete(labels = c("Blank", "Vehicle- Vehicle", "Vehicle - Nicotinic Acid", "SP - Vehicle", "SP - Nicotinic Acid"))+
  stat_compare_means(label.y = 0.80,
                     label.x = 4.25)+
  geom_boxplot(
               fill=c("#B2182B", "#D6604D", "#F4A582", "#FDDBC7", "#D1E5F0"),            
               width=0.2,notch = FALSE)+
  stat_summary(fun=mean, colour="black", geom="point", 
               shape=18, size=3) + 
  geom_signif(comparisons = split(t(combn(levels(PhospholipaseA2NOPo1$Drug), 2)), seq(nrow(t(combn(levels(PhospholipaseA2NOPo1$Drug), 2))))), 
              map_signif_level = TRUE, colour = "#cf1b1b", step_increase = .1)+
  theme_bw() +
  theme(panel.grid.major = element_line(colour = "#d3d3d3"),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(), panel.background = element_blank(),
        plot.title = element_text(size = 14, face = "bold"))+
  theme(axis.text.x = element_text(face="bold", color="#3142b0", 
                                   size=8.9, angle=0),
        axis.text.y = element_text(face="bold", color="#3142b0", 
                                   size=8.9, angle=0))+
  theme(
    plot.title = element_text(size = 12, face = "bold"),
    plot.caption = element_text(hjust = 0, size = 8, face = "italic")
  )+
   scale_linetype_manual('Legend',values='solid')+
  scale_shape_manual('',values = 18)+
  theme(legend.spacing.y = unit(0.01, "cm"))

And this is the DataUsed

Thank you so much for any help!

Its not clear what the legend you propose would help anyone decode.
all linetypes are a single solid line, therefore no legend key will help distinguish one from another,
similarly all shapes are the common shape 18, hence no legend key will help distinguish one from the other.
Apologies if I've misunderstood something.

That aside, the typical way to get working legends in ggplot, is to have relevant aesthetics mapped.
Here I went ahead and added the arbitrary mappings (that I don't think are warranted and wouldn't choose for my own chart).


ggplot(PhospholipaseA2NOPo1, aes( x = Drug, y = Absorbance,linetype="solid lines everywhere"))+
  labs(x = expression(paste("Treatment")))+
  ylab(expression(paste("Absrobance " ~ "(",   A[425], " - ", A[600] ~ nm, ")"))) +
  geom_line()+
  labs(title = "Comparison of the effect of Blank, Vehicle - Vehicle, Vehicle - Nicotinic Acid, SP - Vehicle, and SP - Nicotinic Acid on microglia ",
       caption = "Phospholipase A2 Assay")+
  geom_point() +
  scale_x_discrete(labels = c("Blank", "Vehicle- Vehicle", "Vehicle - Nicotinic Acid", "SP - Vehicle", "SP - Nicotinic Acid"))+
  stat_compare_means(label.y = 0.80,
                     label.x = 4.25)+
  geom_boxplot(
    fill=c("#B2182B", "#D6604D", "#F4A582", "#FDDBC7", "#D1E5F0"),            
    width=0.2,notch = FALSE)+
  stat_summary(fun=mean, colour="black", geom="point", size=3,mapping=aes(shape="shape 18")) + 
  geom_signif(comparisons = split(t(combn(levels(PhospholipaseA2NOPo1$Drug), 2)), seq(nrow(t(combn(levels(PhospholipaseA2NOPo1$Drug), 2))))), 
              map_signif_level = TRUE, colour = "#cf1b1b", step_increase = .1)+
  theme_bw() +
  theme(panel.grid.major = element_line(colour = "#d3d3d3"),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(), panel.background = element_blank(),
        plot.title = element_text(size = 14, face = "bold"))+
  theme(axis.text.x = element_text(face="bold", color="#3142b0", 
                                   size=8.9, angle=0),
        axis.text.y = element_text(face="bold", color="#3142b0", 
                                   size=8.9, angle=0))+
  theme(
    plot.title = element_text(size = 12, face = "bold"),
    plot.caption = element_text(hjust = 0, size = 8, face = "italic")
  )+
  scale_linetype_manual('Legend',values='solid')+
  scale_shape_manual('',values = 18)+
  theme(legend.spacing.y = unit(0.01, "cm"))

I am sorry for not being clear. I wanted to add the amount and the concentration used for treatment, so something like

Legend
Vehicle-Vehicle 10microL of H20
Vehicle-Nicotinic Acid 10microL of H20 - 10microL of 1 mM Nicotinic Acid,
and so on

That was my imagination, as it would add the missing information.

And by looking at your example, that is something I had previously. I had legend and shape 18 expressed as a mean.
After reading a few things, I knew that ggplot2 to produce any legend should have separate aesthetics mapped. However, even though I managed to create a Legend similar to yours, I was stuck, as I could not make it look as I wanted because any code manipulation was wrong, so I was like - I need help.

Ok thanks,
So it seems to me you want to add annotations.
Like somewhat arbitrary text/metadata that you place where you think it looks best.

Perhaps this would help :
8 Annotations | ggplot2 (ggplot2-book.org)

Thank you once again for your help. However, I have been trying these annotations. It seems that they are helpful if such pieces of information are included in the data you want to use—for example, the data with different types of cars have the name of these cars included within the data. Therefore, annotations are the best as you can direct ggplo2 how to use them. Unfortunately, in my example, the amount and the concentrations are not included within the data. Therefore, I have been thinking of adding everything manually.
However, after creating significant values over my drugs, I can manually add what you have added in the example above. I just wanted to add something like this manually on the right-hand side:
Legend:
(box shape and its color) Vehicle - Vehicle (10microL of H20)
(box shape and its color) Vehicle -Nicotinic Acid (10microL of H20 + 10microL of 1mM)
(box shape and its color) Sp-Vehicle (10microL of 5microM + 10microL of H20)
(box shape and its color)Sp-Nicotinic Acid( 10microL of 5microM + 10microL of 1mM)
Everything manually, therefore I cannot use annotations. The problem is that whenever I was trying to do it within the geom_boxplot environment, either it did not see the code used or produced some errors.

EDIT:
I have been trying to create my legend within the geom_point environment by simply adding another aes map --->>>

geom_point((aes(color = set))) +
scale_color_manual(values = c("#B2182B", "#D6604D", "#F4A582", "#FDDBC7", "#D1E5F0" ),
                     labels = c("Blank (50 microL of dH20)", "Vehicle - Vehicle (10microL of H20)", "Vehicle -Nicotinic Acid (10microL of H20 + 10microL of 1mM)", "Sp-Vehicle (10microL of 5microM + 10microL of H20)", "Sp-Nicotinic Acid( 10microL of 5microM + 10microL of 1mM)"))+
  scale_x_discrete(labels = c("Blank", "Vehicle- Vehicle", "Vehicle - Nicotinic Acid", "SP - Vehicle", "SP - Nicotinic Acid"))+

Unfortunately, due to the fact that I a total noob in the R, I have no clue where, and how to create the object set to be found by the R. When I am running such code the following error I get:

Error2

So, I think I am on the right track to make my legend come true, but I am stuck on how and where to create the object set.

Any ideas?

Thank you in advance

EDIT2:

I have found the solution:

First of all, while using scale_color_manual, we must add another aes mapping, in order to produce any type of legend. Therefore, I have added to the geom_point(), geom_point(aes(colour = "Blank (50 µM of dH20)")). Then, having an aes mapping I could produce my legend using the following codes:

scale_color_manual(name = "Final concentrations and volumes",
                     labels = c( "Blank (50 µL of dH20)", "Vehicle - Vehicle (10µL of dH20 + 10µL of dH20)", "Vehicle -Nicotinic Acid (10µL of dH20 + 10µL of 1mM)", "Sp-Vehicle (10µL of 5µM + 10µL of dH20)", "Sp-Nicotinic Acid( 10µL of 5µM + 10µL of 1mM)"),
                     values = c("Blank (50 µL of dH20)" =  "#B2182B", "Vehicle - Vehicle (10µL of dH20 + 10µL of dH20)" = "#D6604D", "Vehicle -Nicotinic Acid (10µL of dH20 + 10µL of 1mM)" = "#F4A582", "Sp-Vehicle (10µL of 5µM + 10µL of dH20)"= "#FDDBC7", "Sp-Nicotinic Acid(10µL of 5µM + 10µL of 1mM)"= "#D1E5F0"))+

And as a result, I have got the following outcome

Now, I am working on how to add a subscript to H20. I know that I could use expression or bquote, but so far I am getting something like this

. Yes, I know it says "unexpected ')'", however, I think I messing up with "*", or something.

Small things, many hours, but there is a little of satisfaction.

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.