Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't use custom colors and get a legend from display_xy() #126

Closed
friendly opened this issue Feb 16, 2024 · 7 comments
Closed

Can't use custom colors and get a legend from display_xy() #126

friendly opened this issue Feb 16, 2024 · 7 comments

Comments

@friendly
Copy link

Using a version of the Penguins data, I want to display grand and other tours, but use custom colors and get a legend. The code in display_xy() doesn't allow this because the only way to specify col= and get a legend is via the palette= arg.

I've looked at the code, but can't see how to modify it to meet this need. Perhaps allowing palette = to specify a named vector of colors?

From display_xy#68:

 # If colors are a variable, convert to colors
  if (is.factor(col) | !areColors(col)) {
    gps <- col
    col <- mapColors(col, palette)
  }

Here's my example:

data(peng, package = "heplots")
peng_scaled <- scale(peng[,3:6])
colnames(peng_scaled) <- c("BL", "BD", "FL", "BM")
peng.col <- c("#F89D38", "#9A78B8", "#73C05B" )
#   Adelie Chinstrap    Gentoo 
# "#F89D38" "#9A78B8" "#73C05B" 

(These colors are actually produced using a function, peng.colors() that gives the result as a vector with the species names.)

Then, try to use this -- no legend is produced

col <- peng.colors()[peng$species] 
pch <- c(21, 22, 24)[peng$species] 

animate(peng_scaled,
        tour_path = grand_tour(d=2),
        display = display_xy(col = col,
                             pch = pch,
                             cex = 1.2))

image

The code depends on col and pch being factor. If I make them so, I get two separate legends, but species aren't labelled.

col <- peng.colors()[peng$species] |> as.factor()
pch <- c(21, 22, 24)[peng$species] |> as.factor()

animate(peng_scaled,
        tour_path = grand_tour(d=2),
        display = display_xy(col = col,
                             pch = pch,
                             cex = 1.2))

image

@friendly
Copy link
Author

Oh, I should add something that does work, if I'm willing to use one of the built-in palettes.

animate(peng_scaled,
        tour_path = guided_tour(holes()),
        display = display_xy(col = peng$species,
                             palette = "Roma",
                             cex = 1.5))

image

@dicook
Copy link
Member

dicook commented Mar 7, 2024

Hi Michael,

Apologies for the delay in responding. I'm actively working the code at the moment, with fixes required by CRAN, and based on new work.

It was deliberate to have this distinction, and only put the legend if a variable is used. And expect the palette to specify colours. If colours are specified entirely by setting the col value it's difficult to know what label to apply.

Your idea of using the palette= might be workable!

@dicook
Copy link
Member

dicook commented Mar 7, 2024

Oh, that was easy - it should work now, eg

animate_xy(flea[,1:6], col=flea$species, palette=c("#F89D38", "#9A78B8", "#73C05B" ))

@friendly
Copy link
Author

friendly commented Mar 7, 2024

Great! I thought that shouldn't be hard.
But what about pch?

If I try to also set pch, I get the correct point symbols in the plot, but not in the legend

animate(peng_scaled,
        tour_path = grand_tour(d=2),
        display = display_xy(col = peng$species,
                             palette = peng.colors("dark"),
                             pch = 15:17,
                             cex = 1.2))

image

PS: I like the new guided_anomaly_tour !

@dicook
Copy link
Member

dicook commented Mar 7, 2024

Oh, you are quick to see the new projection pursuit index! I think it might finally be working this morning.

The shapes palette control has been implemented now, try:

animate_xy(flea[, -7], col = flea$species, pch = flea$species, shapeset=c(1,2,3))

@dicook
Copy link
Member

dicook commented Mar 10, 2024

Thanks for your suggestion Michael, I'm closing this issue as fixed now.

@dicook dicook closed this as completed Mar 10, 2024
@friendly
Copy link
Author

Thanks for all your help! I'm enjoying playing with tourr

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants