Colleagues, I wish to create y-data labels which meet a criterion. Here is my reproducible code. library(dplyr) library(ggplot2) library(cowplot) above_92 <- filter(faithful,waiting>92) ggplot(faithful,aes(x=eruptions,y=waiting))+ geom_point(shape=21,size=3,fill="orange")+ theme_cowplot()+ geom_hline(yintercept = 92)+ annotate(geom="text",x=above_92$eruptions,y=above_92$waiting+2,label=above_92$waiting) A bit of trial and error is required to figure out what number to add or subtract to above_92$waiting. Is there a more efficient way to do this? Thomas Subia Lean Six Sigma Senior Practitioner DR?XLMAIER Group DAA Draexlmaier Automotive of America LLC mailto:thomas.subia at draexlmaier.com http://www.draexlmaier.com "Nous croyons en Dieu. Tous les autres doivent apporter des donn?es. Edward Deming Public: All rights reserved. Distribution to third parties allowed.
Another approach (still seems to require experimentation to get the value '2') would be to replace the annotate() with geom_text(data=above_92, aes(x=eruptions,y=waiting, label=waiting), nudge_y=2) At least this makes clear the reason for the magic number '2' in the code. For details see ?geom_text On Thu, Oct 5, 2023 at 10:38?AM Subia Thomas OI-US-LIV5 < Thomas.Subia at draexlmaier.com> wrote:> Colleagues, > > I wish to create y-data labels which meet a criterion. > > Here is my reproducible code. > library(dplyr) > library(ggplot2) > library(cowplot) > > above_92 <- filter(faithful,waiting>92) > > ggplot(faithful,aes(x=eruptions,y=waiting))+ > geom_point(shape=21,size=3,fill="orange")+ > theme_cowplot()+ > geom_hline(yintercept = 92)+ > > annotate(geom="text",x=above_92$eruptions,y=above_92$waiting+2,label=above_92$waiting) > > A bit of trial and error is required to figure out what number to add or > subtract to above_92$waiting. > > Is there a more efficient way to do this? > > > Thomas Subia > Lean Six Sigma Senior Practitioner > > DR?XLMAIER Group > DAA Draexlmaier Automotive of America LLC > > mailto:thomas.subia at draexlmaier.com > http://www.draexlmaier.com > > "Nous croyons en Dieu. > Tous les autres doivent apporter des donn?es. > Edward Deming > > > Public: All rights reserved. Distribution to third parties allowed. > > ______________________________________________ > R-help at 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. >[[alternative HTML version deleted]]
?s 20:34 de 04/10/2023, Subia Thomas OI-US-LIV5 escreveu:> Colleagues, > > I wish to create y-data labels which meet a criterion. > > Here is my reproducible code. > library(dplyr) > library(ggplot2) > library(cowplot) > > above_92 <- filter(faithful,waiting>92) > > ggplot(faithful,aes(x=eruptions,y=waiting))+ > geom_point(shape=21,size=3,fill="orange")+ > theme_cowplot()+ > geom_hline(yintercept = 92)+ > annotate(geom="text",x=above_92$eruptions,y=above_92$waiting+2,label=above_92$waiting) > > A bit of trial and error is required to figure out what number to add or subtract to above_92$waiting. > > Is there a more efficient way to do this? > > > Thomas Subia > Lean Six Sigma Senior Practitioner > > DR?XLMAIER Group > DAA Draexlmaier Automotive of America LLC > > mailto:thomas.subia at draexlmaier.com > http://www.draexlmaier.com > > "Nous croyons en Dieu. > Tous les autres doivent apporter des donn?es. > Edward Deming > > > Public: All rights reserved. Distribution to third parties allowed. > > ______________________________________________ > R-help at 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.Hdello, Yes, there is an automatic way of doing this. Use a new data set in geom_text or annotate. Below I use geom_text. Then vjust will take care of the labels placement. library(dplyr) library(ggplot2) library(cowplot) above_92 <- filter(faithful, waiting > 92) ggplot(faithful, aes(x = eruptions, y = waiting)) + geom_point(shape=21,size=3,fill="orange") + geom_hline(yintercept = 92) + # use a new data argument here geom_text( data = above_92, mapping = aes(x = eruptions, y = waiting, label = waiting), vjust = -1 ) + theme_cowplot() Hope this helps, Rui Barradas -- Este e-mail foi analisado pelo software antiv?rus AVG para verificar a presen?a de v?rus. www.avg.com
Hi You could use the 'gggrid' package, which gets you access to other coordinate systems. For example, the following places the text labels 2mm above the (centres of the) data points (bottom-justified) ... library(gggrid) labels <- function(data, coords) { textGrob(data$label, coords$x, unit(coords$y, "npc") + unit(2, "mm"), just="bottom") } ggplot(faithful, aes(x = eruptions, y = waiting)) + geom_point(shape=21, size=3, fill="orange") + geom_hline(yintercept = 92) + grid_panel(labels, data = above_92, mapping = aes(x = eruptions, y = waiting, label = waiting)) + theme_cowplot() Hope that helps. Paul On 5/10/2023 8:34 am, Subia Thomas OI-US-LIV5 wrote:> Colleagues, > > I wish to create y-data labels which meet a criterion. > > Here is my reproducible code. > library(dplyr) > library(ggplot2) > library(cowplot) > > above_92 <- filter(faithful,waiting>92) > > ggplot(faithful,aes(x=eruptions,y=waiting))+ > geom_point(shape=21,size=3,fill="orange")+ > theme_cowplot()+ > geom_hline(yintercept = 92)+ > annotate(geom="text",x=above_92$eruptions,y=above_92$waiting+2,label=above_92$waiting) > > A bit of trial and error is required to figure out what number to add or > subtract to above_92$waiting. > > Is there a more efficient way to do this? > > > Thomas Subia > Lean Six Sigma Senior Practitioner > > DR?XLMAIER Group > DAA Draexlmaier Automotive of America LLC > > mailto:thomas.subia at draexlmaier.com > http://www.draexlmaier.com > <http://www.draexlmaier.com> > > "Nous croyons en Dieu. > Tous les autres doivent apporter des donn?es. > Edward Deming > > > Public: All rights reserved. Distribution to third parties allowed. > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > <https://stat.ethz.ch/mailman/listinfo/r-help> > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > <http://www.R-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code.-- Dr Paul Murrell Te Kura Tatauranga | Department of Statistics Waipapa Taumata Rau | The University of Auckland Private Bag 92019, Auckland, New Zealand 64 9 3737599 x85392 paul at stat.auckland.ac.nz www.stat.auckland.ac.nz/~paul/