library(ggcyto)
data(GvHD)
fs <- GvHD[subset(pData(GvHD), Patient %in%5:7 & Visit %in% c(5:6))[["name"]]]
fr <- fs[[1]]
ggcyto
wrapper will construct the ggcyto
object that inherits from ggplot
class.
p <- ggcyto(fs, aes(x = `FSC-H`))
class(p)
## [1] "ggcyto_flowSet"
## attr(,"package")
## [1] "ggcyto"
is(p, "ggplot")
## [1] TRUE
Since only one dimension is specified, we can add any 1d geom layer
p1 <- p + geom_histogram()
p1
As shown, data is facetted by samples name automatically (i.e facet_wrap(~name)
).
We can overwrite the default faceting by any variables that are defined in pData
pData(fs)
## Patient Visit Days Grade name
## s5a05 5 5 19 3 s5a05
## s5a06 5 6 26 3 s5a06
## s6a05 6 5 19 3 s6a05
## s6a06 6 6 27 3 s6a06
## s7a05 7 5 21 3 s7a05
## s7a06 7 6 28 3 s7a06
p1 + facet_grid(Patient~Visit)
To display 1d density
p + geom_density()
Fill the same color
p + geom_density(fill = "black")
Fill different colors
ggcyto(fs, aes(x = `FSC-H`, fill = name)) + geom_density(alpha = 0.2)
Or plot in the same panel by using ggplot
directly (thus removing the default facetting effect)
ggplot(fs, aes(x = `FSC-H`, fill = name)) + geom_density(alpha = 0.2)
# 2d hex
p <- ggcyto(fs, aes(x = `FSC-H`, y = `SSC-H`))
p <- p + geom_hex(bins = 128)
p
A default scale_fill_gradientn
is applied to 2d hexbin plot.
To add limits
p <- p + ylim(c(10,9e2)) + xlim(c(10,9e2))
p
To overwrite the default fill gradien
p + scale_fill_gradientn(colours = rainbow(7), trans = "sqrt")
p + scale_fill_gradient(trans = "sqrt", low = "gray", high = "black")
geom_gate
and geom_stats
layersFirstly we create the polygonGate
with a data-driven method provided by flowStats
package.
# estimate a lymphGate
lg <- flowStats::lymphGate(fr, channels=c("FSC-H", "SSC-H"),scale=0.6)
norm.filter <- lg$n2gate
#fit norm2 filter to multiple samples
fres <- filter(fs, norm.filter)
#extract the polygonGate for each sample
poly.gates <- lapply(fres, function(res)flowViz:::norm2Polygon(filterDetails(res, "defaultLymphGate")))
poly.gates
## $s5a05
## Polygonal gate 'defaultPolygonGate' with 50 vertices in dimensions FSC-H and SSC-H
##
## $s5a06
## Polygonal gate 'defaultPolygonGate' with 50 vertices in dimensions FSC-H and SSC-H
##
## $s6a05
## Polygonal gate 'defaultPolygonGate' with 50 vertices in dimensions FSC-H and SSC-H
##
## $s6a06
## Polygonal gate 'defaultPolygonGate' with 50 vertices in dimensions FSC-H and SSC-H
##
## $s7a05
## Polygonal gate 'defaultPolygonGate' with 50 vertices in dimensions FSC-H and SSC-H
##
## $s7a06
## Polygonal gate 'defaultPolygonGate' with 50 vertices in dimensions FSC-H and SSC-H
Then pass the gates to the gate layer
p + geom_gate(poly.gates)