Change sub-titles in lattice/densityplot - r

I was working in lattice/densityplot and want to change the "in", "out" sub-titles to "inpatinet", "outpatient".
Thank you. The image and code below:
colors = c("black", "blue")
lines = c(1,2) #1
points = c(16,17)
key.trans <- list(title="Gender",
corner = c(0.90, 0.90),
#space="bottom", columns=2, #2
text=list(c("Female","Male")),
#points=list(pch=points, col=colors),
lines=list(col=colors, lty=lines),
cex.title=1, cex=.9)
densityplot(~ Age_yrs|Location_type, groups=Sex,
data = subset(all_isolates_inc_ATCC, Age_yrs<100 &
Institution=="P06"
& Organism=="eco"
& Location_type!="unk"),
xlab = "Age, years",
lty=lines, col=colors,
lwd=2,
plot.points = FALSE,
#jitter=.00005,
key=key.trans)

The strip names can be changed in the function with the strip.custom function. A related question was asked in Change text on strips in lattice plots. In your case you would add the following argument to your densityplot() call:
strip = strip.custom(factor.levels = c("In patient", "Out patient"))
More information is found with help for strip.default although help for strip.custom gets you there as well.

Related

Working with heatmap2 function in R. Getting Error Message: Error in par(op): invalid value for graphic parameter pin

