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

How to add cats in legend? #1

Open
nroak opened this issue Jul 8, 2021 · 1 comment
Open

How to add cats in legend? #1

nroak opened this issue Jul 8, 2021 · 1 comment

Comments

@nroak
Copy link

nroak commented Jul 8, 2021

I'm running a simple point plotting with this package. I would like to add this to the legend but since I'm not using any aesthetic, legend doesn't show up.
p = p + geom_cat(aes(y=freq,x=category,cat = cats))

@R-CoderDotCom
Copy link
Owner

Hi, at this moment the legend doesn't work as it should. Considering the following example:

set.seed(1)
# Data simulation
x <- runif(30)
y <- 5 * x ^ 2 + rnorm(length(x), sd = 2)
group <- ifelse(x < 0.4, "bongo",
                ifelse(x > 0.8, "nyancat", "toast"))
x <- x + runif(length(x), -0.2, 0.2)

# Data frame
df <- data.frame(x = x, y = y, group = group)


library(ggplot2)
library(ggcats)

# Scatter plot by group
ggplot(df, aes(x = x, y = y, color = group)) +
  geom_cat(cat = group, size = 4)

imagen

This is because the rasterGrob function used within draw_key_cat function only accepts an unique image and at this moment I don't know how to fix it.

draw_key_cat <- function(data, params, size) {

  filename <- system.file(paste0(data$cat, ".png"), package = "ggcats")
  img <- as.raster(png::readPNG(filename))
  aspect <- dim(img)[1]/dim(img)[2]
  # rasterGrob
  grid::rasterGrob(image = img,
                   width = ggplot2::unit(data$size / size, 'snpc'),
                   height = ggplot2::unit(data$size / size * aspect, 'snpc'))
}

If you have any solution please make a PR.

Best regards

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