Sandy
Sandy

Reputation: 1148

xyplot how to swap the default colour

I have a dataframe called df

 clust    gender    conf    chall
   1        F        2        6
   2        M        4        1
   1        M        5        2
   1        F        3        5
   3        F        3        4

I want to make an xyplot from the lattic package as below:

xyplot(chall ~ conf, 
   data = df,
   group = gender, 
   auto.key = list(space = 'right'),
   jitter.x = T, jitter.y = T)

The problem is that the default colour assigns 'Blue' to females and 'Pink' to males. I just want to swap these colours. I understand it might be a basic question but I am unable to find a solution to this.

The SO posts that I looked at, were for more advanced changes in plot settings and were not useful to me:

  1. How can I change the color of the header in a xyplot?

  2. change background and text of strips associated to multiple panels in R / lattice

  3. assigning colours to plots in lattice according to Set or factor, not groups

  4. Change default colour lines xyplot with referencing

Any help on this would be highly appreciated.

For convenience, the dput(df):

dput(df)
    structure(list(clust = structure(c(1L, 2L, 1L, 1L, 3L), 
    .Label = c("1", "2", "3"), class = "factor"), 
    gender = c("F", "M", "M", "F", "F"), 
    conf = c(2L, 4L, 5L, 3L, 3L), 
    chall = c(6L, 1L, 2L, 5L, 4L)), 
    row.names = c(NA, 5L), class = "data.frame")

Upvotes: 0

Views: 807

Answers (1)

Tad Dallas
Tad Dallas

Reputation: 1189

Plot options to the symbols are passed via the par.settings argument, which takes a list as an argument and can be used to set colors, point size, etc.

xyplot(chall ~ conf, 
   data = df,
   group = gender, 
   auto.key = list(space = 'right'),
   jitter.x = T, jitter.y = T,
   par.settings = list(superpose.symbol = list(
    col = c("pink", "blue"), pch=16)))

Upvotes: 2

Related Questions