Highlight data points in stripchart

1,179 views
Skip to first unread message

Christoph Ruehlemann

unread,
Jun 18, 2016, 5:24:00 AM6/18/16
to corplin...@googlegroups.com
Hi,

Is there a way to highlight select data points in a stripchart (e.g. by using a different point character or color)?

Best
Chris

--
PD Dr. Christoph Rühlemann
Vertretungsprofessur
Englische Sprachwissenschaft
Institut für Anglistik und Amerikanistik
Universität Paderborn
Warburger Str. 100
33098 Paderborn
Büro: C3.341
Tel: (05251) 60-3804


https://kw.uni-paderborn.de/anglistik-amerikanistik/pd-dr-christoph-ruehlemann/

ἰχθύς



Matías Guzmán Naranjo

unread,
Jun 18, 2016, 5:31:51 AM6/18/16
to corplin...@googlegroups.com
there are several ways to do this depending on the ploting package you're using. In ggplot the easiest is to create an additional factor variable for your data, where the particular data points you want to highlight receive a different value from the rest. Then you set the "color=VAR", where VAR is the new variable. In base graphics I guess you could just replot the particular data points you want to emphasize.

--
You received this message because you are subscribed to the Google Groups "CorpLing with R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to corpling-with...@googlegroups.com.
To post to this group, send email to corplin...@googlegroups.com.
Visit this group at https://groups.google.com/group/corpling-with-r.
For more options, visit https://groups.google.com/d/optout.

Stefan Th. Gries

unread,
Jun 18, 2016, 5:35:57 AM6/18/16
to CorpLing with R
that would work, yes. Take care to set a seed if you use jitter, too,
and want to highlight (one) jittered points. Also, I'm not sure
whether you need to use

stripchart(jitter(y))

as opposed to

stripchart(y, method="jitter")

to make sure you get the right coordinate with jittered data.

HTH,
STG
--
Stefan Th. Gries
----------------------------------
Univ. of California, Santa Barbara
http://tinyurl.com/stgries
----------------------------------

Alex Perrone

unread,
Jun 18, 2016, 11:33:07 AM6/18/16
to corplin...@googlegroups.com
You can set the color equal to a vector for a 1D stripchart, just as one would do with the col variable using the plot function. As in plot, the ifelse trick can be useful to do it on the fly (or you can pre-compute the variable as done further below). 

str(airquality)  # dataset already loaded by R 
stripchart(airquality$Temp, col = "gray", 
           bg = ifelse(airquality$Temp > 80 & airquality$Temp < 90, "red", "black"), 
           pch = 21, 
           xlab = "Temp (F)")

However, this does not work if you are plotting by group due to stripchart deficiencies, apparently. If you are doing by group then you may need to re-plot the points or use ggplot2, as suggested. Because when I try to color by a factor variable using stripchart, it seems to mess up the indices and colors the wrong points. I think it is grouping things internally differently from how it is using the color indices. 

airquality$HighTemp <- airquality$Temp > 80 
stripchart(Temp ~ Month, data = airquality, vertical = TRUE, 
           col = "gray", 
           bg = ifelse(airquality$HighTemp, "red", "black"), 
           pch = 21, 
           xlab = "Temp (F)”)

I’d honestly use ggplot2, but for stripchart this page looked useful: http://www.programiz.com/r-programming/strip-chart

And this SO answer explains the stripchart coloring really well, including how you need bg to color the points as opposed to col: http://stackoverflow.com/a/22923960

Alex
signature.asc
Reply all
Reply to author
Forward
0 new messages