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

fill by variable does not work with ggcyto #91

Open
sapirhv opened this issue Aug 8, 2023 · 2 comments
Open

fill by variable does not work with ggcyto #91

sapirhv opened this issue Aug 8, 2023 · 2 comments

Comments

@sapirhv
Copy link

sapirhv commented Aug 8, 2023

when using ggcyto, the "fill" function of ggplot does not work. It's not recording the variable in "fill" and indicating that it's not found resulting in an error.
sample code:

library(ggcyto)
library(flowCore)
library(ncdfFlow)
library(flowAssist)

# Convert the matrix into a flowFrame object
ff <- DFtoFF(DATA[c("Median.var1", "Median.var2", "Median.var3")])

# Create a flowSet object from the flowFrame
fs <- flowSet(ff)


# Create a plot using ggcyto
p <- ggcyto(fs, aes(x = Median.var1, y = Median.var2)) +
  geom_point(aes(fill = Median.var3)) +
  labs(x = "Median Intensity - var1", 
       y = "Median intenisty - var2") +
  ggtitle("var1 vs var2- Median")

sample error:

Error in `geom_point()`:
! Problem while computing aesthetics.
i Error occurred in the 1st layer.
Caused by error in `FUN()`:
! object 'Median.var3' not found
@sapirhv sapirhv changed the title fill does not work with ggcyto fill by variable does not work with ggcyto Aug 8, 2023
@djhammill
Copy link
Contributor

djhammill commented Aug 8, 2023

@sapirhv, there are a couple of issues with your code example. Variables with dots in their name must always be wrapped in backticks for ggplot and the color aesthetic is the one to modify for geom_point(). The correct code would look something like this:

p <- ggcyto(fs, aes(x = `Median.var1`, y = `Median.var2`)) +
  geom_point(aes(color = `Median.var3`)) +
  labs(x = "Median Intensity - var1", 
       y = "Median intenisty - var2") +
  ggtitle("var1 vs var2- Median")

However, if you run that code you will encounter another issue which points out that Median.var3 doesn't exist. This is because ggcyto only exposes the columns used for the xy axes to ggplot (to conserve memory) and so ggplot does not have access to Median.var3. If you re only plotting a single sample, a simple workaround would be to extract the data manually and pass it ggplot directly:

# extract data
exprs <- as.data.frame(exprs(fs[[1, c("Median.var1", "Median.var2", "Median.var3")]]))
# plot using ggplot
ggplot(exprs, aes(x = `Median.var1`, y = `Median.var2`, color = `Median.var3`)) + geom_point()

Perhaps @mikejiang has a better solution. The problem is that we can't append columns on the fly as new geoms are added, so I think if we wanted to make this work we'd have to make ggcyto expose all the (relevant) columns to ggplot - which would have significant memory implications. @mikejiang would it be possible to capture all variables mentioned within the top-level ggcyto aes() and expose those to ggplot? Then perhaps the following code could work:

# fortify need to include all three columns
p <- ggcyto(fs, aes(x = `Median.var1`, y = `Median.var2`, color = `Median.var3`)) +
  geom_point() 

We could check all mapping values against the colnames or markernames of the flowSet and append them to the dims here:

dims <- mapping[grepl("[x|y]", names(mapping))]

I'd be happy to put together a PR for this, if you think it could work.

@sapirhv
Copy link
Author

sapirhv commented Aug 8, 2023

Thanks a lot for the swift reply!!
unfortunately I cannot use ggplot in this case since I require gating, which is also done in this code in a late stage, something ggplot is having a very hard time with (and is also the reason i used ggcyto in the first place)
do you know of a way to resolve this?
Thanks again!

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