2

How would I specify a fixed colorBy so that the class is the same across charts? For example, if I have two charts on a page and I want setosa red, versicolor blue and virginica to green, how would I fix that in advance? I have multiple charts so the class should be the same color in each chart.

# setosa red / versicolor blue
library(canvasXpress)
y=read.table("cX-irist-dat.txt", header=TRUE, sep="\t", quote="", row.names=1, fill=TRUE, check.names=FALSE, stringsAsFactors=FALSE)
z=read.table("cX-irist-var.txt", header=TRUE, sep= "\t", quote="", row.names=1, fill=TRUE, check.names=FALSE, stringsAsFactors=FALSE)

fn <- data.frame(cbind(y, Species = z), stringsAsFactors=FALSE)
fn1 <- fn[1:90,]     # first dataset
fn2 <- fn[91:150,]   # second dataset shares 1 class with first


x1 <- fn1[,1:3]
x2 <- subset(fn1,select="Species")

canvasXpress(
  data=x1,
  varAnnot=x2,
  axisTickScaleFontFactor=0.5,
  axisTitleScaleFontFactor=0.5,
  colorBy="Species",
  graphType="Scatter3D",
  title="Iris Data Set",
  xAxis=list("Sepal.Length"),
  yAxis=list("Sepal.Width"),
  zAxis=list("Petal.Length")
)

R canvasxpress iris red/blue colorBy class

# versicolor red should be blue / virginica blue should be green
x3 <- fn2[,1:3]
x4 <- subset(fn2,select="Species")

canvasXpress(
  data=x3,
  varAnnot=x4,
  axisTickScaleFontFactor=0.5,
  axisTitleScaleFontFactor=0.5,
  colorBy="Species",
  graphType="Scatter3D",
  title="Iris Data Set",
  xAxis=list("Sepal.Length"),
  yAxis=list("Sepal.Width"),
  zAxis=list("Petal.Length")
)

R canvasxpress also red/blue colorBy class for different labels

I cannot add the tag canvasxpress, but probably should be added - awesome library thank you!

calycolor
  • 726
  • 1
  • 7
  • 19
  • See if [this post](https://stackoverflow.com/questions/21536835/ggplot-colour-points-by-groups-based-on-user-defined-colours) on matching grouping variables to specific colors helps you. – Kamil Feb 09 '18 at 22:32
  • I tried adding a column of HEX codes, but I have the same issue...I still do not know how to set the [canvasxpress colorBy](https://canvasxpress.org/html/data_point_attributes.html#colorBy) parameter to have the same colors for multiple charts....in other words, so give a color/values for the colorBy parameter. – calycolor Feb 12 '18 at 18:20

1 Answers1

1

Just add the colorKey parameter to your config like this:

colorKey = list("Species"=list("setosa"="gold","versicolor"="silver","virginica"="red"))

and then call:

canvasXpress(
  data=x3,
  varAnnot=x4,
  axisTickScaleFontFactor=0.5,
  axisTitleScaleFontFactor=0.5,
  colorBy="Species",
  graphType="Scatter3D",
  title="Iris Data Set",
  xAxis=list("Sepal.Length"),
  yAxis=list("Sepal.Width"),
  zAxis=list("Petal.Length"),
  colorKey=colorKey
)
Isaac
  • 26
  • 2