I made this plot with ggplot, with a bargraph and two line and points plots.
I can't figure out why also the two line and points plots in the legend have the square around them as for the barplot. I'd like to remove it, but I can't understand how. I attach the image of the plot to help you to understand it better.
Here the ggplot code that I wrote to have this plot.
ggplot(CO2_F,aes(x=Date,group=1))+
geom_col(aes(y=Water/1.2,colour = "Water"), size = 1.5, fill = "#33CCFF")+
scale_y_continuous(sec.axis = sec_axis(trans = ~ .*1.2, name = expression(bold(atop(Water,(mm))))))+
geom_point(aes(y=CO2_Fert,colour = "CO2_Fert"),size = 2)+
geom_line(data=CO2[!is.na(CO2$CO2_Fert),],aes(y=CO2_Fert,color="CO2_Fert"), size = 1)+
geom_errorbar(aes(ymin=CO2_Fert-se_Fert, ymax=CO2_Fert+se_Fert,color="CO2_Fert"), width=1,position=position_dodge(0.1))+
geom_point(aes(y=CO2_Granular,colour = "CO2_Granular"),size = 2)+
geom_line(data=CO2[!is.na(CO2$CO2_Granular),],aes(y=CO2_Granular,color="CO2_Granular"), size = 1)+
geom_errorbar(aes(ymin=CO2_Granular-se_Granular, ymax=CO2_Granular+se_Granular,color="CO2_Granular"), width=1,position=position_dodge(0.05))+
labs(y = expression(bold(atop(CO["2"],("\u03bc"*mol~m^bold("-2")~s^bold("-1"))))), x = "Datetime")+
scale_color_manual(name = "", values = c("Water" = "#33CCFF","CO2_Fert" = "#5436D9", "CO2_Granular" = "#FF9900"))+
ggtitle("Fert Vs Granular") +
scale_x_date(date_labels="%b %d",date_breaks ="7 days",date_minor_breaks = "1 day",limits = c(as.Date("2023-06-30"), as.Date("2023-09-22")))+
theme_classic()+theme(legend.position = "top")+
theme(axis.title.y.left = element_text(margin = margin(r=10),color="black", size=14),
axis.title.y.right = element_text(margin = margin(l=10),color="black", size=14),
plot.title = element_text(color="black", size=14, face="bold",hjust = 0.5),
axis.text.y = element_text(color="black",size=10),
axis.title.x = element_text(color="black", size=14,face="bold"),
axis.text.x = element_text(color="black",size=12),
legend.text = element_text(color = "black", size = 12),
legend.background = element_blank(),legend.box = "horizontal")
In which line I've introduced this error? What should I have to modify? Someone can help me? Thanks.

This is not really an error. But getting the legend right when multiple geom and/or aesthetics are involved is kind of an art. (;
The "shape" or symbol used as the legend key is determined by the
key_glyphof a geom. And if you have multiple geom layers (e.g. in your case we have four: col, point, line, errorbar) all the key glyphs get merged. In your case the border surrounding the legend keys stems from the key glyph used bygeom_col.From my experience there is not always a "one size fits all" solution to fix the legend. One option which works in your case is to map on the
fillaes ingeom_col, get rid of the outline by settinglinewidth = 0and add ascale_fill_manualwhere I set the fill color to"transparent"for the point/line/errobar `.Second, to make sure that the
colorandfilllegends get merged use the samename=and setlimits=to include all three categories.Using some fake random data: