1

I want to plot some data using beeswarm in R, where the bg color of each point is defined by a column in my data. For example:

head(mydata)

## id   provean color
## 1635 9.428   #fee5d9
## 1092 9.000   #fb6a4a
## 791  8.708   #ffffff
## 1472 7.596   #fcae91
## 228  7.552   #ffffff
## 1004 7.433   #fcae91

The color column has the hex color codes I would like each point to have, and I try to implement it like this:

colorByMML <- mydata$color
beeswarm(mydata$provean,pch=21,cex=0.7, bty="n", col="#708090",pwbg=colorByMML)

But I get different colors than the expected ones. enter image description here

If instead of the hex codes I give integers, I get other colors. For instance if instead of the hex code of white(#ffffff) I input '1', I get black as the bg color. In short, how do I customize the color of each point?

Julio Diaz
  • 9,067
  • 19
  • 55
  • 70
  • Try specifying `pwpch` or `pwcol` rather than `pwbg`. I believe bg is for background color. – conv3d Mar 22 '17 at 22:39
  • With pwpch you specify the type of marker and with pwcol sets the color of the border. I want to set the color of the fill. Also If I try to use pwcol, I have the same problem customizing the colors. – Julio Diaz Mar 22 '17 at 22:49
  • Why not use `pwpch=19`? (Or post a MCVE in you want more than guesses.) – IRTFM Mar 23 '17 at 00:45
  • psych only changes the shape of the point not the color. – Julio Diaz Mar 27 '17 at 01:10

1 Answers1

1

Your code should work fine as it is, assuming colorByMML has type "character". However, if colorByMML has type "factor", the integer factor levels will be used (along with your default palette) to set colors. Based on the colors in your figure, I'm guessing this is the problem.

In other words, this is probably what you are looking for:

colorByMML <- as.character(mydata$color)