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 -- Dr. Robin Haunschild Max Planck Institute for Solid State Research Heisenbergstr. 1 D-70569 Stuttgart (Germany) phone: +49 (0) 711-689-1285 fax: +49 (0) 711-689-1292 email: [hidden email] http://www.fkf.mpg.de/ivs Publons: https://publons.com/researcher/2845202/robin-haunschild/ GS: https://scholar.google.de/citations?user=kDfateQAAAAJ&hl=de&oi=ao ______________________________________________ [hidden email] 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. |
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 [hidden email] 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 > > > ______________________________________________ > [hidden email] 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. > ______________________________________________ [hidden email] 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. |
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 ______________________________________________ [hidden email] 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. |
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 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 > > > ______________________________________________ > [hidden email] 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. > -- Dr. Robin Haunschild Max Planck Institute for Solid State Research Heisenbergstr. 1 D-70569 Stuttgart (Germany) phone: +49 (0) 711-689-1285 fax: +49 (0) 711-689-1292 email: [hidden email] http://www.fkf.mpg.de/ivs Publons: https://publons.com/researcher/2845202/robin-haunschild/ GS: https://scholar.google.de/citations?user=kDfateQAAAAJ&hl=de&oi=ao ______________________________________________ [hidden email] 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. |
In reply to this post by Gerrit Eichner
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: > 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 > [hidden email] 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 >> >> >> ______________________________________________ >> [hidden email] 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. >> > > ______________________________________________ > [hidden email] 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. -- Dr. Robin Haunschild Max Planck Institute for Solid State Research Heisenbergstr. 1 D-70569 Stuttgart (Germany) phone: +49 (0) 711-689-1285 fax: +49 (0) 711-689-1292 email: [hidden email] http://www.fkf.mpg.de/ivs Publons: https://publons.com/researcher/2845202/robin-haunschild/ GS: https://scholar.google.de/citations?user=kDfateQAAAAJ&hl=de&oi=ao ______________________________________________ [hidden email] 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. |
In reply to this post by Dr. Robin Haunschild
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 ______________________________________________ [hidden email] 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. |
Dear Rasmus,
thanks, that works, too. Great! Best, Robin On 3/10/21 5:22 PM, Rasmus Liland 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 > -- Dr. Robin Haunschild Max Planck Institute for Solid State Research Heisenbergstr. 1 D-70569 Stuttgart (Germany) phone: +49 (0) 711-689-1285 fax: +49 (0) 711-689-1292 email: [hidden email] http://www.fkf.mpg.de/ivs Publons: https://publons.com/researcher/2845202/robin-haunschild/ GS: https://scholar.google.de/citations?user=kDfateQAAAAJ&hl=de&oi=ao ______________________________________________ [hidden email] 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. |
Good for you!
______________________________________________ [hidden email] 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. |
Free forum by Nabble | Edit this page |