Lolliplot

Lolliplot is for the visualization of the methylation/variant/mutation data.

library(trackViewer)
SNP <- c(10, 12, 1400, 1402)
sample.gr <- GRanges("chr1", IRanges(SNP, width=1, names=paste0("snp", SNP)))
features <- GRanges("chr1", IRanges(c(1, 501, 1001), 
                                    width=c(120, 400, 405),
                                    names=paste0("block", 1:3)))
lolliplot(sample.gr, features)

## More SNPs
SNP <- c(10, 100, 105, 108, 400, 410, 420, 600, 700, 805, 840, 1400, 1402)
sample.gr <- GRanges("chr1", IRanges(SNP, width=1, names=paste0("snp", SNP)))
lolliplot(sample.gr, features)

## Define the range
lolliplot(sample.gr, unname(features), ranges = GRanges("chr1", IRanges(104, 109)))

Change the lolliplot color

Change the color of the features.

features$fill <- c("#FF8833", "#51C6E6", "#DFA32D")
lolliplot(sample.gr, features)

Change the color and opacity of the lollipop.

sample.gr$color <- sample.int(6, length(SNP), replace=TRUE)
sample.gr$border <- sample(c("gray80", "gray30"), length(SNP), replace=TRUE)
sample.gr$alpha <- sample(100:255, length(SNP), replace = TRUE)/255
lolliplot(sample.gr, features)

Add the index labels in the node

Users can control the node labels one by one by setting metadata start with ‘node.label.’ Please note that for each node label, the node.label.gp must be a list to control the style of node labels. You can also simply use node.label.col, node.label.cex, node.label.fontsize, node.label.fontfamily, node.label.fontface, node.label.font to assign the node labels attributes.

sample.gr$node.label <- as.character(seq_along(sample.gr))
sample.gr$node.label.col <- 
  ifelse(sample.gr$alpha>0.5 | sample.gr$color==1, "white", "black")
sample.gr$node.label.cex <- sample.int(3, length(sample.gr), replace = TRUE)/2
lolliplot(sample.gr, features)

sample.gr$node.label.cex <- 1 ## change it back for pretty showcase.

Change the height of the features

features$height <- c(0.02, 0.05, 0.08)
lolliplot(sample.gr, features)

## Specifying the height and its unit
features$height <- list(unit(1/16, "inches"),
                        unit(3, "mm"),
                        unit(12, "points"))
lolliplot(sample.gr, features)

Plot multiple transcripts in the features

The metadata ‘featureLayerID’ are used for drawing features in different layers.

features.mul <- rep(features, 2)
features.mul$height[4:6] <- list(unit(1/8, "inches"),
                                 unit(0.5, "lines"),
                                 unit(.2, "char"))
features.mul$fill <- c("#FF8833", "#F9712A", "#DFA32D", 
                       "#51C6E6", "#009DDA", "#4B9CDF")
end(features.mul)[5] <- end(features.mul[5])+50
features.mul$featureLayerID <- 
    paste("tx", rep(1:2, each=length(features)), sep="_")
names(features.mul) <- 
    paste(features.mul$featureLayerID, 
          rep(1:length(features), 2), sep="_")
lolliplot(sample.gr, features.mul)

## One name per transcript
names(features.mul) <- features.mul$featureLayerID
lolliplot(sample.gr, features.mul)

Change the height of a lollipop plot

#Note: the score value is an integer less than 10
sample.gr$score <- sample.int(5, length(sample.gr), replace = TRUE)
lolliplot(sample.gr, features)

##Remove y-axis
lolliplot(sample.gr, features, yaxis=FALSE)

#Try a score value greater than 10
sample.gr$score <- sample.int(15, length(sample.gr), replace=TRUE)
sample.gr$node.label <- as.character(sample.gr$score)
lolliplot(sample.gr, features)

#increase the cutoff value of style switch.
lolliplot(sample.gr, features, lollipop_style_switch_limit=15)

#Try a float numeric score
sample.gr$score <- runif(length(sample.gr))*10
sample.gr$node.label <- as.character(round(sample.gr$score, digits = 1))
lolliplot(sample.gr, features)

# Score should not be smaller than 1
# remove the alpha for following samples
sample.gr$alpha <- NULL

Customize the x-axis label position

xaxis <- c(1, 200, 400, 701, 1000, 1200, 1402) ## define the position
lolliplot(sample.gr, features, xaxis=xaxis)

names(xaxis) <- xaxis # define the labels
names(xaxis)[4] <- "center" 
lolliplot(sample.gr, features, xaxis=xaxis)

Customize the y-axis label position

#yaxis <- c(0, 5) ## define the position
#lolliplot(sample.gr, features, yaxis=yaxis)
yaxis <- c(0, 5, 10, 15) ## define the position
names(yaxis) <- yaxis # define the labels
names(yaxis)[3] <- "y-axis" 
lolliplot(sample.gr, features, yaxis=yaxis)

Jitter the label

sample.gr$dashline.col <- sample.gr$color
lolliplot(sample.gr, features, jitter="label")

Add a legend

legend <- 1:6 ## legend fill color
names(legend) <- paste0("legend", letters[1:6]) ## legend labels
lolliplot(sample.gr, features, legend=legend)

## use list to define more attributes. see ?grid::gpar to get more details.
legend <- list(labels=paste0("legend", LETTERS[1:6]), 
               col=palette()[6:1], 
               fill=palette()[legend])
lolliplot(sample.gr, features, legend=legend)

## if you have multiple tracks, please try to set the legend by list.
## see more examples in the section [Plot multiple samples](#plot-multiple-samples)
legendList <- list(legend)
lolliplot(sample.gr, features, legend=legendList)

# from version 1.21.8, users can also try to set legend 
# as a column name in the metadata of GRanges.
sample.gr.newlegend <- sample.gr
sample.gr.newlegend$legend <- LETTERS[sample.gr$color]
lolliplot(sample.gr.newlegend, features, legend="legend")

# from version 1.41.6, users can set the legend position to right
lolliplot(sample.gr, features, legend=legend,
          legendPosition = list(position='right',
                                width=unit(1, 'inch')))

## use ncol or nrow to control the legend layout.
legendList[[1]]$ncol <- 2 # if legend is not a list of list, use legend$ncol <- 2
lolliplot(sample.gr, features, legend=legendList, legendPosition = 'right')

Control the labels

Users can control the parameters of labels by naming the metadata start as label.parameter such as label.parameter.rot or label.parameter.gp. Note: label.parameter.label can be used to plot snp labels other than the names of inputs. The parameter is used for grid.text.

sample.gr.rot <- sample.gr
sample.gr.rot$label.parameter.rot <- 45
lolliplot(sample.gr.rot, features, legend=legend)

sample.gr.rot$label.parameter.rot <- 60
sample.gr.rot$label.parameter.col <- "brown"
## change the label text into user-defined names other than names of the sample.gr
sample.gr.rot$label.parameter.label <- names(sample.gr)
random_ids <- sample(seq_along(sample.gr), 5)
sample.gr.rot$label.parameter.label[random_ids] <- 
  paste("new label", random_ids)
random_ids <- sample(seq_along(sample.gr), 2)
sample.gr.rot$label.parameter.label[random_ids] <- NA ## remove some labels
lolliplot(sample.gr.rot, features, legend=legend)

## try different colors
sample.gr.rot$label.parameter.col <- sample.int(7,
                                                length(sample.gr),
                                                replace = TRUE)
sample.gr.rot$label.parameter.draw <- TRUE
sample.gr.rot$label.parameter.draw[[1]] <- FALSE ## another method to remove the first label
lolliplot(sample.gr.rot, features, legend=legend)

Users can also control the labels one by one by setting label.parameter.gp. Please note that for each label, the label.parameter.gp must be a list.

label.parameter.gp.brown <- gpar(col="brown")
label.parameter.gp.blue <- gpar(col="blue")
label.parameter.gp.red <- gpar(col="red")
sample.gr$label.parameter.gp <- sample(list(label.parameter.gp.blue,
                                            label.parameter.gp.brown,
                                            label.parameter.gp.red),
                                       length(sample.gr), replace = TRUE)
lolliplot(sample.gr, features)

User can write the labels of the features directly on them and not in the legend by set the parameter label_on_feature to TRUE.

lolliplot(sample.gr, features, label_on_feature=TRUE)

Please note that lolliplot does not support any parameters to set the title and xlab. If you want to add the title and xlab, please try to add them by grid.text.

lolliplot(sample.gr.rot, features, legend=legend, ylab="y label here")
grid.text("label of x-axis here", x=.5, y=.01, just="bottom")
grid.text("title here", x=.5, y=.98, just="top", 
          gp=gpar(cex=1.5, fontface="bold"))

Start from version 1.33.3, lolliplot can also plot motifs as labels. The parameters are controlled by the parameters of labels by naming the metadata start as label.parameter such as label.parameter.pfm or label.parameter.font. The parameter is used for plotMotifLogoA.

library(motifStack)
pcms<-readPCM(file.path(find.package("motifStack"), "extdata"),"pcm$")
sample.gr.rot$label.parameter.pfm <- pcms[sample(seq_along(pcms),
                                                 length(sample.gr.rot),
                                                 replace = TRUE)]
lolliplot(sample.gr.rot, features, legend=legend)

Change the lolliplot type

Change the shape for “circle” plot

## shape must be "circle", "square", "diamond", "triangle_point_up", or "triangle_point_down"
available.shapes <- c("circle", "square", "diamond", 
                      "triangle_point_up", "triangle_point_down")
sample.gr$shape <- sample(available.shapes, size = length(sample.gr), replace = TRUE)
sample.gr$legend <- paste0("legend", as.numeric(factor(sample.gr$shape)))
lolliplot(sample.gr, features, type="circle", legend = "legend")

sample.gr.mul.shape <- sample.gr
sample.gr.mul.shape$score <- ceiling(sample.gr.mul.shape$score)
sample.gr.mul.shape$shape <- lapply(sample.gr.mul.shape$score, function(s){
  sample(available.shapes, size = s, replace = TRUE)
})
sample.gr.mul.shape$color <- lapply(sample.gr.mul.shape$score, function(s){
  sample.int(7, size = s, replace = TRUE)
})
lolliplot(sample.gr.mul.shape, features, type="circle",
          lollipop_style_switch_limit = max(sample.gr.mul.shape$score))

Google pin

lolliplot(sample.gr, features, type="pin")

sample.gr$color <- lapply(sample.gr$color, function(.ele) c(.ele, sample.int(6, 1)))
sample.gr$border <- sample.int(6, length(SNP), replace=TRUE)
lolliplot(sample.gr, features, type="pin")