I am working with heat map in R and using heatmap2 function to create a correlation heat map. I created a heat map last night and it was just fine. I only missed to add one variable "Sports Involvement" and came back to add it and now it isn't working and showing me this error:
Error in .External.graphics(C_layout, num.rows, num.cols, mat, as.integer(num.figures), :
invalid graphics state
Error in par(op) : invalid value specified for graphical parameter "pin"
Here is my code:
labvec <- c(rep("ACE Score"))
labvec[c("Bully-Victim","Family Resilience",
"TBI","Child's Sex","Child's Age",
"Child's Race","Caregiver Education","Sports
Involvement","Epilepsy
Status", "Percent FPL")] <- c("Bully-Victim","Family Resilience",
"TBI","Child's Sex","Child's Age",
"Child's Race","Caregiver Education","Sports Involvement","Epilepsy
Status", "Percent FPL")
heatmap.2(acenation[,-1], cellnote = acenation[,-1], notecol
= "black", key.xlab = "r value",
trace = "none",
par(cex.main=0.8),
dendrogram = "column", density.info = "none", cexCol =1.3,
cexRow = 1.3,
Rowv = NA,
margins = c(15,15),
labRow = labvec,
main = "Correlation Matrix Between ACE Score, TBI and Covariates",
scale= "none",col= brewer.pal(8, "Greens"))
I created multiple heat maps with this code several times after I cracked this two months ago. However, it isn't working now. Any help will be appreciated. Thanks so much!

R: plotmath expression symbols not showing up in interaction plot

For some reason, my interaction plots don't seem to show the greek symbols (latex) in R markdown (using R studio). The code I am using is reproduced below. Why are the expression() functions not working? Any suggestions?
with(ba_results, interaction.plot(as.factor(f1), as.factor(f2),
y,
type = "b",
pch = c(18, 19, 24),
fixed = TRUE,
xlab = "Scale factor",
ylab = "Mean Response",
trace.label = expression(mu_e),
main = paste("Interaction plot of",
expression(mu[e]),
"f1")))
For the title, wrap the whole thing in expression. For example, main = expression(paste("Interaction plot of ", mu[e], " f1")) or main = expression(Interaction~plot~of~mu[e]~f1).
For the trace.label, the expression is not being parsed properly. It looks like the problem is this line in the code for interaction.plot:
text(xleg, ylim[2L] - 0.05 * yrng, paste(" ", trace.label), adj = 0)
So trace.label is wrapped in paste which turns the expression back into a text string. For example:
expression(mu[e])
# expression(mu[e])
paste(" ", expression(mu[e]))
# " mu[e]"
As a workaround, you can modify the function to use trace.label as is. To do that, get the code for interaction.plot by typing interaction.plot in the console. Copy the code into a new R script file and assign the function a new name like my_interaction_plot. Then change the line above to this:
text(xleg, ylim[2L] - 0.05 * yrng, trace.label, adj = 0)
This change will result in expression(mu[e]) being parsed properly.
Now just use my_interaction_plot instead of interaction.plot like this:
with(ba_results,
my_interaction_plot(as.factor(f1), as.factor(f2), y, type = "b",
pch = c(18, 19, 24), fixed = TRUE,
xlab = "Scale factor", ylab = "Mean Response",
trace.label = expression(mu_e),
main = expression(paste("Interaction plot of ", mu[e], " f1"))))

Neither Color Key nor Main Title appear (properly) on heat map (heat map.2 in r)

I'm learning how to use R and for an exercise I'm using an experimental ExpresionSet from Bioconductor: http://bioconductor.org/packages/release/data/experiment/html/leukemiasEset.html
As simple as that: My heat map doesn't display the color key and the main title appears chopped off on the left site. I had given up with the no color key problem after searching on the web how to solve it and not really finding a solution. But then I added the title and since it doesn't appear properly either, I thought that maybe both things are due to the same (unknown for me) reason...
Here is my code:
difexp <- exprs(leukemiasEset)[c(which(adjPval < 0.01)),c(1:12, 25:36)]
heatmap.2(difexp,
trace = "none",
cexCol = 0.6,
ColSideColors = as.character(as.numeric(factor(leukemiasEset$LeukemiaType[c(1:12, 25:36)]))),
main = "Differentially expressed genes in ALL and CLL samples",
cex.main = 1.5,
key = TRUE,
keysize = 1.5,
density.info = "histogram")
Where:
pvalues <- c()
for(i in 1:nrow(exprs(leukemiasEset))) {
R <- t.test(exprs(leukemiasEset)[i, leukemiasEset$LeukemiaType == "ALL"],
exprs(leukemiasEset)[i, leukemiasEset$LeukemiaType == "CLL"],
var.equal = TRUE)
pvalues <- c(pvalues, R$p.value)
}
adjPval <- p.adjust(pvalues, method = "fdr")
And this is how it looks like:
Since I am a beginner, I think this is probably an "easy peasy" for the experts... thank you very much in advance!
Use "\n" to insert a newline in your title and put half of it on a second line.
heatmap.2(difexp,
trace = "none",
cexCol = 0.6,
ColSideColors = as.character(as.numeric(factor(leukemiasEset$LeukemiaType[c(1:12, 25:36)]))),
main = "Differentially expressed genes\nin ALL and CLL samples",
cex.main = 1.5,
key = TRUE,
keysize = 1.5,
density.info = "histogram")

Using meta, metaprop, and forest to create forest plot graphics in R

I am using the metaprop command in R to do a meta-analysis of proportions. Some sample code is below.
library(meta)
m <- metaprop(4:1, c(10, 20, 30, 40))
forest(m)
I have several questions.
How do I force all of the text (proportion column, 95% CI column, fixed and random weights columns) to appear on the LEFT side of the plot rather than on the right?
I have another column of text identifiers for each study that I would like to add on the left as well. How can I stick this in as its own column?
I really need to plot these as percentages, not proportions. Is there a way to make this change?
Last, I need the bottom axis to go from 0 to 100% and have a label.
Edit
Ok, I have figured out how to do almost everything. The only thing I need help with is, I have another vector of labels that I would like to add to the left side of the plot. It seems like the leftlabs or leftcols command should do this but I can't get it to work. I would like to also plot another column on the left entitled "Details" that, for each of the four studies, has a little sentence about them.
forest(m, xlim = c(0,100), pscale = 100, weight = "random", leftcols = c("studlab", "event", "n", "effect", "ci", "w.random"), rightcols = F, leftlabs = c("Study", "Number", "Total", "Prevalence (%)", "95% CI", "Weight"), xlab = "Prevalence (%)", addspace = TRUE, digits = 1, squaresize = 0.5, text.I2 = "I2", text.tau2 = "tau2")
# https://rdrr.io/cran/meta/man/forest.html
# See the example in the link above, specifically
data(Olkin95)
meta1 <- metabin(event.e, n.e, event.c, n.c,
data=Olkin95, subset=c(41,47,51,59),
sm="RR", method="I",
studlab=paste(author, year))
#
# Specify column labels only for newly created variables
# 'year' and 'author' (which are part of dataset Olkin95)
#
forest(meta1,
leftcols=c("studlab", "event.e", "n.e", "event.c", "n.c",
"author", "year"),
leftlabs=c("Author", "Year of Publ"))

How to change the color and width of lines with par function in R

I have a question about the par function in R.
I want to change the color and/or width of a line in a graph with par function. (I am using par function because the gaps.plot command below does not allow "col" option to be included. The gaps.plot command is used after the synth command).
So, I used the following command. But I noticed that the lines of the BOX are changed rather than the lines of the GRAPHS.
synth1<-read.csv(file="C:\\Users\\Research\\R\\synthinR_v4.csv",header=TRUE)
attach(synth1)
library("Synth")
dataprep.out34 <- dataprep(foo = synth1, predictors = c("lncdsales", "md1", "md2","md3", "md4", "md5", "md6", "md7", "md8", "md9", "md10", "md11", "yd1", "yd2", "yd3", "yd4", "yd5", "yd6", "yd7", "yd8"), predictors.op = "mean", time.predictors.prior = -13:1, dependent = "lndigital", unit.variable = "artistalbumcode", time.variable = "release", treatment.identifier = 34, controls.identifier = c(1:33, 35:49), time.optimize.ssr = -13:1, time.plot = -13:25)
synth.out34 <- synth(data.prep.obj = dataprep.out34, method = "BFGS")
par(lwd = 2, col="#cccccc")
gaps.plot(synth.res = synth.out34, dataprep.res = dataprep.out34, Ylab = " Log Digital Sales ", Xlab = "Release", Ylim = c(-7, 7) , Main = NA)
Does anyone know how to fix this problem??
Thank you in advance for your willingness to help. I greatly appreciate it!
The col argument to par sets the default plotting colour (i.e. when col is not explicitly specified in plotting calls), but unfortunately col = "black" is hard-coded into the source of gaps.plot.
You can make a modified copy of the function by either (1) viewing the source (F2 in RStudio, or just executing gaps.plot), editing it and assigning it to a new object, or (2) doing something like the following:
gaps.plot2 <- eval(parse(text=gsub('col = "black"', 'col = "red"',
deparse(Synth:::gaps.plot))))
and then using gaps.plot2 as you would use gaps.plot:
gaps.plot2(synth.res = synth.out34, dataprep.res = dataprep.out34,
Ylab = " Log Digital Sales ", Xlab = "Release", Ylim = c(-7, 7) ,
Main = NA)
Alter the lwd similarly. For example to make lines red and have width of 3, use nested gsub calls like this:
gaps.plot2 <- eval(parse(text=gsub('lwd = 2', 'lwd = 3',
gsub('col = "black"', 'col = "red"',
deparse(Synth:::gaps.plot)))))

Resources