qplot: Only graphing nodes below a threshold - r

I am trying to make a visual graph of a dissimilarity matrix. Using this site, I ran into the qgraph function from the package qgraph. Using the threshold flag, I am able to remove edges from my network above the supplied numerical value. This works beautifully, however, what if I only want to plot values below a certain threshold, not above?
For this, I came back to this site and read here: How to plot near-zero values with qgraph? to use the cut flag for this purpose. However, as the answer states, this flag will only "adjust the saturation so that everything above the cut point has the strongest color intensity, anything below the cut point, the saturation gets weaker."
What I would like to do is to plot only lines between the nodes that are below my cut value (or threshold), not anything else.
Here is some reproducible data:
Dist <- data.frame(Sample_1 = c(0.0, 0.245, 0.191, 0.78, 0.5),
Sample_2 = c(0.3, 0.0, 0.2, 0.99, 0.6),
Sample_3 = c(0.65, 0.45, 0.0, 0.05, 0.8),
Sample_4 = c(0.45, 0.06, 0.88, 0.0, 0.7),
Sample_5 = c(0.11, 0.79, 0.66, 0.37, 0.0),
row.names = c("Sample_1", "Sample_2", "Sample_3", "Sample_4", "Sample_5"))
Plotting the graph:
qgraph(Dist, layout = "circle", vsize = 5, color = c("cyan", "yellow", "pink", "green3", "gray"), labels = c("Sample_1", "Sample_2", "Sample_3", "Sample_4", "Sample_5"), label.cex = 3, cut = 0.2)
As you can see, anything above the cut = 0.2 is also plotted and darker.
I would like only values below the 0.2 threshold to be plotted. Is there any way to do this?
Thanks.

qgraph does not seems to have the ability to cut below a threshold, so we have to manipulate the input data.
Replacing values above the threshold to 0 or NA should do it. Using NA result in the same output but with a warning.
Dist <- data.frame(
Sample_1 = c(0.0, 0.245, 0.191, 0.78, 0.5),
Sample_2 = c(0.3, 0.0, 0.2, 0.99, 0.6),
Sample_3 = c(0.65, 0.45, 0.0, 0.05, 0.8),
Sample_4 = c(0.45, 0.06, 0.88, 0.0, 0.7),
Sample_5 = c(0.11, 0.79, 0.66, 0.37, 0.0),
row.names = c("Sample_1", "Sample_2", "Sample_3", "Sample_4", "Sample_5")
)
library(qgraph)
qgraph(
replace(Dist, Dist > 0.2, 0),
layout = "circle",
vsize = 5,
color = c("cyan", "yellow", "pink", "green3", "gray"),
labels = c("Sample_1", "Sample_2", "Sample_3", "Sample_4", "Sample_5"),
label.cex = 3
)
Created on 2020-04-06 by the reprex package (v0.3.0)

Related

Pie Chart using R

I want to create a piechart (showing Forest.Area.ha. by GaPa_NaPa) based on following attribute table as below:-
The dataframe for the data is as shown:-
structure(list(GaPa_NaPa = c("Gaidahawa", "Kanchan", "Kotahimai",
"Marchawari", "Mayadevi", "Omsatiya", "Rohini", "Sammarimai",
"Siyari", "Sudhdhodhan", "Devdaha", "Lumbini Sanskritik", "Sainamaina",
"Siddharthanagar", "Tillotama", "Butwal"), Total.Area..ha. = c(9657L,
5835L, 5812L, 4844L, 7228L, 4844L, 6449L, 5066L, 6620L, 5743L,
13667L, 11194L, 16082L, 3595L, 12592L, 10139L), Forest.Area.ha. = c(114.91,
178.19, 31.37, 43.43, 152.87, 29.12, 63.16, 59.81, 36.4, 16.42,
113.13, 422.87, 186.13, 167.2, 60.27, 45.3), Forest.Percent = c(6.67,
10.35, 1.83, 2.52, 8.88, 1.69, 3.67, 3.47, 2.11, 0.95, 6.57,
24.57, 10.81, 9.71, 3.5, 2.63), Forest.Area..Fraction. = c(0.07,
0.1, 0.02, 0.03, 0.09, 0.02, 0.04, 0.03, 0.02, 0.01, 0.07, 0.25,
0.11, 0.1, 0.04, 0.03), Household.No = c(8612L, 9828L, 5939L,
5305L, 8003L, 6683L, 6349L, 5164L, 7889L, 7619L, 15624L, 10736L,
17572L, 12329L, 30452L, 36989L), Family.Size = c(10020L, 10483L,
7921L, 6972L, 10040L, 8218L, 8096L, 7303L, 9060L, 8717L, 17582L,
13854L, 19657L, 16011L, 36399L, 51099L), Total = c(56529L, 42528L,
46417L, 41058L, 57341L, 41080L, 43277L, 43300L, 45274L, 41472L,
71806L, 88090L, 78477L, 76307L, 149657L, 195054L)), row.names = c(NA,
16L), class = "data.frame")
The code I used is:-
setwd("C:/Users/lenovo/Desktop/AllAboutR/AssignmentDocs")
ForestArea2010<-read.csv("Forest2010.csv")
View(ForestArea2010)
pie(RupandehiLULC19$GaPa_NaPa, main="Piechart of Forest Area", las=3, col=hsv(12))
But I couldn't work further on how to show piechart showing Forest.Area.ha. by GaPa_NaPa working on code to plot piechart. Please help on it. How the code must be written?
Here is another option with ggplot2. If you don't want the labels then the geom_text_repel line can be removed.
library(ggplot2)
library(ggrepel)
ggplot(RupandehiLULC19, aes(x = "", y = `Forest.Area.ha.`, fill = GaPa_NaPa)) +
geom_bar(width = 1, stat = "identity") +
coord_polar("y", start = 0) +
xlab("") +
ylab("Piechart of Forest Area") +
theme(axis.text.x = element_blank(), axis.ticks.x = element_blank()) +
geom_text_repel(aes(label = `Forest.Area.ha.`),
position = position_stack(vjust = 0.5))
Output
Or another option using legend:
pie(RupandehiLULC19$Forest.Area.ha., labels = "", main="Piechart of Forest Area", las=3, col=palette(rainbow(16)))
legend(.85, 1.1, RupandehiLULC19$GaPa_NaPa, cex = 0.7, fill = palette(rainbow(16)), box.col = "white",bg = "white")
Or with the values:
pie(RupandehiLULC19$Forest.Area.ha., labels = RupandehiLULC19$Forest.Area.ha., main="Piechart of Forest Area", las=3, col=palette(rainbow(16)))
legend(.85, 1.1, RupandehiLULC19$GaPa_NaPa, cex = 0.7, fill = palette(rainbow(16)), box.col = "white",bg = "white")

Change font of specific rows to bold in forestplot

I wrote a script using the "forestplot" package. I want to group the variables in certain categories, which I would like to show in bold, in order to accentuate those categories. How can i adjust my script, so that only certain rows, i.e Risk factor OR (95% CI), patient characteristics, medication history, comorbidities, surgical history and other are shown in bold? I have two colums and 18 rows. Can someone help me? I would be much grateful!!
My script is as below:
tabletext <- cbind(
c("Risk factor" ,"Patient characteristics","Sex, male*", "Bmi (5 points)",
"Alcohol (5 units)", "Smoking*","Medication history",
"Steroid use", "Anticoagulant use*","Comorbidities",
"COPD GOLD 1/2", "COPD GOLD 3/4", "Other pulmonary disease",
"Surgical history",
"Previous colorectal surgery*",
"Previous abdominal surgery (other)","Other", "HIPEC*"),
c("OR (95% CI)",NA, "1.78 (1.20-2.68)", "1.15 (0.95-1.38)", "1.04 (0.94-1.14)",
"1.78 (1.11-2.80)", NA," 1.40 (0.68-2.67)", "1.55 (1.02-2.32)",NA,
"1.40 (0.70-2.61)", "1.56 (0.42-4.67)", "1.78 (0.63-4.28)",NA,
"1.61 (1.03-2.49)", "0.80 (0.47-1.32)",NA, "4.14 (2.14-7.73)"))
?fpTxtGp
require(forestplot)
forestplot(tabletext,
txt_gp = fpTxtGp(label = list(gpar(fontfamily = "Times",
fontface="bold"),
gpar(fontfamily = "",
col = "black"))),
df_c,new_page = TRUE,
boxsize = 0.2,
is.summary = c(rep(FALSE,32)),
clip = c(0,17),
xlab = 'Odds ratio with 95% confidence interval
* indicates significance',
xlog = FALSE,
zero = 1,
plotwidth=unit(12, "cm"),
colgap=unit(2, "mm"),
col = fpColors(box = "royalblue",
line = "darkblue",
summary = "royalblue"))
Its not clear what df_c is so I just created it based on your tabletext matrix:
df_c <- data.frame(mean = c(NA, NA, 1.78, 1.15, 1.04, 1.78, NA, 1.4, 1.55,
NA, 1.4, 1.56, 1.78, NA, 1.61, 0.8, NA, 4.14),
lower = c(NA, NA, 1.2, 0.95, 0.94, 1.11, NA, 0.68, 1.02, NA, 0.7,
0.42, 0.63, NA, 1.03, 0.47, NA, 2.14),
upper = c(NA, NA, 2.68, 1.38,1.14, 2.8, NA, 2.67,2.32, NA,
2.61, 4.67, 4.28, NA, 2.49, 1.32, NA, 7.73))
From there, its just a matter of adjusting the values passed to is.summary:
forestplot(tabletext,
txt_gp = fpTxtGp(label = list(gpar(fontfamily = "Times"),
gpar(fontfamily = "",
col = "black"))),
df_c,new_page = TRUE,
boxsize = 0.2,
is.summary = c(TRUE, TRUE, rep(FALSE, 4),
TRUE, FALSE, FALSE, TRUE,
rep(FALSE,3), TRUE, rep(FALSE,4)),
clip = c(0,17),
xlab = 'Odds ratio with 95% confidence interval
* indicates significance',
xlog = FALSE,
zero = 1,
plotwidth=unit(12, "cm"),
colgap=unit(2, "mm"),
col = fpColors(box = "royalblue",
line = "darkblue",
summary = "royalblue"))
Which generates the following figure:

Complex clipping (spatial intersection ?) of polygons and lines in R

I would like to clip (or maybe the right formulation is performing spatial intersection) polygons and lines using a polygon rather than a rectangle, like so:
Here is some code to make the polygons for reproducibility and examples:
p1 <- data.frame(x = c(-0.81, -0.45, -0.04, 0.32, 0.47, 0.86, 0.08, -0.46, -1, -0.76),
y = c(0.46, 1, 0.64, 0.99, -0.04, -0.14, -0.84, -0.24, -0.44, 0.12))
p2 <- data.frame(x = c(-0.63, -0.45, -0.2, -0.38, -0.26, -0.82, -0.57, -0.76),
y = c(-0.1, 0.15, -0.17, -0.79, -1, -0.97, -0.7, -0.61))
l1 <- data.frame(x = c(0.1, 0.28, 0.29, 0.52, 0.51, 0.9, 1),
y = c(0.19, -0.15, 0.25, 0.28, 0.64, 0.9, 0.47))
plot.new()
plot.window(xlim = c(-1, 1), ylim = c(-1,1))
polygon(p2$x, p2$y, col = "blue")
polygon(p1$x, p1$y)
lines(l1$x, l1$y)
You could use the spatstat package for this. Below the original example is
worked through. In spatstat polygons are used as “observation windows” of
point patterns, so they are of class owin. It is possible to do set
intersection, union etc. with owin objects.
p1 <- data.frame(x = c(-0.81, -0.45, -0.04, 0.32, 0.47, 0.86, 0.08, -0.46, -1, -0.76),
y = c(0.46, 1, 0.64, 0.99, -0.04, -0.14, -0.84, -0.24, -0.44, 0.12))
p2 <- data.frame(x = c(-0.63, -0.45, -0.2, -0.38, -0.26, -0.82, -0.57, -0.76),
y = c(-0.1, 0.15, -0.17, -0.79, -1, -0.97, -0.7, -0.61))
l1 <- data.frame(x = c(0.1, 0.28, 0.29, 0.52, 0.51, 0.9, 1),
y = c(0.19, -0.15, 0.25, 0.28, 0.64, 0.9, 0.47))
In spatstat polygons must be traversed anti-clockwise, so:
library(spatstat)
p1rev <- lapply(p1, rev)
p2rev <- lapply(p2, rev)
W1 <- owin(poly = p1rev)
W2 <- owin(poly = p2rev)
L1 <- psp(x0 = l1$x[-nrow(l1)], y0 = l1$y[-nrow(l1)],
x1 = l1$x[-1], y1 = l1$y[-1], window = boundingbox(l1))
plot(boundingbox(W1,W2,L1), type= "n", main = "Original")
plot(W2, col = "blue", add = TRUE)
plot(W1, add = TRUE)
plot(L1, add = TRUE)
W2clip <- W2[W1]
L1clip <- L1[W1]
plot(W1, main = "Clipped")
plot(W2clip, col = "blue", add = TRUE)
plot(L1clip, add = TRUE)

R package "forestplot": Error message re: identifying the upper/lower boundaries from the input matrix

I'd be grateful for your help. I'm using the forestplot R package to create a forest plot. It was going well.
library(forestplot)
test_data <- data.frame(
coef = c(1.53, 1.56, 1.11, 1.35, 1.00, 0.94, 1.03),
low = c(1.43, 1.38, 1.08, 1.05, 0.91, 0.87, 0.95),
high = c(1.64, 1.76, 1.15, 1.75, 1.10, 1.01, 1.11),
boxsize = c(0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1)
)
row_names <- cbind(
c("Variable", "BMI", "WHR", "Diabetes", "Insulin resistance", "LDL", "HDL", "ApoB"),
c("SNPs", "807", "368", "386", "53", "213", "510", "241"),
c("OR (95% CI)", "1·53 (1·43−1·64)", "1·56 (1·38−1·76)", "1·11 (1·08−1·15)", "1·35 (1·05−1·75)", "1·00 (0·91−1·10)", "0·94 (0·87−1·01)", "1·03 (0·95−1·11)"),
c("p-value", "6·72E−35", "2·90E−12", "6·61E−10", "0·02", "0·97", "0·083", "0·53")
)
test_data <- rbind(rep(NA, 3), test_data)
forestplot(
labeltext = row_names,
xlab = "Odds ratio",
test_data[, c("coef", "low", "high")],
is.summary = FALSE,
boxsize = test_data$boxsize,
zero = 1,
cex = 0.1,
align = TRUE,
xlog = FALSE,
graphwidth = unit(1.5, "snpc"),
col = fpColors(lines = "royalblue", box = "darkblue")
)
I've been adding one variable at a time and plotting my graph after the addition of each to make sure there were no errors introduced at each step. It was all going well until I added my eighth variable, "Triglycerides", and its associated numbers:
library(forestplot)
test_data <- data.frame(
coef = c(1.53, 1.56, 1.11, 1.35, 1.00, 0.94, 1.03, 1.15),
low = c(1.43, 1.38, 1.08, 1.05, 0.91, 0.87, 0.95, 1.07),
high = c(1.64, 1.76, 1.15, 1.75, 1.10, 1.01, 1.11, 1.11),
boxsize = c(0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1)
)
row_names <- cbind(
c("Variable", "BMI", "WHR", "Diabetes", "Insulin resistance", "LDL", "HDL", "ApoB", "Triglycerides"),
c("SNPs", "807", "368", "386", "53", "213", "510", "241", "427"),
c("OR (95% CI)", "1·53 (1·43−1·64)", "1·56 (1·38−1·76)", "1·11 (1·08−1·15)", "1·35 (1·05−1·75)", "1·00 (0·91−1·10)", "0·94 (0·87−1·01)", "1·03 (0·95−1·11)", "1·15 (1·07−1·11)"),
c("p-value", "6·72E−35", "2·90E−12", "6·61E−10", "0·02", "0·97", "0·083", "0·53", "2·14E−4")
)
test_data <- rbind(rep(NA, 3), test_data)
forestplot(
labeltext = row_names,
xlab = "Odds ratio",
test_data[, c("coef", "low", "high")],
is.summary = FALSE,
boxsize = test_data$boxsize,
zero = 1,
cex = 0.1,
align = TRUE,
xlog = FALSE,
graphwidth = unit(1.5, "snpc"),
col = fpColors(lines = "royalblue", box = "darkblue")
)
On adding this eighth variable, I now receive the following error message:
Error in prFpConvertMultidimArray(mean) :
Sorry did not manage to correctly identify the upper/lower boundaries from the input matrix.
I can't spot a syntax error here, and I'm really at a loss as to why this has only just happened. Any advice would be greatly appreciated.
Thank you.

