[R] Color in stripchart
Gerrit Eichner
gerr|t@e|chner @end|ng |rom m@th@un|-g|e@@en@de
Wed Mar 10 15:04:05 CET 2021
Dear Robin,
if you study stripchart's code (graphics:::stripchart) carefully
you will find out that the elements of the vector provided to the
col-argument (and the pch-argument as well) are selected by iterating
along the groups (in your case year). I don't seen easy solution for
your problem using stripchart, but you may want to look at the
examples of function beeswarm in package beeswarm.
Hth -- Gerrit
---------------------------------------------------------------------
Dr. Gerrit Eichner Mathematical Institute, Room 212
gerrit.eichner using math.uni-giessen.de Justus-Liebig-University Giessen
Tel: +49-(0)641-99-32104 Arndtstr. 2, 35392 Giessen, Germany
http://www.uni-giessen.de/eichner
---------------------------------------------------------------------
Am 10.03.2021 um 14:08 schrieb Dr. Robin Haunschild:
> Dear fellow R users,
>
> I'd like to color individual points in a stripchart. This works well as
> long as I have only a single value per y axis category:
>
> df <- data.frame(year = seq(2011, 2018), value = seq(10,80, 10))
> df$color <- 'black'
> df[df$value<33,]$color <- 'blue'
> df[df$value>66,]$color <- 'red'
> stripchart(df$value ~ df$year, pch=18, col=df$color, method='stack')
>
> When I use multiple values per y axis category, all points in a specific
> y axis category have the same color:
>
> set.seed(20210310)
> years <- sample(x=seq(2011, 2018), size = 365*8, replace = TRUE)
> values <- sample(x=seq(0,100), size = 365*8, replace = TRUE)
> df <- data.frame(year = years, value = values)
> df$color <- 'black'
> df[df$value<33,]$color <- 'blue'
> df[df$value>66,]$color <- 'red'
> stripchart(df$value ~ df$year, pch=18, col=df$color, method='stack')
>
> I'd expect to see all points with a value below 33 in blue color, all
> points with a value above 66 in red color, and the remaining points in
> black color.
>
> I can use a black stripchart plot as basis and plot blue and red points
> over the black ones, but I do not get the stacking right:
>
> stripchart(df$value ~ df$year, pch=18, method='stack')
> points(df[df$color=='blue',]$value, df[df$color=='blue',]$year-2010,
> type='p', pch=18, col='blue')
> points(df[df$color=='red',]$value, df[df$color=='red',]$year-2010,
> type='p', pch=18, col='red')
>
> Am I somehow misusing the stripchart function?
>
>
> Best regards,
>
> Robin
>
>
> ______________________________________________
> 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