Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

gplot legend problem with geomcol plot and geompoint + geomline plot

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.

The Plot

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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_glyph of 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 by geom_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 fill aes in geom_col, get rid of the outline by setting linewidth = 0 and add a scale_fill_manual where I set the fill color to "transparent" for the point/line/errobar `.

Second, to make sure that the color and fill legends get merged use the same name= and set limits= to include all three categories.

Using some fake random data:

library(ggplot2)
set.seed(1)

CO2 <- data.frame(
  Date = 1:20,
  Water = runif(20, 1, 8),
  CO2_Fert = runif(20, 2, 3),
  CO2_Granular = runif(20, 3, 4),
  se_Fert = runif(20),
  se_Granular = runif(20)
)

ggplot(CO2, aes(x = Date, group = 1)) +
  geom_col(aes(y = Water / 1.2, fill = "Water"), linewidth = 0) +
  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"), linewidth = 1
  ) +
  geom_errorbar(
    aes(ymin = CO2_Fert - se_Fert, ymax = CO2_Fert + se_Fert, color = "CO2_Fert"),
    width = 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"), linewidth = 1
  ) +
  geom_errorbar(
    aes(ymin = CO2_Granular - se_Granular, ymax = CO2_Granular + se_Granular, color = "CO2_Granular"),
    width = 1
  ) +
  labs(y = expression(
    bold(atop(CO["2"], ("\u03bc" * mol ~ m^bold("-2") ~ s^bold("-1"))))
  ), x = "Datetime") +
  scale_color_manual(
    name = NULL,
    values = c("Water" = "#33CCFF", "CO2_Fert" = "#5436D9", "CO2_Granular" = "#FF9900"),
    limits = c("Water", "CO2_Fert", "CO2_Granular")
  ) +
  scale_fill_manual(
    name = NULL,
    values = c("Water" = "#33CCFF", "CO2_Fert" = "transparent", "CO2_Granular" = "transparent"),
    limits = c("Water", "CO2_Fert", "CO2_Granular")
  ) +
  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"
  )

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading