X-AXIS BREAK IN FORESTPLOT PACKAGE - break

I have created a graph using Forestplot package. The r-script is following:
forestplot(row_names, OR, low, high,
txt_gp=fpTxtGp(label=list(gpar(fontfamily="Arial")),
ticks=gpar(cex=1.1),
xlab=gpar(cex = 1.2,fontface="bold")),
is.summary =
c(TRUE,FALSE,TRUE,rep(FALSE,3),TRUE,rep(FALSE,3),TRUE,rep(FALSE,3),TRUE,rep(FALSE,3),TRUE,
rep(FALSE,3),TRUE,rep(FALSE,3),TRUE,rep(FALSE,3),TRUE,rep(FALSE,3),TRUE,rep(FALSE,3),TRUE,
rep(FALSE,3),TRUE,rep(FALSE,3),TRUE,
rep(FALSE,49)),
legend=c("Females", "Males"),
fn.ci_norm=rep(c("fpDrawNormalCI", "fpDrawCircleCI")),
xlab="Chronic phlegm",
xticks= c(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24),
col=fpColors(box="black", lines="black", zero = "black"),
zero=1, lineheight = "auto", boxsize=0.4, colgap=unit(10,"mm"),
lwd.ci=1, ci.vertices=TRUE, ci.vertices.height = 0.2, cex=1,clip=c(0.95,1.5),
new_page = TRUE)
I would like to shorten the x-axis by inserting a break at points 8 to 23.

Related

Labeling contour lines in R using metR with gratia or ggplot2

Some ways to add labels on contour plots
# load packages
library('mgcv')
library('gratia') # draw(); smooth_estimates()
library('metR') # geom_contour2(); geom_text_contour()
library('ggplot2')
Simulate data using the example from Gavin Simpson's website: https://fromthebottomoftheheap.net/2018/10/23/introducing-gratia/
set.seed(1)
dat <- gamSim(2, n = 4000, dist = "normal", scale = 1, verbose = FALSE)
mod <- gam(y ~ s(x, z, k = 30), data = dat$data, method = "REML")
sm <- smooth_estimates(mod); sm
Plot using gratia with the number of contour lines automatically adjusted:
draw(mod) +
geom_text_contour(
aes(z = est), # 'est' from smooth_estimates(mod)
colour = "black", size = 4.5, fontface = "bold",
stroke = 0.3, stroke.colour = "white", # 'stroke' controls the width of stroke relative to the size of the text
skip = 0, # number of contours to skip
rotate = FALSE, # horizontal labeling; if TRUE, rotate text following the contour
label.placer = label_placer_fraction(frac = 0.5)) # 'frac = 0.5' places the label at equal distance from extremities. Try 'label.placer = label_placer_n(2)' to display two labels per contour line
However, contour lines and labeling do no longer match if we use e.g. 'n_contour = 10' within draw().
To allow this matching, use 'n_contour = 0' within draw(), define 'binwidth' within geom_contour2() and 'breaks' within geom_text_contour(), as follows.
Plot using gratia::draw with 'binwidth'-adjusted contour lines:
min(sm$est); max(sm$est) # find min() and max() for adjusting the 'est' z-scale
draw(mod, n_contour = 0) +
geom_contour2(aes(z = est), binwidth = 0.2) +
geom_text_contour(
aes(z = est), # 'est' from smooth_estimates(mod)
breaks = seq(-0.4, 0.4, by = 0.2), # 'breaks' must match with 'binwidth' above
colour = "black", size = 4.5, fontface = "bold",
stroke = 0.3, stroke.colour = "white", # 'stroke' controls the width of stroke relative to the size of the text
skip = 0, # number of contours to skip
rotate = FALSE, # horizontal labelling; if TRUE, rotate text following the contour
label.placer = label_placer_fraction(frac = 0.5)) # 'frac = 0.5' places the label at equal distance from contour lines' extremities. Try 'label.placer = label_placer_n(2)' to display two labels per contour line
Also possible to customize the graph directly with ggplot2:
ggplot(data = sm, aes(x = x, y = z, z = est)) +
geom_contour2(aes(z = est), binwidth = 0.1) +
geom_text_contour(
aes(z = est), # 'est' from smooth_estimates(mod)
breaks = seq(-0.4, 0.4, by = 0.1), # 'breaks' instead of 'bins' to not have too many decimals
colour = "black", size = 4.5, fontface = "bold",
stroke = 0.3, stroke.colour = "white", # 'stroke' controls the width of stroke relative to the size of the text
skip = 0, # number of contours to skip
rotate = FALSE, # horizontal labelling; if TRUE, rotate text following the contour
label.placer = label_placer_fraction(frac = 0.5)) # 'frac = 0.5' places the label at equal distance from contour lines' extremities. Try 'label.placer = label_placer_n(2)' to display two labels per contour line
You can use geom_textcontour from geomtextpath to obtain nicely placed labels without having to tweak lots of different parameters:
library(geomtextpath)
ggplot(sm, aes(x, z, z = est)) + geom_textcontour()
To use it within the gratia::draw framework, you can remove the existing contour from the plot first:
p <- draw(mod)
p$layers[[2]] <- NULL
p + geom_textcontour(aes(z = est), fontface = 'bold')
EDIT
To get a similar effect to the stroke parameter we can do:
library(ggfx)
p + with_outer_glow(geom_textcontour(aes(z = est), fontface = 'bold',
linetype = NA),
colour = 'white', expand = 3, sigma = 1) +
geom_textcontour(aes(z = est), fontface = 'bold', textcolour = NA)

Change data label font in ggplot:

This solution https://stackoverflow.com/a/71590169/7106842
is effective a changing the font of the title, x-axis, and y-axis labels. But not of the annotated data labels. They are still default font. Is there a way to change those labels as well?
I recognize this isn't a minimally reproducible example but the code below was the original partial solution to the problem.
#incorrect model with labels added by sjPlot
a = (plot_model(sl_distr_model,
order.terms = c(1,2,3,4,5,6,7),
show.values = TRUE,
value.offset = .3,
rm.terms = c("taxon_detailAnas platyrhynchos","taxon_detailAnas strepera","logshape","logshape:cadencefactor1h","logshape:cadencefactor2h","logshape:cadencefactor3h","logshape:cadencefactor6h","logshape:cadencefactor12h","logshape:cadencefactor24h"),
axis.lim = c(xlimrange_min,xlimrange_max),
colors = c("firebrick"),
wrap.labels = 60,
title = c("Plot 3: Intercept parameters by time interval - Relationship bewtween shape and scale gamma parameters of step length distributions 7.429"),
axis.title = "Intercept = Red; Ratio of Shape to Rate parameters = Blue"))
#added 2 fonts I had installed
windowsFonts(A = windowsFont("Times New Roman"), B = windowsFont("Century Gothic"))
#incomplete formating
a + theme(text = element_text(family = "A")
The above code did not change the data label font. The all components of the chart were Times New Roman except for the data labels which remained the system deault (courier), to fix this I had to manually add the relevant values using geom_text() referencing the same font family as the rest of the chart
#correct model without data labels
aa = (plot_model(sl_distr_model,
order.terms = c(1,2,3,4,5,6,7),
show.values = FALSE,
value.offset = .3,
rm.terms = c("taxon_detailAnas platyrhynchos","taxon_detailAnas strepera","logshape","logshape:cadencefactor1h","logshape:cadencefactor2h","logshape:cadencefactor3h","logshape:cadencefactor6h","logshape:cadencefactor12h","logshape:cadencefactor24h"),
axis.lim = c(xlimrange_min,xlimrange_max),
colors = c("firebrick"),
wrap.labels = 60,
title = c("Plot 3: Intercept parameters by time interval - Relationship bewtween shape and scale gamma parameters of step length distributions 7.429"),
axis.title = "Intercept = Red; Ratio of Shape to Rate parameters = Blue"))
aa + theme(text = element_text(family = "B")) + geom_text(aes(label= round(sl_distr_model#beta[2:8],2)), family = "B", nudge_x = 0.25, check_overlap = F)
Now all text is Century Gothic.

ggsurvplot changing confidence interval line size

Trying to change the line size of my confidence interval using ggsurvplot. When I use change the size to 1 or 2, it only affects the size of the center line, and not the size of the confidence interval lines. Any suggestions on how to alter the size of those lines?
Here is my code:
ggsurvplot(ovlsfit,
surv.scale="percent",
title="Survival Curve",
xlim=c(-0.02,5.1),
xlab=c("Time (y)"),
break.x.by=1,
censor=FALSE,
axes.offset=FALSE,
conf.int.style=c("step"),
legend=c("none"),
linetype = "strata",
color="black",
size=0.8,
risk.table=TRUE,
risk.table.height=0.18,
risk.table.pos=c("out"),
risk.table.fontsize=4)
And output:
Let's create a model similar to yours for demonstration:
library(survival)
library(survminer)
ovlsfit <- survfit(Surv(time/660, status) ~ 1, data = colon)
Now we will try to recreate your plot:
p <- ggsurvplot(ovlsfit,
surv.scale = "percent",
title = "Survival Curve",
xlim = c(-0.02,5.1),
xlab = c("Time (y)"),
break.x.by = 1,
censor = FALSE,
axes.offset = FALSE,
conf.int.style = c("step"),
legend = c("none"),
linetype = 'strata',
palette = "black",
size = 0.8,
risk.table = TRUE,
risk.table.height = 0.18,
risk.table.pos = c("out"),
risk.table.fontsize = 4)
p
Unfortunately, there is no direct access to the confidence interval line size from within the call to ggsurvplot, but we can easily access the layers they are in and change the line size directly. Here, we will make them large for demonstration purposes:
p$plot$layers[[3]]$aes_params$size <- 2
p$plot$layers[[4]]$aes_params$size <- 2
p

Reduce whitespace in corrplot in R

I have a 1 row 2 column plot which I need to insert into a report. When I insert the plot I notice there's a large white area around both correlation plots. Is there an argument in the corrplot function to reduce this? I've read here about using knitr hook functions to crop pdfs but that a bridge too far for my coding capabilities. I've messed around with the par() arguments such as omi and mai but when I export the image to TIFF I still get a huge white space area.
My code so far looks as follows, please note this is hacked together from various sources and dputs to both correlation matrices can be found here and here:
col <- colorRampPalette(c("#BB4444", "#EE9988", "#FFFFFF", "#77AADD", "#4477AA"))
par(mfrow=c(1,2),omi=c(0,0,0,0),mai=c(0.1,0.1,0.1,0.1))
corrplot(LX0088U21A1_24hrCor, method = "color", col = col(200),
type = "upper", order = "original", number.cex = 0.85,
addCoef.col = "black", # Add coefficient of correlation
tl.col = "black", tl.srt = 90,
number.digits = 1,
# Text label color and rotation
# hide correlation coefficient on the principal diagonal
diag = FALSE,
#title = "Intraday Correlation LX0088U21A1",
mar=c(0,0,1,0))
legend(0.5,5,ncol = 1, cex = 1.5,legend = "(a)", box.lwd = 0, box.col = "white")
corrplot(LX0088U09A3_24hrCor, method = "color", col = col(200),
type = "upper", order = "original", number.cex = 0.75,
addCoef.col = "black", # Add coefficient of correlation
tl.col = "black", tl.srt = 90,
number.digits = 1,
# Text label color and rotation
# hide correlation coefficient on the principal diagonal
diag = FALSE,
#title = "Intraday Correlation LX0088U09A3",
mar=c(0,0,1,0))
legend(0.5,5,ncol = 1, cex = 1.5,legend = "(b)", box.lwd = 0, box.col = "white")
Any help is greatly appreciated.

Circle plot error, some data offset from zero, plot inside

I am trying to plot vanishing bearing data from pigeons in R using plot.circular from the package circular. My R packages are up to date
Currently I have two problems
1) For some reason the bearings at 270 and 90 are being plotted closer to 0 than they should.
2) I would like to plot the data inside the circle rather than outside it
Cheers
Josh
Example code:
###Data
VB<-rad(c(0,90,90,180,180,180,270,270,270,270))
##Make circular
VB<-as.circular(VB, type = 'angles',units = "radians",
template = "none", modulo = "asis",
zero = pi/2, rotation = c("clock"))
##Plot
plot.circular(VB, pch = 16, cex = 1, stack = TRUE,
axes = FALSE, sep = 0.07, shrink = 1, bins = NULL, ticks = FALSE,
tcl = 0.025, tcl.text = 0.125, col = NULL, tol = 0.04, uin = NULL,
xlim = c(-1, 1), ylim = c(-1, 1), digits = 6, units = "radians",
template = NULL, zero = pi/2, rotation = "clock",
main = "", sub="", xlab = "", ylab = "",
control.circle=circle.control())
The misplaced points are apparently due to the way the bins are formed.
Try setting the bins parameter to some large value.
plot(VB, stack=TRUE, bins=360)
To plot inside the circle, you can use a negative value for the sep parameter.
plot(VB, stack=TRUE, bins=360, sep=-.07)

Resources