nlme package: augPred change the x axis with log='x' - r

Hi I am trying to change the scale on the x-axis to a log scale on a plot generated by
model2 <- nlme(consumption~Q*10^(5*(exp(-alpha*Q*price)-1)), fixed=Q+alpha~1,`random=Q+alpha~1|ratid, groups = ~ treatment,`data = oxydata, start = c(Q=3, alpha=0.001))
plot(augPred(model2), log='x', xlim = c(1,3000), xlab = "Price (resp/mg", ylab = "Consumption")
However, the log='x' or log="x" does nothing to change the x-axis scale. I can change the xlim and ylim, add titles etc. but I can't seem to change the x-axis to a log scale. Any help or verification that it can or can't be done is greatly appreciated.
Ugly plot with data all crammed

Thanks to anyone that was trying to figure this out, but I got it. This function uses the xyplot(), which requires the scale = list...
`plot(augPred(model2), scale = list(x = list(log = 10)), xlim = c(1,3000), xlab = "Price (resp/mg", ylab = "Consumption")

Related

y axis labeling in R and how to change x-axis to specific increments in R

I would like to create a plot of this data, with x-axis increments of 500000 and with sampleIDs on the y-axis. The following code works to create the plot, but the y-axis labels don't work, and I am unsure how to code the x-axis ticks. Also, I had to add headings manually to the data file (and then obviously add header = TRUE when I assigned d) to get the code to work. I shouldn't have had to put the column titles in though should I since I use setNames?
d = read.delim("n_reads_per_sample.tsv", header = TRUE, sep = "\t")
xticks <- ( ? increments of 500000 to xmax ? )
dotchart(
sort(setNames(d$n_reads, d$X.sample)),
xlim = c(0, at = xticks, 1 max(d$n_reads)),
labels = dimnames(d[[1]])
,
main = "reads per sample",
xlab = "number of reads",
ylab = "sample"
)
In case the link doesn't work, this is what the file looks like.
x.sample n_reads
LT-145 3193621
LT-323 786578
LT-458 485543
LT-500 3689123
LT-95 3308764
LT-367 765972
LT-205 2090226
LT-245 10238727
I can't get at your full data right now, so I am just using your sample in the question.
Not sure what you mean that the y-axis labels don't work. They seem OK to me. You can get the x-axis labels that you want by suppressing the x-axis produced by dotchart and then making your own axis using the axis function. That requires a little fancy footwork with par. Also, unless you stretch out your graphics window, there will not be enough room to print all of the axis labels. I reduced the font size and stretched the window to get the graph below.
UpperLimit <- ceiling(max(d$n_reads)/500000)*500000
xticks <- seq(0,UpperLimit, 500000)
par(xaxt = "n")
dotchart(
sort(setNames(d$n_reads, d$X.sample)),
xlim=c(0, UpperLimit),
labels = dimnames(d[[1]]),
main = "reads per sample",
xlab = "number of reads",
ylab = "sample"
)
par(xaxt = "s")
axis(1, at=xticks, cex.axis=0.7)

add text / labels to survival plot (kaplan Meier) in R

Want to add patient ID as labels to my survival plot. I know that I did not provide source data, sorry. But I guess, my problem could be solve anyway with you smart people ;-)
library(survival)
s=Surv(data$OS,data$Death.1)~data$Tumordignity
b=survfit(s)
plot(b,
col = c("darkred","darkgoldenrod","darkblue","green3"),
bty = "n",
lwd = 3,
mark.time = TRUE,
main="Overall survival in patients with malignancies",
xlab="survival time...",
ylab = "Survival rate...",
xlim=c(0,2600),
cex.main=0.8)
#so far, so good...
# try to add text to the mark.time-points
text(data$OS,
data$Death.1,
labels = data$ID,
cex= 0.7, pos=3)
Problem with this, that labels (IDs) will be plotted in the right x position but not in the right y position.
see my plot
How to fix it? Use points(x..)?

Plotting in R using plot function

I am trying to plot few graphs using loops. I am now describing in details.
First I have a function which is calculates the y-variable (called effect for vertical axis)
effect<- function (x, y){
exp(-0.35*log(x)
+0.17*log(y)
-0.36*sqrt(log(x)*log(y)/100))
}
Now I run the following code and use the option par to plot the lines in the same graph. I use axis=FALSE and xlab="" to get a plot without labels. I do this so that my labels are not re-written each time the loop runs and looks ugly.
for (levels in seq(exp(8), exp(10), length.out = 5)){
x = seq(exp(1),exp(10), length.out = 20)
prc= effect(levels,x)
plot(x, prc,xlim = c(0,max(x)*1.05), ylim=c(0.0,0.3),
type="o", xlab = "",ylab = "", pch = 16,
col = "dark blue", lwd = 2, cex = 1, axes = F)
label = as.integer(levels) #x variable
text(max(x)*1.03,max(prc), label )
par(new=TRUE)
}
Finally, I duplicate the plot command this time using the xlab and ylab options
plot(x, prc, xlab = "X-label", ylab = "effect",
xlim = c(0,max(x)*1.05), ylim = c(0,0.3),
type="l", col ='blue')
I have several other plots in the similar lines, using complex equations. I have two questions:
Is there an better option to have the same plot with smoother lines?
Is there an easier option with few lines to achieve the same, where I can place the texts (levels) for each line on the right with white background at the back?
I believe working with the plot function was tedious and time consuming. So, I have finally used ggplot2 to plot. There were several help available online, which I have used.

overlaying different density curves without showing the histogram

I want to be able to show three different density curves on the same axis. I've got the code below so far but i don't know how to combine them to so that they can
curve(dnorm(x,mean=0,sd=1),col="darkgreen",xlim=c(-4,8),ylim=c(0,.8))
curve(dnorm(x,mean = 0,sd=1.5),col="red",xlim = c(-5,8),ylim=c(0,.6))
curve(dnorm(x,mean = 0.5,sd=0.5),col="black",xlim = c(-2,8), ylim =c(0,1))
This is the basic solution. You can add more formatting if need be (for axes etc). Note you need to change the xlim and ylim to match on the plots.
curve(dnorm(x,mean=0,sd=1),col="darkgreen",xlim=c(-5,8),ylim=c(0,1), ylab = "")
par(new = TRUE)
curve(dnorm(x,mean = 0,sd=1.5),col="red",xlim = c(-5,8),ylim=c(0,1), ylab = "")
par(new = TRUE)
curve(dnorm(x,mean = 0.5,sd=0.5),col="black",xlim = c(-5,8), ylim =c(0,1), ylab = "")

Customize minimum and maximum value for logi.hist.plot in R

Anybody know how to set the minimum and maximum values for x-axis when running logi.hist.plot in popbio package in R?
At the moment, the minimum value is defined as my minimum data value. I want it to be 0.
library(popbio)
logi.hist.plot(data$Heat, data$Death, logi.mod = 1,
boxp = FALSE,type="hist", col="gray",
ylabel = "Probability of death",
ylabel2 = "Death Frequency",
xlabel = "Heat",
mainlabel = "Logistic probability plot of Heat vs Death")
You have not offered a dataset for testing possible solutions to this request, but I offer an idea:
First make a plot that basically sets up the desired limits with xlim and ylim as desired, and blank x- and y-labels and axt="n",
...then issue par(new=TRUE),
...then run your plot function.
Taking a quick look at the source code - just type logi.hist.plot - it isn't possible to change the axis limits.
The source code is fairly long, but not that complicated. Essentially, the boxp=FALSE option calls this part of code:
logi.scater <- function(independ, depend, scater = "n", x.lab = xlabel,
las = las.h) {
plot(independ, depend, cex = 1, type = scater, ylab = ylabel,
xlab = x.lab, main = mainlabel, cex.lab = 1.2, las = las)
}
You can see that the plot function doesn't allow limits to be passed.
You options are:
Take apart the source code and construct your own plot.
Decided you are happy with the axis.

Resources