R: subscript text as a variable - r

In R I would like to create several plots in a for-loop. The y-axis label is supposed to exhibit a subscript, in which the subscript text should vary along with the loop-iterator.
For subscripting a label, I previously used "expression". However, as you can see in the minimal example, the subscript in the expression cannot be indexed the way I thought it would (instead of printing "1", "2", "3" it simply prints "i"). Do you have an idea on how to fix this (either by using the expression function or any other text function able to produce subscripts)?
Minimal code:
# minimal example code
Data = matrix(ncol = 4, nrow = 1000)
colnames(Data) = c("time", "k1", "k2", "k3")
Data[,1] = seq(0.1,100,0.1)
Data[,2] = sin(Data[,1])
Data[,3] = cos(Data[,1])
Data[,4] = tan(Data[,1])
for(i in 1:3) {
plot(Data[,1], Data[,(1+i)], type = "l", lwd = 2, xlab = "time", ylab = expression("k" [i]))
}
Thank you!

Use bquote. Stolen from this SO:
Subscripts in plots in R
for(i in 1:3) {
plot(Data[,1], Data[,(1+i)], type = "l", lwd = 2, xlab = "time", ylab = bquote(k[.(i)]))
}
It has very strange syntax of: bquote(WORD [ . (OBJECT) ]. Note that WORD is not quoted and the dot. I believe the . is referring to what environment to go find OBJECT.

Related

Create an R function that normalizes data based on input values

I don't make to many complicated functions and typically stick with very basic ones. I have a question, how do I create a function that takes a dataset and normalizes based on desired normalization method and boxplots the output? Currently norm_method is different between the norm methods, was wondering if there is a way to call this in the start of function to pull through the correct method? Below is the code I created, but am stuck how to proceed.
library(reshape2) # for melt
library(cowplot)
demoData;
# target_deoData will need to be changed at some point
TestFunc <- function(demoData) {
# Q3 norm (75th percentile)
target_demoData <- normalize(demoData ,
norm_method = "quant",
desiredQuantile = .75,
toElt = "q_norm")
# Background normalization without spike
target_demoData <- normalize(demoData ,
norm_method = "neg",
fromElt = "exprs",
toElt = "neg_norm")
boxplot(assayDataElement(demoData[,1:10], elt = "q_norm"),
col = "red", main = "Q3",
log = "y", names = 1:10, xlab = "Segment",
ylab = "Counts, Q3 Normalized")
boxplot(assayDataElement(demoData[,1:10], elt = "neg_norm"),
col = "blue", main = "Neg",
log = "y", names = 1:10, xlab = "Segment",
ylab = "Counts, Neg. Normalized")
}
You might want to consider designing your normalize() and assayDataElement() functions to take ..., which provides more flexibility.
In lieu of that, given the examples above, you could make a simple configuration list, and elements of that configuration are passed to your normalize() and assayDataElement() functions, like this:
TestFunc <- function(demoData, method=c("quant", "neg")) {
method = match.arg(method)
method_config = list(
"quant" = list("norm_args" = list("norm_method" = "quant", desired_quantile = 0.75, "toElt" = "q_norm"),
"plot_args" = list("col"="red", main="Q3", ylab = "Counts, Q3 Normalized")),
"neg" = list("norm_args" = list("fromElt" = "exprs", "toElt" = "neg_norm"),
"plot_args" = list("col"="blue", main="Neg", ylab = "Counts, Neg Normalized"))
)
mcn = method_config[[method]][["norm_args"]]
mcp = method_config[[method]][["plot_args"]]
# normalize the data
target_demoData = do.call(normalize, c(list(data = demoData[1:10]), mcn))
# get the plot
boxplot(assayDataElement(
demoData[1:10], elt=mcp[["toElt"]],col = mcp[["col"],main = mcp[["main"]],
log = "y", names = 1:10, xlab = "Segment",ylab = mcp[["ylab"]]
)
}
Again, using this approach is not as flexible as ... (and consider splitting into two functions.. one that returns normalized data, and a second function that generates the plot..

R: How to batch create multiple copies of an .R script file with different variable names?

This is my first post here and I am trying to follow the guidelines to the best of my knowledge, so please bear with me.
I want to create a large number of similar .R script files which differ only in the variable names used and in the strings that mention these variables. Of course, this can also be achieved via search & replace but I was wondering whether there is a more convenient solution to create a bunch of them more quickly.
Let's take this made up script (the actual data is irrelevant here):
prefix.AnExemplaryRandomVariable <- rnorm(n = 100, mean = 0, sd = 1)
AnotherRandomVariable.suffix <- rnorm(n = 100, mean = 10, sd = 3)
plot(prefix.AnExemplaryRandomVariable, AnotherRandomVariable.suffix,
type = "p", pch = "*", xlab = "An Exemplary Random Variable",
ylab = "Another Random Variable", main = "A plot of An Exemplary
Random Variable and Another Random Variable")
My idea was to define two vectors with k new names for each one.
newNamesVar1 <- c("prefix.FirstVariable", "prefix.SomeData")
newNamesVar2 <- c("SecondVariable.suffix", "CannotThinkOfMoreNames.suffix")
The result I am looking for are k new .R files that look like this:
prefix.FirstVariable <- rnorm(n = 100, mean = 0, sd = 1)
SecondVariable.suffix <- rnorm(n = 100, mean = 10, sd = 3)
plot(prefix.FirstVariable, SecondVariable.suffix, type = "p",
pch = "*", xlab = "First Variable", ylab = "Second Variable",
main = "A plot of First Variable and Second Variable")
and
prefix.SomeData <- rnorm(n = 100, mean = 0, sd = 1)
CannotThinkOfMoreNames.suffix <- rnorm(n = 100, mean = 10, sd = 3)
plot(prefix.SomeData, CannotThinkOfMoreNames.suffix, type = "p",
pch = "*", xlab = "Some Data", ylab = "Cant Think Of More Names",
main = "A plot of Some Data and Cannot Think Of More Names")
I see the following two challenges:
Replacing the original variable names with the corresponding vector entries
Checking any strings for similarities with the original variable names and replacing them while keeping the syntax and format (case sensitivity, spacing,...) intact.
This is the first time I am trying to use R for anything beyond actual data analysis, so I cannot even provide much of a code draft. I was able to get the variable names with ls(), but I do not have the slightest clue about what to do next, mainly because the changes are not to be applied to the file that is currently active but to a completely new one.
Any solutions, tips, hints or nudges are appreciated!
Thank you!
Here's one method.
Setup for this answer:
writeLines('
prefix.AnExemplaryRandomVariable <- rnorm(n = 100, mean = 0, sd = 1)
AnotherRandomVariable.suffix <- rnorm(n = 100, mean = 10, sd = 3)
plot(prefix.AnExemplaryRandomVariable, AnotherRandomVariable.suffix,
type = "p", pch = "*", xlab = "An Exemplary Random Variable",
ylab = "Another Random Variable",
main = "A plot of An Exemplary Random Variable and Another Random Variable")
', "template.R")
Table of replacement values to use, where the column name indicates the template string, and the column values are the replacement text.
replacements <- data.frame(
"An Exemplary Random Variable" = c("First Variable", "Some Data"),
"Another Random Variable" = c("Second Variable", "Cannot Think Of More Names"),
check.names = FALSE
)
replacements
# An Exemplary Random Variable Another Random Variable
# 1 First Variable Second Variable
# 2 Some Data Cannot Think Of More Names
The Work that replaces each template string from the template.R, makes the replacement, ultimately storing into new files.
code <- readLines("template.R")
for (row in seq_len(nrow(replacements))) {
newcode <- code
for (col in seq_along(replacements)) {
if (!is.na(replacements[row,col])) {
ptn1 <- colnames(replacements)[col] # original
ptn2 <- gsub(" +", "", ptn1) # "Title Case Sentence" to "TitleCaseSentence"
repl1 <- replacements[row,col]
repl2 <- gsub(" +", "", repl1)
newcode <- gsub(paste0("\\b", ptn1, "\\b"), repl1,
gsub(paste0("\\b", ptn2, "\\b"), repl2, newcode))
}
}
writeLines(newcode, sprintf("code_%s.R", row))
}
This has the added functionality that if the replacement string (value within a particular cell in replacements) is NA, then no replacement will be attempted for that pattern.
Output:
code_1.R
prefix.FirstVariable <- rnorm(n = 100, mean = 0, sd = 1)
SecondVariable.suffix <- rnorm(n = 100, mean = 10, sd = 3)
plot(prefix.FirstVariable, SecondVariable.suffix,
type = "p", pch = "*", xlab = "First Variable",
ylab = "Second Variable",
main = "A plot of First Variable and Second Variable")
code_2.R
prefix.SomeData <- rnorm(n = 100, mean = 0, sd = 1)
CannotThinkOfMoreNames.suffix <- rnorm(n = 100, mean = 10, sd = 3)
plot(prefix.SomeData, CannotThinkOfMoreNames.suffix,
type = "p", pch = "*", xlab = "Some Data",
ylab = "Cannot Think Of More Names",
main = "A plot of Some Data and Cannot Think Of More Names")
Limitations:
pattern strings must be contiguous on their own line, so notice that I changed the template main= string to not span two lines
pattern strings must not be immediately preceded/followed by letters; the use of \\b (regex word boundary) allows for some characters (like the literal .), but this makes no attempt to be any fancier
Edited: after I finished, I realized that it might be easier to define the patterns and replacement strings with the spaces, and then remove the spaces for the second (TitleCase) pattern. This way one avoids some ambiguity and trickery of splitting a string by title-case. It also allows for your patterns or replacements to be not title case.

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"))))

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)))))

Export plot data from R to Excel

I have this plot which i generate it from this code:
m <- bcea(e=effects,c=costs, ref=2, interventions=treatments, Kmax=50000)
The plot is:
evi.plot(m)
Now, i need to export this evpi.plot(m) in an excel file, not the jpeg created, but the data along with it, i mean what created the X and Y axis.
I've been using something like this but it's not for this case
write.table( thresholds, 'clipboard', sep='\t', row.names=FALSE, col.names=FALSE )
In the documentation for function bcea from package BCEA you can see the structure of your object:
Value
An object of the class "bcea" containing the following elements
n.sim Number of simulations produced by the Bayesian model
n.comparators Number of interventions being analysed
...
k
The vector of values for the grid approximation of the willingness to pay
...
evi The vector of values for the Expected Value of Information, as a
function of the willingness to pay
And if you look at the function definition of evi.plot you will see that your x and y-values are the elements named k and evi:
> evi.plot
function (he)
{
options(scipen = 10)
plot(he$k, he$evi, t = "l", xlab = "Willingness to pay",
ylab = "EVPI", main = "Expected Value of Information")
if (length(he$kstar) > 0) {
points(rep(he$kstar, 3), c(-10000, he$evi[he$k == he$kstar]/2,
he$evi[he$k == he$kstar]), t = "l", lty = 2, col = "dark grey")
points(c(-10000, he$kstar/2, he$kstar), rep(he$evi[he$k ==
he$kstar], 3), t = "l", lty = 2, col = "dark grey")
}
}
<environment: namespace:BCEA>
So:
res <- cbind(m$k, m$evi)
write.table(res, file="bcea.csv", sep=',', row.names=FALSE, col.names=FALSE )

Resources