I'm using the Metafor package to plot forest plots and I was wondering if anyone knew a way to omit the upper and lower CIs from the text annotations, for example where one group is a reference and outcome is fixed at 1.
Here is an example:
library(metafor)
par(mar=c(5,4,1,2))
forest(x = c(1, 0.9, 1.1),
ci.ub = c(1, 0.98, 1.18),
ci.lb = c(1, 0.82, 1.02),
refline = 1)
I would like to remove [1.00, 1.00] from the Study 1 row only, leaving just 1.00.
The only possibility I can think of would be to plot something in white over the top of these, but this would be fiddly, and I have a large complex plot with many groups.
You could play around with the graph produced by the call to the forest function, removing the annotation on the right-hand side with the option annotate = FALSE. After that, you could opt to add your own text with the function text (with limited possibilities) like in the code below.
library(metafor)
par(mar=c(5,4,1,2))
forest(x = c(1, 0.9, 1.1),
ci.ub = c(1, 0.98, 1.18),
ci.lb = c(1, 0.82, 1.02),
refline = 1,
annotate = FALSE, ### added
)
text(x = c(1.25, 1.25, 1.25), y = c(3, 2, 1),
label=c("1.00", "0.90", "1.10"))
This yields the following graph:
Related
I'm trying to highlight only correlation values <-.7 and >0.7 in my correlation plot, but I'm getting trouble on it.
I have this data like this
Parn_Spt_comp_2
And I did a Pearson correlation with this code:
par_spt.pearson2 <-cor(Parn_Spt_comp_2[,3:14], method = "pearson")
par_spt.pearson2
After I plotted the values with the code below:
corrplot(par_spt.pearson2, method="color", addCoef.col = "black" ,type="lower", sig.level = 0.05,
number.cex = 1.2, tl.col = "black",tl.cex = 1.25, number.font = 1, tl.srt = 0, tl.offset = 0.3, pch=19, addgrid.col="black",col=colorRampPalette(c("white","coral3"))(10), cl.cex=1.25, mar = c(0,0,0,0))
And display it:
But I would like to show only correlation values in the interval mentioned above, and the values >-0.7 and <0.7 do not appear in the plot. I tried many things, but it still not working. I would be grateful if you help me.
Thank you in advance!
I am trying to use grid.arrange to combine multiple types of graph/table, one of which is a correlation matrix using corrplot. Is there a way to convert a corrplot to a grob or export/import as an image compatible with grid.arrange? Since the other plots I'm combining are from ggplot and tableGrob, I can't seem to use par(mfrow = c(2, 2)) or layout(matrix(1:2)) as suggested in other posts.
P1 <- corrplot(PANAcor, order="hclust", addgrid.col = "gray",
type="full", col = col2(50), tl.cex=1.5, tl.col="black",
method="color", tl.pos="lt", tl.srt=45, hclust.method = "average",
cl.ratio = 0.25, cl.align = "l", number.cex = 2)
summary <- grid.arrange(
top=textGrob(sprintf("%s Summary",subject), gp=gpar(fontsize=16,font=8)),
blank, P1, P2,
blank, T1, T2,
ncol=3, widths = c(0.1, 3, 3),
nrow=2, heights= c(1, 1),
bottom = textGrob(sprintf("%s run %s",version,runtime),
gp=gpar(fontsize=6,font=8), hjust=-1)
)
Error in gList(list(1, 0.45, 0.62, 0.55, 0.68, 0.64, -0.13, -0.37,
-0.22, : only 'grobs' allowed in "gList" In addition: Warning message: In grob$wrapvp <- vp : Coercing LHS to a list
Data:
PANAcor <- structure(c(1, 0.56, 0.68, -0.49, -0.4, -0.39, 0.56, 1, 0.64, -0.55,
-0.49, -0.54, 0.68, 0.64, 1, -0.69, -0.57, -0.65, -0.49,
-0.55, -0.69, 1, 0.82, 0.73, -0.4, -0.49, -0.57, 0.82, 1,
0.71, -0.39, -0.54, -0.65, 0.73, 0.71, 1),
.Dim = c(6L, 6L),
.Dimnames = list(c("Anxious", "Irritable", "Upset", "Happy",
"Enthusiastic", "Outgoing"),
c("Anxious", "Irritable", "Upset", "Happy",
"Enthusiastic", "Outgoing")))
col2 <- colorRampPalette(c("#7bffff","#7bbdff","#0000ff","black",
"#ff1a1a","#ff8000","#ffff4d"))
grid.echo + grid.grab from the gridGraphics package will convert a graphic drawn by corrplot into an identical-looking grob. Trouble is, the grob only looks identical at the exact same graphics device size.
Reproducing the problem:
library(gridGraphics)
library(grid)
corrplot(PANAcor, order="hclust", addgrid.col = "gray",
type="full", col = col2(50), tl.cex=1.5, tl.col="black",
method="color", tl.pos="lt", tl.srt=45, hclust.method = "average",
cl.ratio = 0.25, cl.align = "l", number.cex = 2)
## grab the scene as a grid object & save it to P1
grid.echo()
P1 <- grid.grab()
grid.draw(P1) # looks fine, until you resize the graphics device
Original size (looks identical to the graphic generated by corrplot:
Larger size (coloured regions remain squares, even though the matrix has extended to rectangular cells, & don't extend to the edge of each cell):
Smaller size (coloured regions have a minimum height / width, which cause them to spill out beyond the confines of each cell):
And if we arrange multiple grobs together, it's almost certainly going to look weird:
library(gridExtra)
grid.arrange(P1, P1, P1, layout_matrix = matrix(c(1, 1, 2, 3), nrow = 2, ncol = 2))
In short, due to the way corrplot draws the graphic, all other children grobs in P1 adjust in sync when the graphics device is re-sized, except for the grob responsible for colour.
Solution:
# save correlation matrix colors to a vector, then make coloured matrix grob transparent
matrix.colors <- getGrob(P1, gPath("square"), grep = TRUE)[["gp"]][["fill"]]
P1 <- editGrob(P1,
gPath("square"), grep = TRUE,
gp = gpar(col = NA,
fill = NA))
# apply the saved colours to the underlying matrix grob
P1 <- editGrob(P1,
gPath("symbols-rect-1"), grep = TRUE,
gp = gpar(fill = matrix.colors))
# convert the background fill from white to transparent, while we are at it
P1 <- editGrob(P1,
gPath("background"), grep = TRUE,
gp = gpar(fill = NA))
Replace gPath("square") with gPath("circle") if you use corrplot's default method. I haven't tested the other method options for the corresponding grob names, but the general principle should be similar.
Check that everything's aligned now:
grid.arrange(P1, P1, P1, layout_matrix = matrix(c(1, 1, 2, 3), nrow = 2, ncol = 2))
By the way, you may want to adjust the text size arguments in corrplot. Based on your current code, the labels appear rather large, and are liable to be cut off when you arrange multiple plots together.
How to change the labels of the values on the x-axis of a scatterPlot of the openair package?
And how to change the scale on this x-axis?
I am not aware of how this can be done in openair's scatterPlot function, as the typical way of doing so (outlined below) does not work on testing. However, if you are after a scatter plot for simple linear regression, for example, you can always use the plot() function, and apply the following modifications -
With the plot() function in R, this can be done using the xaxt= "n" argument and axis() function to create your own x-axis labels. The range can be changed with the xlim=c(min, max) argument. For example -
plot(x_vector, y_vector, xaxt = "n", xlim=c(0.02, 0.09))
axis(1, at = c(0.02, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09),
lab = expression (1, 2, 3, 4, 5, 6, 7, 8, 9))
The at = c(values) is the list of the default x-label values, The lab=expression(values) is where you give your value for each of the default values listed under at =c(values).
I have succeeded in creating and aligning three scatter-plots in R, using the following code:
par(mfrow = c(3,1))
plot(CGP.GOSL ~ FPT.MAF.GOSL, data = all.locs, main = "A. Place I")
abline(h=c(0.5))
abline(v=c(0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5), lty=2)
plot(CGP.IRE ~ FPT.MAF.IRE, data = all.locs, main = "B. Place II")
abline(h=c(0.5))
abline(v=c(0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5), lty=2)
plot(CGP.BAR ~ FPT.MAF.BAR, data = all.locs, main = "C. Place III")
abline(h=c(0.5))
abline(v=c(0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5), lty=2)
What I would like to do now is save space by having a single Axis label for the x and y axis. I have tried experimenting with the par() function, inserting x and ylab functions, but it seems that as these are not graphical parameters is will not accept them. I suspect the problem lies in where I place this information in the code, as using the xlab and ylab seems to make sense, and I can write x and ylab = "" in the individual plot codes.
I am also struggling to change the position of the main titles so that the appear on the left, to remove the values from the x-axis so that they only show at the bottom of the whole figure, and to arrange the figure so that there is less space.
This figure shows the current layout and the layout I want to achieve:
I am sorry to post so many questions at once. I am very new to R and programming am still finding the helpfiles a bit daunting, although I am getting there. Some suggestions on functions, where to put them and how to use them to achieve some of these aims would be great.
The documentation can be a bit challenging at times. Here's a skeleton for what I think you're looking for:
# 3 rows
par(mfrow=c(3,1))
# tighter margins
par(mar = c(0, 0, 0, 0), oma = c(4, 4, 0.5, 0.5))
# need some data
data(cars)
# 3 plots, no axis junk
plot(cars, ann=FALSE)
plot(cars, ann=FALSE)
plot(cars, ann=FALSE)
# outer labels
mtext("x axis", side = 1, outer = TRUE, cex = 0.7, line = 2.2)
mtext("y axis", side = 2, outer = TRUE, cex = 0.7, line = 2.2)
This answer is based on hrbrmstr's answer, but the result is closer to the requested layout:
# 3 rows
par(mfrow=c(3,1))
# Adjust margins. Each vector element refers to one side of the plot;
# the order is c(bottom, left, top, right). (See ?par)
par(mar = c(2.5, 4.1, 1, 2.1), oma = c(3, 3, 2, 0))
# need some data
data(cars)
# 3 plots. On the first two: Suppress axis labels (ann = FALSE) and
# the x axis (xaxt = "n"), then add the ticks using axis() and the
# title using mtext(). On the last one, do not suppress x axis.
# Note that repeating arguments could be set globally using par().
plot(cars, ann = FALSE, xaxt = "n")
axis(side = 1, labels = FALSE)
mtext(text = "A. Place I", side = 3, at = par("usr")[1], line = 1)
plot(cars, ann=FALSE, xaxt = "n")
axis(side = 1, labels = FALSE)
mtext(text = "B. Place II", side = 3, at = par("usr")[1], line = 1)
plot(cars, ann=FALSE)
mtext(text = "C. Place III", side = 3, at = par("usr")[1], line = 1)
# outer labels
mtext("X Axis label", side = 1, outer = TRUE)
mtext("Y Axis label", side = 2, outer = TRUE)
Given such data:
SN = Sensitivity;
SP = Specificity
Cutpoint SN 1-SP
1 0.5 0.1
2 0.7 0.2
3 0.9 0.6
How can i plot the ROC curve and calculate AUC. And compare the AUC between two different ROC curves. In the most of the packages such pROC or ROCR, the input of the data is different from those shown above. Can anybody suggest the way to solve this problem in R or by something else?
ROCsdat <- data.frame(cutpoint = c(5, 7, 9), TPR = c(0.56, 0.78, 0.91), FPR = c(0.01, 0.19, 0.58))
## plot version 1
op <- par(xaxs = "i", yaxs = "i")
plot(TPR ~ FPR, data = dat, xlim = c(0,1), ylim = c(0,1), type = "n")
with(dat, lines(c(0, FPR, 1), c(0, TPR, 1), type = "o", pch = 25, bg = "black"))
text(TPR ~ FPR, data = dat, pos = 3, labels = dat$cutpoint)
abline(0, 1)
par(op)
First off, I would recommend to visit your local library and find an introductory book on R. It is important to have a solid base before you can write your own code, and copy-pasting code found on the internet without really understanding what is means is risky at best.
Regarding your question, I believe the (0,0) and (1,1) cooordinates are part of the ROC curve so I included them in the data:
ROCsdat <- data.frame(cutpoint = c(-Inf, 5, 7, 9, Inf), TPR = c(0, 0.56, 0.78, 0.91, 1), FPR = c(0, 0.01, 0.19, 0.58, 1))
AUC
I strongly recommend against setting up your own trapezoid integration function at this stage of your training in R. It's too error-prone and easy to screw up with a small (syntax) mistake.
Instead, use a well established integration code like the trapz function in pracma:
library(pracma)
trapz(ROCsdat$FPR, ROCsdat$TPR)
Plotting
I think you mostly got the plotting, although I would write it slightly differently:
plot(TPR ~ FPR, data = ROCsdat, xlim = c(0,1), ylim = c(0,1), type="b", pch = 25, bg = "black")
text(TPR ~ FPR, data = ROCsdat, pos = 3, labels = ROCsdat$cutpoint)
abline(0, 1, col="lightgrey")
Comparison
For the comparison, let's say you have two AUCs in auc1 and auc2. The if/else syntax looks like this:
if (auc1 < auc2) {
cat("auc1 < auc2!\n")
} else if (auc1 == auc2) {
cat("aucs are identical!\n")
} else {
cat("auc1 > auc2!\n")
}
I suppose you could just compute it manually:
dat <- data.frame(tpr=c(0, .5, .7, .9, 1), fpr=c(0, .1, .2, .6, 1))
sum(diff(dat$fpr) * (dat$tpr[-1] + dat$tpr[-length(dat$tpr)]) / 2)
# [1] 0.785
You need to have the tpr and fpr vectors begin with 0 and end with 1 to compute the AUC properly.