[R] ggplot pointrange from other df and missing legend

PIKAL Petr petr@p|k@| @end|ng |rom prechez@@cz
Mon May 16 14:21:40 CEST 2022


Hallo Rui

Thanks. After I sent the first mail I noticed the missing tablePal.

palette("Tableau 10")
tablePal <- palette()

ggplot() +
   geom_col(
     data=test,
     aes(x=Sample, y=value, fill=SSAtype),
     position="dodge"
   ) +
   geom_pointrange(
     data=test.ag,
     aes(x=Sample, y=avg, ymin=avg-odch, ymax=avg+odch),
     size = 2
   ) +
   scale_fill_manual(
    values=tablePal,
     name="Calculation\n performed\n according to",
     labels=c("bla", "blabla")
   ) +
   scale_shape_manual(name = "Measured", values = 19, labels = NULL)

You are correct that legend for bars is there. But the legend for points is missing. If I use only geom_point and values from the "test" data, also legend for the point appears. Is it possible that the second legend could be created only if all data are from one data frame?

Best regards
Petr

> -----Original Message-----
> From: Rui Barradas <ruipbarradas using sapo.pt>
> Sent: Monday, May 16, 2022 1:53 PM
> To: PIKAL Petr <petr.pikal using precheza.cz>; r-help using r-project.org
> Subject: Re: [R] ggplot pointrange from other df and missing legend
> 
> Hello,
> 
> In the code below the legend is not missing, I couldn't reproduce that error.
> As for the size of the points, include argument size. I have size=2.
> 
> Also:
>   - tablePal is missing, I've chosen colors 2:3;
>   - I have commented out the guides(.), you are setting the shape after so it
> makes no difference.
> 
> 
> ggplot() +
>    geom_col(
>      data=test,
>      aes(x=Sample, y=value, fill=SSAtype),
>      position="dodge"
>    ) +
>    geom_pointrange(
>      data=test.ag,
>      aes(x=Sample, y=avg, ymin=avg-odch, ymax=avg+odch),
>      size = 2
>    ) +
>    scale_fill_manual(
>      #values=tablePal,
>      values = 2:3,
>      name="Calculation\n performed\n according to",
>      labels=c("bla", "blabla")
>    ) +
>    #guides(fill = guide_legend(override.aes = list(shape = NA))) +
>    scale_shape_manual(name = "Measured", values = 19, labels = NULL)
> 
> 
> Hope this helps,
> 
> Rui Barradas
> 
> 
> Às 12:15 de 16/05/2022, PIKAL Petr escreveu:
> > Hallo all
> >
> >
> >
> > Here are the data from dput
> >
> >
> >
> > test <- structure(list(Sample = c("A", "A", "A", "A", "A", "A", "B",
> >
> > "B", "B", "B", "B", "B", "C", "C", "C", "C", "C", "C"), SSAtype = c("one",
> >
> > "one", "one", "two", "two", "two", "one", "one", "one", "two",
> >
> > "two", "two", "one", "one", "one", "two", "two", "two"), value =
> > c(8.587645149,
> >
> > 8.743793651, 8.326440422, 9.255940687, 8.971931555, 8.856323865,
> >
> > 9.650809096, 9.725504448, 9.634449367, 9.69485369, 9.526758476,
> >
> > 10.03758001, 10.76845392, 10.66891602, 10.34894497, 10.76284989,
> >
> > 10.53074081, 11.16464528), SSAmeasuredP = c(8.3, 8.3, 8.3, 8.3,
> >
> > 8.3, 8.3, 9.5, 9.5, 9.5, 9.5, 9.5, 9.5, 11, 11, 11, 11, 11, 11
> >
> > ), identity = c("point", "point", "point", "point", "point",
> >
> > "point", "point", "point", "point", "point", "point", "point",
> >
> > "point", "point", "point", "point", "point", "point")), row.names = c(NA,
> >
> > 18L), class = "data.frame")
> >
> >
> >
> > test.ag <- structure(list(Sample = c("A", "B", "C"), avg = c(8.3, 9.5, 11
> >
> > ), odch = c(0.2, 0.4, 0.3), identity = c("point", "point", "point"
> >
> > )), row.names = c(NA, 3L), class = "data.frame")
> >
> >
> >
> > I try to make some relatively simple barplot and I wanted to add points to
> > it which I somehow did.
> >
> >
> >
> > library(ggplot2)
> >
> >
> >
> > p <- ggplot(test, aes(x=Sample, y=value, fill=SSAtype))
> >
> > p+geom_col(position="dodge")+geom_point(aes(y=SSAmeasuredP,
> shape=identity),
> > size=5)+
> >
> > scale_fill_manual(values=tablePal, name="Calculation\n performed\n
> according
> > to",
> >
> > labels=c("bla", "blabla"))+
> >
> > guides(fill= guide_legend(override.aes = list(shape=NA)))+
> >
> > scale_shape_manual(name = "Measured", values=19, labels=NULL)
> >
> >
> >
> > But instead of points I want to use pointrange with data from other df. I
> > found some help and all is good except size of the points and missing
> legend
> >
> >
> >
> > ggplot()+
> >
> > geom_col(data=test, aes(x=Sample, y=value, fill=SSAtype),
> position="dodge")+
> >
> > scale_fill_manual(values=tablePal, name="Calculation\n performed\n
> according
> > to",
> >
> > labels=c("bla", "blabla"))+
> >
> > geom_pointrange(data=test.ag, aes(x=Sample, y=avg, ymin=avg-odch,
> >
> > ymax=avg+odch)) +
> >
> > guides(fill= guide_legend(override.aes = list(shape=NA)))+
> >
> > scale_shape_manual(name = "Measured", values=19, labels=NULL)
> >
> >
> >
> > Although I will try to find some workable way I also would like to ask for
> > help from R gurus, maybe I overlooked some simple way how to do it.
> >
> >
> >
> > Best regards
> >
> > Petr
> >
> >
> > ______________________________________________
> > R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> > and provide commented, minimal, self-contained, reproducible code.


More information about the R-help mailing list