satijalab / seurat

R toolkit for single cell genomics
http://www.satijalab.org/seurat
Other
2.3k stars 918 forks source link

ImageDimPlot seems confusing and unfriendly to programmers #9309

Open Puriney opened 2 months ago

Puriney commented 2 months ago

Problem

ImageDimPlot seems not friendly to programmers. I was about to draw area of interests to the spatial images, but I find the way ImageDimPlot and SingleImagePlot use ggplot2 makes my work of annotation very frustrating and my scripts very confusing.

Here is an example showing the problems: 1) The raw image is up-side-down of the raw spatial coordinates as visualized by ggplot2. 2) Adding a vertical line using geom_vline will draw a horizontal line.

image

The first issue is not surprising, because the imaging data (seems always) has the reversed y-axis. Reversing ggplot2's y-axis can quickly solve the problem. What bothers me is the second issue. Because to visualize the correct ROIs, I need to feed the real x to y and feed the real y to x. In this case, if I want to draw a vertical line, I need using geom_hline -- making my codes very confusing to others.

This problem is applicable to other spatial-related visualization functions.

Why

It is because SingleImagePlot firstly always feed the real x to y and the real y to x. See source code here:

https://github.com/satijalab/seurat/blob/1549dcb3075eaeac01c925c4b4bb73c73450fc50/R/visualization.R#L8780-L8788

Then, to achieve the effect of flip_xy=FALSE, the following lines have to 'ad-hoc' fix the problem by doing coord_flip(); this is confusing to programmers. https://github.com/satijalab/seurat/blob/1549dcb3075eaeac01c925c4b4bb73c73450fc50/R/visualization.R#L2587-L2590

Though the end-result of visualization is right, it leads to very frustrating effect on following annotations -- I will have to feed the real x to y and the real y to x.

Example to show the idea

image
# insert reproducible example here
iris$raw_x <- iris$Sepal.Length
iris$raw_y <- iris$Petal.Length

ggplot(iris, aes(x=raw_x, y=raw_y)) +
  geom_point() +
  geom_vline(xintercept =5, color='red') + 
  labs(title='correct and wanted annotation')

# Mimics the implementation of flip_xy=FALSE
# It gives the correct result
ggplot(iris, aes(y=raw_x, x=raw_y)) +
  geom_point() + 
  coord_flip() +
  labs(title='Mimics the implemention of flip_xy=FALSE')

# However, it leads to confusing and frustrating further annotations
ggplot(iris, aes(y=raw_x, x=raw_y)) +
  geom_point() + 
  coord_flip() +
  geom_vline(xintercept =5, color='red')+
  labs(title='wrong annotation because of the complexed implementation')

Possible solution

ggplot(iris, aes(x=raw_x, y=raw_y)) +
  geom_point() +
  scale_y_reverse() +
  geom_vline(xintercept =5, color='red') + 
  labs(title='repeat the in situ visulization and wanted annotation')
Puriney commented 2 months ago

raw ggplot2 + scale_y_reverse() + geom_vline(xintercept = 1000, color='red')

image
alikhuseynov commented 2 months ago

I hear you, see these discussions: