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.
Related
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")
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)
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..)?
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.
I want to plot data in a graph with larger font-size for the lables.
x = c(0:10)
y = sin(x) + 10
plot (
x, y, type="o",
xlab = "X values",
ylab = "Y values",
cex.axis = "2",
cex.lab = "2",
las = 1
)
Unfortunately the numbers on the y-axis overlap the label for the y-axis. I tried to use mar, but that did not work (By the way, how can I find out which graphic parameters can be directly used in the plot command and which have to be set with the par()-method? ).
How can I avoid that labels overlap?
Thanks for your help.
Sven
Use par(mar) to increase the plot margins and par(mgp) to move the axis label.
par(mar = c(6.5, 6.5, 0.5, 0.5), mgp = c(5, 1, 0))
#Then call plot as before
In the help page ?par it explains which parameters can be used directly in plot and which must be called via par.
There are several parameters can only be set by a call to ‘par()’:
• ‘"ask"’,
• ‘"fig"’, ‘"fin"’,
• ‘"lheight"’,
• ‘"mai"’, ‘"mar"’, ‘"mex"’, ‘"mfcol"’, ‘"mfrow"’, ‘"mfg"’,
• ‘"new"’,
• ‘"oma"’, ‘"omd"’, ‘"omi"’,
• ‘"pin"’, ‘"plt"’, ‘"ps"’, ‘"pty"’,
• ‘"usr"’,
• ‘"xlog"’, ‘"ylog"’
The remaining parameters can also be set as arguments (often via
‘...’) to high-level plot functions such as ‘plot.default’,
‘plot.window’, ‘points’, ‘lines’, ‘abline’, ‘axis’, ‘title’,
‘text’, ‘mtext’, ‘segments’, ‘symbols’, ‘arrows’, ‘polygon’,
‘rect’, ‘box’, ‘contour’, ‘filled.contour’ and ‘image’. Such
settings will be active during the execution of the function,
only. However, see the comments on ‘bg’ and ‘cex’, which may be
taken as _arguments_ to certain plot functions rather than as
graphical parameters.
The quick and dirty way would be to use par and add a newline in ylab, even though it's conceptually terrible.
x = 0:10
y = sin(x) + 10
par(mar=c(5,7,4,2))
plot (
x, y, type="o",
xlab = "X values",
ylab = "Y values\n",
cex.axis = "2",
cex.lab = "2",
las = 1
)
Concerning which parameters you can set directly in plot have a look at ?plot.default and ?plot.xy as they will recieve the ... arugments. There's also a couple of calls to undocumented functions (as far as I can find) like localWindow and localBox but I don't know what happens to them. I'd guess they're just ignored.
You can put the mgp parameter into the title() function to avoid having to reset your defaults afterwards. That way the parameter only acts on the label(s) added by the function. like this:
plot (
x, y, type="o",
xlab = "", #Don't include xlab in main plot
ylab = "Y values",
cex.axis = "2",
cex.lab = "2",
las = 1
)
title(xlab="X values"
,mgp=c(6,1,0)) #Set the distance of title from plot to 6 (default is 3).