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

legend improvements #106

Closed
3 of 4 tasks
cnrrobertson opened this issue May 24, 2024 · 8 comments
Closed
3 of 4 tasks

legend improvements #106

cnrrobertson opened this issue May 24, 2024 · 8 comments
Assignees
Labels
enhancement New feature or request

Comments

@cnrrobertson
Copy link
Contributor

cnrrobertson commented May 24, 2024

Describe how it works in R's ggplot2

  • Legends work with categorical variables, Example: ggplot(diamonds, aes(x=x,y=y,color=color)) + geom_point()
  • Multiple legends
    • For multiple aes specs. Example: ggplot(diamonds, @aes(x=Depth, y=Price)) + geom_point(@aes(color=Carat)) + geom_point(@aes(color=Table))
    • Generate a legend for aes with multiple features (such as both size and color), Example: ggplot(diamonds, aes(x=x,y=y,color=color, size=carat)) + geom_point()
@cnrrobertson cnrrobertson added the enhancement New feature or request label May 24, 2024
@rdboyes
Copy link
Member

rdboyes commented May 26, 2024

Can you elaborate on the first point? I think legends already work with categorical variables, e.g.

ggplot(penguins, @aes(x=bill_length_mm,y=bill_depth_mm,color=island)) + geom_point()

image

@cnrrobertson
Copy link
Contributor Author

You're right! I didn't realize. But I think I've stumbled on a bug - it doesn't work when the DataFrame column for color is a CategoricalVector (apparently most/all? datasets from RDatasets.jl have categorical columns stored in that format). But I think this an upstream issue in Makie:

ERROR: MethodError: no method matching to_color(::CategoricalValue{String, UInt8})

Closest candidates are:
  to_color(::Makie.Palette)
   @ Makie ~/.julia/packages/Makie/iRM0c/src/conversions.jl:845
  to_color(::Symbol)
   @ Makie ~/.julia/packages/Makie/iRM0c/src/conversions.jl:854
  to_color(::Makie.Scene, ::Any, ::Makie.Cycled)
   @ Makie ~/.julia/packages/Makie/iRM0c/src/makielayout/blocks/axis.jl:695
  ...

You can recreate with

using CategoricalArrays
penguins.island = categorical(penguins.island)
ggplot(penguins, @aes(x=bill_length_mm, y=bill_depth_mm, size=island)) + geom_point()

Alternatively, we could try to convert those columns to strings when plotting?

@rdboyes
Copy link
Member

rdboyes commented May 28, 2024

It's strange - I actually convert the Strings to CategoricalVectors under the hood already. Should be an easy fix I think, I'm sure I'm just missing a check somewhere

@rdboyes
Copy link
Member

rdboyes commented Sep 19, 2024

This is fixed in 0.8.0! Multiple legends are still a work in progress though

@Jigyasu4indp
Copy link

In the geom_tile example (link: https://tidierorg.github.io/TidierPlots.jl/latest/examples/generated/geoms/geom_tile/), the color legend is missing. How can I add it?

@rdboyes
Copy link
Member

rdboyes commented Feb 15, 2025

Finally made progress on this!

ggplot(penguins) + 
  geom_point(aes(x = :bill_length_mm, y = :bill_depth_mm, size = :bill_depth_mm, color = :sex)) + 
  scale_size(range = [5, 15], name = "Bill Depth") + 
  scale_color_discrete(palette = :julia, name = "Sex")

Image

@rdboyes
Copy link
Member

rdboyes commented Feb 15, 2025

I'm going to close this as complete - the second bullet does not work even in R ggplot, and seems like a use case we would want to discourage

@rdboyes rdboyes closed this as completed Feb 15, 2025
@Jigyasu4indp
Copy link

I encountered an issue with the following code:

ggplot(gfgt, @aes(x = lon, y = lat, z = co2)) +
  geom_tile() + 
  scale_color_discrete(palette = :tab20, name = "co2") +
  theme_light()

Attributes with 5 entries:
  Axis => Attributes with 13 entries:
    backgroundcolor => transparent
    bottomspinevisible => false
    leftspinevisible => false
    rightspinevisible => false
    topspinevisible => false
    xgridcolor => (:black, 0.07)
    xlabelpadding => 3
    xminorticksvisible => false
    xticksvisible => false
    ygridcolor => (:black, 0.07)
    ylabelpadding => 3
    yminorticksvisible => false
    yticksvisible => false
  Axis3 => Attributes with 9 entries:
    xgridcolor => (:black, 0.07)
    xspinesvisible => false
    xticksvisible => false
    ygridcolor => (:black, 0.07)
    yspinesvisible => false
    yticksvisible => false
    zgridcolor => (:black, 0.07)
    zspinesvisible => false
    zticksvisible => false
  Colorbar => Attributes with 3 entries:
    spinewidth => 0
    ticklabelpad => 5
    ticksvisible => false
  Legend => Attributes with 2 entries:
    framevisible => false
    padding => (0, 0, 0, 0)
  textcolor => gray50

When I run this code as-is, it doesn't work as expected (i.e., it doesn't produce the desired plot). However, if I comment out the scale_color_discrete line:

ggplot(gfgt, @aes(x = lon, y = lat, z = co2)) +
  geom_tile() +
  # scale_color_discrete(palette = :tab20, name = "co2") +
  theme_light()

the plot is generated, but the color legend is missing. Could you please fix the color legend for geom_tile plots?

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

No branches or pull requests

3 participants