YMa-lab / CARD

GNU General Public License v3.0
80 stars 20 forks source link

The CARD spatial plots are squished compared to the slide image #72

Closed ashishjain1988 closed 2 months ago

ashishjain1988 commented 3 months ago

Hi,

Is there a way to make the spatial plots of the distribution of cell type proportion a bit more wide? It seems like the plots are squished.

image

YMalab commented 3 months ago

Hi @ashishjain1988,

Thank you for your interest in our method!

For a quick solution, you can set the NumCols = 2 or 3. If you want to modify the source code, you can remove the coord_fixed()+ in the CARD.visualize.prop function.

Best, Ying

ashishjain1988 commented 2 months ago

Thank you! It worked for me.

vpcristina commented 2 months ago

Hi, I have this same problem, the plot appears squished, but none of the suggested solutions worked for me. I'm only doing one plot, so NumCols is not the problem, and I can't remove the coord_fixed()+ in the function. Is there something else I can do?

image

Thanks in advance!

YMalab commented 2 months ago

Hi @vpcristina,

Thanks for your interest. You should be able to remove the coord_fixed line by executing the following function before you make the plot:

CARD.visualize.prop <- function(proportion,spatial_location,ct.visualize = ct.visualize,colors = c("lightblue","lightyellow","red"),NumCols, pointSize = 3.0){
if(is.null(colors)){
    colors = c("lightblue","lightyellow","red")
}else{
    colors = colors
}
res_CARD = as.data.frame(proportion)
res_CARD = res_CARD[,order(colnames(res_CARD))]
location = as.data.frame(spatial_location)
if(sum(rownames(res_CARD)==rownames(location))!= nrow(res_CARD)){
   stop("The rownames of proportion data does not match with the rownames of spatial location data")
}
ct.select = ct.visualize
res_CARD = res_CARD[,ct.select]
if(!is.null(ncol(res_CARD))){
res_CARD_scale = as.data.frame(apply(res_CARD,2,function(x){
    (x - min(x)) / (max(x) - min(x))
} ))}else{
res_CARD_scale = as.data.frame((res_CARD - min(res_CARD)) / (max(res_CARD) - min(res_CARD)))
colnames(res_CARD_scale) = ct.visualize
}
res_CARD_scale$x = as.numeric(location$x)
res_CARD_scale$y = as.numeric(location$y)
mData = melt(res_CARD_scale,id.vars = c("x","y"))
colnames(mData)[3] <- "Cell_Type"
b = c(0,1)
p = suppressMessages(ggplot(mData, aes(x, y)) + 
geom_point(aes(colour = value),size = pointSize) +
scale_color_gradientn(colours = colors) + 
#scale_color_viridis_c(option = 2)+
scale_x_discrete(expand = c(0, 1)) + scale_y_discrete(expand = c(0,1))+ 
facet_wrap(~Cell_Type,ncol = NumCols)+ 
theme(plot.margin = margin(0.1, 0.1, 0.1, 0.1, "cm"),
    #legend.position=c(0.14,0.76),
    panel.background = element_blank(),
    plot.background = element_blank(),
    panel.border = element_rect(colour = "grey89", fill=NA, size=0.5),
    axis.text =element_blank(),
    axis.ticks =element_blank(),
    axis.title =element_blank(),
    legend.title=element_text(size = 14,face="bold"),
    legend.text=element_text(size = 11),
    strip.text = element_text(size = 12,face="bold"),
    legend.key = element_rect(colour = "transparent", fill = "white"),
    legend.key.size = unit(0.45, 'cm')))
return(p)

And then follow the tutorial:

## select the cell type that we are interested, for example: 
ct.visualize = c("Endothelial")
## visualize the spatial distribution of the cell type proportion
p2 <- CARD.visualize.prop(
    proportion = CARD_obj@Proportion_CARD,        
    spatial_location = CARD_obj@spatial_location, 
    ct.visualize = ct.visualize,                 ### selected cell types to visualize
    colors = c("lightblue","lightyellow","red"), ### if not provide, we will use the default colors
    NumCols = 1,                                 ### number of columns in the figure panel
        pointSize = 3.0)                             ### point size in ggplot2 scatterplot  
print(p2)

##### you can save it as png or pdf by increasing the height, then it will not be squished.

Hope this helps.

Best, Ying

lvmt commented 2 weeks ago

hi

all you need like this

# for plot size  

options(repr.plot.width=10, repr.plot.height=10)