R - display ≥ / more or equal on a forestplot

I prepared the code for forestplot, however I have problem with exporting plot with "≥" / more or equal sign.
library(forestplot)
names6 <- c("Variable",
"A ≥ 4000***",
"B ≥ 50***",
"C**",
"D",
"E***",
"F",
"G*",
"H**",
"I*",
"J***")
coef6 <- c(0.42, 1.58, 1.35, 0.49,
0.46, 0.66, 0.62, 1.34, 0.52, 0.72)
low6 <- c(0.34, 1.29, 1.08, 0.21,
0.33, 0.44, 0.43, 1.08, 0.29, 0.61)
high6 <- c(0.51, 1.93, 1.69, 1.21,
0.64, 0.99, 0.91, 1.66, 0.92, 0.86)
boxsize6 <-c(0.2, 0.2, 0.2,0.2,
0.2,0.2, 0.2,0.2,
0.2, 0.2)
test_data <- data.frame(coef=coef6,
low=low6,
high=high6,
boxsize=boxsize6)
row_names <- cbind(names6,
c("OR",test_data$coef), c("CI-95%", test_data$low), c("CI+95%", test_data$high) )
test_data <- rbind(NA, test_data)
#####FIGURE
forestplot(labeltext = row_names,
mean = test_data$coef, upper = test_data$high,
lower = test_data$low,
is.summary=c(TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE),
boxsize = test_data$boxsize,
zero = 1,
xlog = FALSE,
xlab = "OR (95% CI)",
col = fpColors(lines="black", box="black"),
ci.vertices = TRUE,
xticks = c(0,0.5, 1, 1.5, 2.0),
colgap = unit(0.03,'npc'),
lineheight = unit(1.1,"cm"),
txt_gp=fpTxtGp(label = gpar(cex = 0.8),
title = gpar(cex = 1),
ticks = gpar(cex = 0.6),
xlab = gpar(cex = 0.7)))
However I cannot export plot as a .pdf file with present "≥" / more or equal.
Instead of this, I got sign "=" / equal.
What should I change to get this sign on plot?
Edit:
\u2265 do not work...
This solution might be system-specific. Here is something that works on windows:
library("forestplot")
library("withr")
names6 <- c("Variable",
"A \u2265 4000***",
"B \u2265 50***",
"C**",
"D",
"E***",
"F",
"G*",
"H**",
"I*",
"J***")
### Data & params code here....
with_cairo_pdf('forestplot.pdf',
### Forest plot code here....
forestplot(labeltext = row_names,
....)
)

Resources