Skip to content

Color in stripchart

8 messages · Dr. Robin Haunschild, Gerrit Eichner, Rasmus Liland

#
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
#
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 at 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 Robin and Gerrit,

I am unable to see the difference in the 
plot in the two cases ... 

	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'
	
	file <- "/tmp/robin.png"
	width <- 1000
	height <- 1000
	res <- 150
	png(file=file, width=width, height=height, res=res)
	par(mfrow=c(2,1))
	
	stripchart(df$value ~ df$year, pch=18, col=df$color, method='stack', main="stripchart with color column as col")
	
	stripchart(df$value ~ df$year, pch=18, method='stack', main="stripchart with black basis")
	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')
	
	dev.off()

Best,
Rasmus

-------------- next part --------------
A non-text attachment was scrubbed...
Name: robin.png
Type: image/png
Size: 38927 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20210310/5862f5ba/attachment.png>

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20210310/5862f5ba/attachment.sig>
#
Dear Rasmus,

there is no difference in the small exmaple, because there is only one
point per year.

If you use the example with multiple points per year, you will see the
difference.

Best, Robin
On 3/10/21 3:15 PM, Rasmus Liland wrote:

  
    
#
Dear Gerrit,

thanks a lot; it works with beeswarm and pwcol=df$color.

Best, Robin
On 3/10/21 3:04 PM, Gerrit Eichner wrote:

  
    
#
Hello there again,

Sorry, I missed that part in the middle 
about set.seed.  As per [1], you need to 
run stripchart again with the add 
argument set to TRUE, and slicing the df 
according to the color, like so

	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(value ~ year, pch=18, method='stack', data=df)
	for (color in unique(df$color)) {
	  stripchart(value ~ year, pch=18, method='stack', data=df[df$color==color,], col=color, add=TRUE)
	}

Best,
Rasmus

[1] https://stackoverflow.com/questions/32833210/define-color-for-each-datapoint-in-a-stripchart-separately

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20210310/f42f0e5a/attachment.sig>
#
Dear Rasmus,

thanks, that works, too.

Great!

Best, Robin
On 3/10/21 5:22 PM, Rasmus Liland wrote: