I did a barplot as column annotation on a heatmap. I use ComplexHeatmap.
My input for annotation is:
vector_pvalues_adj <- c(0.3778364, 0.0001000, 0.2122000, 0.4174714, 0.3778364, 0.4799250, 0.1613250, 0.4861000, 0.4174714, 0.1008000, 0.0141000, 0.4174714, 0.0001000, 0.0018000, 0.4861000, 0.4799250, 0.0001000, 0.0001000)
And the code is:
library(ComplexHeatmap)
column_ha3 = HeatmapAnnotation("-log adj p-value"= anno_barplot(-log(vector_pvalues_adj)), gp = gpar(fill = "red"), height = unit(20, "mm"))
I would to add an abline
abline(h = -log(0.05), col= "red")
But I'm not able, could someone suggest to me how to do it?
In order to add something like a horizontal line, you can use the ComplexHeatmap function decorate_annotation and the grid package.
This adds a dashed line.
library(ComplexHeatmap) # BiocManager::install("ComplexHeatmap")
library(grid)
# original code from question
vector_pvalues_adj <- c(0.3778364, 0.0001000, 0.2122000, 0.4174714,
0.3778364, 0.4799250, 0.1613250, 0.4861000,
0.4174714, 0.1008000, 0.0141000, 0.4174714,
0.0001000, 0.0018000, 0.4861000, 0.4799250,
0.0001000, 0.0001000)
# original code from question
column_ha3 = HeatmapAnnotation(
"-log adj p-value" = anno_barplot(-log(vector_pvalues_adj)),
gp = gpar(fill = "red"), height = unit(20, "mm"))
# added for context
Heatmap(matrix(rnorm(18*18), 18), name = "mat", top_annotation = column_ha3)
# 'decorate' "-log adj p-value"
decorate_annotation("-log adj p-value", {
grid.lines(c(.5, 18.5), c(4, 4), gp = gpar(lty = 2, col = "red"),
default.units = "native")
})
In the last function, c(.5, 18.5) is the x-axis values, which is always equivalent to the number of columns, or 1:18 based on your data (using .5 get's you to the edges).
For y, I have it set to start at 4 and end at 4, so you get a horizontal line. in gpar I used lty = 2 which gave me a dashed line instead of the default, a solid line.)
Related
For grid practice purposes, I am trying to adjust the plot symbol. The idea is to connect min/max values with a vertical line, and give both symbols & line the same color fill without visible outline.
I've figured out most steps. My problem is with removing the symbol outline and changing the symbol.
library(grid)
n <- 10
mins <- 10*runif(n)
maxs <- mins + 5*runif(n)
grid.newpage()
pushViewport(plotViewport(c(5.1, 4.1, 4.1, 2.1)))
vp <- dataViewport( xData = 1:n , yData = c(mins,maxs) , name = "theRegion")
pushViewport(vp)
grid.rect()
grid.points(1:n,mins , gp = gpar(pch=2,col="blue",fill="blue"))
grid.edit("dataSymbols",pch=2)
# --------------------------------
# Error in editDLfromGPath(gPath, specs, strict, grep, global, redraw) :
# 'gPath' (dataSymbols) not found
# --------------------------------
grid.points(1:n,maxs, gp = gpar(pch=2,col="yellow"))
grid.xaxis()
grid.yaxis()
for(i in 1:n){
grid.lines(x = unit(c(i,i),"native"),
y = unit(c(mins[i],maxs[i]),"native"),
gp = gpar(col = "green",lwd=6))
}
First, a couple of issues:
1. pch is not a gpar parameter - move pch outside gpar.
2. pch=2 has a 'col' but does not have a 'fill'. A triangle-shaped symbol with both fill and col is pch=24.
3. If you want to edit a named grob, you need a grob with that name.
library(grid)
n <- 10
mins <- 10*runif(n)
maxs <- mins + 5*runif(n)
grid.newpage()
pushViewport(plotViewport(c(5.1, 4.1, 4.1, 2.1)))
vp <- dataViewport(xData = 1:n, yData = c(mins,maxs), name = "theRegion")
pushViewport(vp)
grid.rect()
# Symbols are triangles with blue border and yellow fill.
# Note the grob's name
grid.points(1:n, mins, pch = 24, gp = gpar(col = "blue", fill = "yellow"), name = "dataSymbols")
# Edit that grob so that the symbols do not have a border
grid.edit("dataSymbols", gp = gpar(col = NA))
# Edit that grob so that the symbol changes to pch = 2
grid.edit("dataSymbols", pch = 2)
# OOPS! The symbols have only a fill assigned, but pch = 2 does not have a fill
# So, give the symbols a blue border
grid.edit("dataSymbols", gp = gpar(col = "blue"))
I have a question regarding R plot.
The problem is that I have my graph in the centre of the plot and would like to remove unnecessary empty space around it.
Setting limits to the axes did not help.
my R code is below:
visualize_circle_arrows <- function(x) {
colors<- unlist(lapply(x$MeanOffsetLength,get_grey_color, max(x$MeanOffsetLength), min(x$MeanOffsetLength)), recursive = TRUE, use.names = TRUE)
library(graphics)
plot(x$IconCenterX, -x$IconCenterY, xlim=c(0, 1080), ylim=c(-1920, 0), asp=1, cex = .05, col = "blue", main="Offset vector for Thumb in warm",
xlab="X coordinates of the screen, px", ylab="Y coordinates of the screen, px")
radius <- rep(135,times=nrow(x))
symbols(x$IconCenterX, -x$IconCenterY, circles=radius, add=TRUE, inches=FALSE, bg=colors)
arrows(x$IconCenterX, -x$IconCenterY, x1 = x$MeanTouchX, y1 = -x$MeanTouchY, length = 0.04, angle = 25,
code = 2, col = "black", lty = par("lty"),
lwd = 2)
}
get_grey_color <- function(x,max=135, min=0) {
value <- ((x+10-min)/(max+10-min))
rgb(value, value, value)
}
visualize_circle_arrows(warm_thumb)
and the result of it is this graph below
my graph
Any help will be very much appreciated!
Thanks a lot!
p.s. snapshot of warmthumb object is here
warmthumb object
I'm using lsmip from lsmeans to plot my model,
library(lsmeans)
PhWs1 <- lsmip(GausNugget1, Photoperiod:Ws ~ Month,
ylab = "Observed log(number of leaves)", xlab = "Month",
main = "Interaction between Photoperiod and Water stress over the months (3 photoperiods)",
par.settings = list(fontsize = list(text = 15, points = 10)))
but I was not able to get a suggestion on the internet on how to handle the legend position, size, title, etc.
I used trellis.par.get() to see the parameters but I could not find the one related to my issue. As you can see from the graph, the legend should be "Photoperiod*Ws" but Ws is not visible.
I see two possibly complementing alternatives to approach this issue. The first would be to create a fully customized legend and pass it on to the key argument of xyplot (which lsmip is heavily based on). Here is an example taken from ?lsmip to clarify my point.
## default trellis point theme
trellis_points <- trellis.par.get("superpose.symbol")
## create customized key
key <- list(title = "Some legend title", # legend title
cex.title = 1.2,
x = .7, y = .9, # legend position
points = list(col = trellis_points$col[1:2], # points
pch = trellis_points$pch[1:2],
cex = 1.5),
text = list(c("A", "B"), cex = .9)) # text
## create results and extract lattice plot
d <- lsmip(warp.lm, wool ~ tension, plotit = FALSE,
main = "Some figure title", key = key)
p <- attr(d, "lattice")
p
As you can see, setting up a customized legend let's you modify all the different components of the legend - including labels, text and symbol sizes, legend spacing, etc. Have a deeper look at the key argument described in ?xyplot which describes the various modification options in detail.
Now, if you have a long legend title and you do not want to include the legend inside the plot area, you could also define separate viewports, thus allowing the legend to occupy more space at the right margin. Note the use of update to remove the initially created legend from p and the subsequent assembly of the single figure components using grid functionality.
## remove legend from figure
p <- update(p, legend = NULL)
## assemble figure incl. legend
library(grid)
png("plot.png", width = 14, height = 10, units = "cm", res = 300)
grid.newpage()
## add figure without legend
vp0 <- viewport(x = 0, y = 0, width = .75, height = 1,
just = c("left", "bottom"))
pushViewport(vp0)
print(p, newpage = FALSE)
## add legend
upViewport(0)
vp1 <- viewport(x = .7, y = 0, width = .3, height = 1,
just = c("left", "bottom"))
pushViewport(vp1)
draw.key(key, draw = TRUE)
dev.off()
I'm using base R plotting functions to produce a pie chart and I want to change the line thickness of the outlines of each pie segment. ?pie seems to indicate that I can add optional graphic parameters, but adding lwd= does not appear to work. Anyone have any clues as to how I might be able to do this. I'm not yet proficient in producing pie charts in ggplot, and would like to stick with base R plotting (if possible).
library(RColorBrewer)
x1 <- data.frame(V1 = c(200, 100)) ## generate data
row.names(x1) <- c("A", "B")
x1$pct <- round((x1$V1/sum(x1$V1))*100, 1)
lbls1 <- paste(row.names(x1), "-(",x1$pct, '%)', sep='') ## add some informative stuff
pie(x1$V1, labels=lbls1, col=tail(brewer.pal(3, 'PuBu'), n=2),
main=paste('My 3.1415'), cex=1.1, lwd= 3)
Notice lwd= does not increase line thickness like it would in other base plotting.
Anyone have any clues?
The call to polygon and lines within pie does not pass ... or lwd
...
polygon(c(P$x, 0), c(P$y, 0), density = density[i], angle = angle[i],
border = border[i], col = col[i], lty = lty[i])
P <- t2xy(mean(x[i + 0:1]))
lab <- as.character(labels[i])
if (!is.na(lab) && nzchar(lab)) {
lines(c(1, 1.05) * P$x, c(1, 1.05) * P$y)
....
You can get around this by setting par(lwd = 2) (or whatever) outside and prior to your call to pie
i.e.
# save original settings
opar <- par(no.readonly = TRUE)
par(lwd = 2)
pie(x1$V1, labels=lbls1, col=tail(brewer.pal(3, 'PuBu'), n=2),
main=paste('My 3.1415'), cex=1.1)
par(lwd = 3)
# reset to original
par(opar)
At the moment, the function inside pie that does the actual drawing is polygon and here is how it is called:
polygon(c(P$x, 0), c(P$y, 0), density = density[i], angle = angle[i],
border = border[i], col = col[i], lty = lty[i])
Notice there is no lwd argument and more critically no ... argument to accept arguments that might not have been hard coded.
Create a new pie2 function. First type pie, copy the code and make a few changes:
pie2 <-
function (x, labels = names(x), edges = 200, radius = 0.8, clockwise = FALSE,
init.angle = if (clockwise) 90 else 0, density = NULL, angle = 45,
col = NULL, border = NULL, lty = NULL, main = NULL, lwd=1,...)
{
................
polygon(c(P$x, 0), c(P$y, 0), density = density[i], angle = angle[i],
border = border[i], col = col[i], lty = lty[i], lwd=lwd )
.................
}
pie2(x1$V1, labels=lbls1, col=tail(brewer.pal(3, 'PuBu'), n=2),
main=paste('My 3.1415'), cex=1.1, lwd=5)
The following sample resembles my dataset:
require(randomForest)
alpha = c(1,2,3,4,5,6)
bravo = c(2,3,4,5,6,7)
charlie = c(2,6,5,3,5,6)
mydata = data.frame(alpha,bravo,charlie)
myrf = randomForest(alpha~bravo+charlie, data = mydata, importance = TRUE)
varImpPlot(myrf, type = 2)
I cannot seem to control the placement of the y-axis labels in varImpPlot. I have tried altering the plot parameters (e.g. mar, oma), with no success. I need the y-axis labels shifted to the left in order to produce a PDF with proper label placement.
How can I shift the y-axis labels to the left?
I tried to use adj parameter but it produces a bug. As varImpPlot , use dotchart behind, Here a version using lattice dotplot. Then you can customize you axs using scales parameters.
imp <- importance(myref, class = NULL, scale = TRUE, type = 2)
dotplot(imp, scales=list(y =list(cex=2,
at = c(1,2),
col='red',
rot =20,
axs='i') ,
x =list(cex=2,col='blue')) )
You can extract the data needed to construct the plot out of myref and construct a plot with ggplot. By doing so you have more freedom in tweaking the plot. Here are some examples
library(ggplot2)
str(myrf)
str(myrf$importance)
data <- as.data.frame(cbind(rownames(myrf$importance),round(myrf$importance[,"IncNodePurity"],1)))
colnames(data) <- c("Parameters","IncNodePurity")
data$IncNodePurity <- as.numeric(as.character(data$IncNodePurity))
Standard plot:
(p <- ggplot(data) + geom_point(aes(IncNodePurity,Parameters)))
Rotate y-axis labels:
(p1 <- p+ theme(axis.text.y = element_text(angle = 90, hjust = 1)))
Some more tweaking (also first plot shown here):
(p2 <- p1 + scale_x_continuous(limits=c(3,7),breaks=3:7) + theme(axis.title.y = element_blank()))
Plot that looks like the varImpPlot (second plot shown here) :
(p3 <- p2+ theme(panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
panel.grid.minor.y = element_blank(),
panel.grid.major.y = element_line(colour = 'gray', linetype = 'dashed'),
panel.background = element_rect(fill='white', colour='black')))
Saving to pdf is easy with ggplot:
ggsave("randomforestplot.pdf",p2)
or
ggsave("randomforestplot.png",p2)
p2
p3
Did I understood correctly, that you want to get texts charlie and bravo more left of the boundary of the plot? If so, here's one hack to archive this, based on the modification of the rownames used in plotting:
myrf = randomForest(alpha~bravo+charlie, data = mydata, importance = TRUE)
#add white spaces at the end of the rownames
rownames(myrf$importance)<-paste(rownames(myrf$importance), " ")
varImpPlot(myrf, type = 2)
The adj parameter in dotchart is fixed as 0 (align to right), so that cannot be changed without modifying the code of dotchart:
mtext(labs, side = 2, line = loffset, at = y, **adj = 0**, col = color,
las = 2, cex = cex, ...)
(from dotchart)
EDIT:
You can make another type of hack also. Take the code of dotchart, change the above line to
mtext(labs, side = 2, line = loffset, at = y, adj = adjust_ylab, col = color,
las = 2, cex = cex, ...)
Then add argument adjust_ylab to the argument list, and rename the function as for example dotchartHack. Now copy the code of varImpPlot, find the line which calls dotchart, change the function name to dotchartHack and add the argument adjust_ylab=adjust_ylab to function call, rename the function to varImpPlotHack and add adjust_ylab to this functions argument list. Now you can change the alignment of the charlie and bravo by changing the parameter adjust_ylab:
myrf = randomForest(alpha~bravo+charlie, data = mydata, importance = TRUE)
varImpPlotHack(myrf, type = 2,adjust_ylab=0.5)
From ?par:
The value of adj determines the way in which text strings are
justified in text, mtext and title. A value of 0 produces
left-justified text, 0.5 (the default) centered text and
right-justified text. (Any value in [0, 1] is allowed, and on most
devices values outside that interval will also work.)