When creating a sunflower plot for logistic regression, the x-axis starts at 2. Can I change this behaviour to make the x-axis start at 0? I've already tried to manually change this by changing the x-axis, but that didn't make x=0 visible (see #c1).
The Sunflower Plot Image as it currently is, starting at x=2;
# EE contains the Likert Scale values
EE.min <- min(EE)
EE.max <- max(EE)
EE.x <- seq(EE.min, EE.max, length = 500)
New.EE <- data.frame(EE = EE.x)
# Creating prediction
EE.p <- predict(logit, New.EE, type = "response")
sunflowerplot(EE, cb, main = "Effort Expectancy",
xlab= "EE (5-point Likert-Scale)", ylab="Likelihood", yaxt="n", xaxt="n")
# c1:
axis(1, at = seq(0,5,0.5), labels = c(0, 0.5, 1, 1.5, 2, 2.5 , 3, 3.5, 4, 4.5, 5), las=1)
axis(2, at = seq(0,1,0.2), labels = c("No = 0", 0.2, 0.4, 0.6, 0.8, "Yes = 1"), las = 2)
abline(h = seq(0,1,0.2), lty = 2)
lines(EE.x, EE.p)
sunflowerplot has an argument xlim for the limits of the x axis.
Compare
sunflowerplot(iris$Sepal.Length, iris$Sepal.Width)
to
sunflowerplot(iris$Sepal.Length, iris$Sepal.Width, xlim = c(0, 20))
Related
I've found an issue when setting pos = n when I use the axes command for creating charts in R. When the y-axis range is -6 to -1, it all works perfectly, but when my range is for example -5 to -2 the axes won't meet at the specified position; the y-axis omits the -5 label and tick mark.
The code for one of my charts is as follows:
x <- c(3179047.6, 1956470.4, 609295.9, 136037.6)
y <- c(-4.758698, -4.227640, -4.019197, -2.653719)
model_fit <- lm(y~x)
par(mar=c(2.1, 2.1, 2.5, 1.1)) # I want to reduce the white space around the margins
plot(x, y, axes = FALSE, xlab = NULL, ylab = NULL, # I set the axes manually afterwards
ylim = c(c(min(0, y)), ceiling(max(y))),
xlim = c(0, 1000000*ceiling(max(x)/1000000)))
lines(x4, predict(model_fit), lty = 2, col = "red")
mtext(side = 1, line = 1.1, expression(paste(epsilon^"2")), cex = 0.7) # Greek symbol squared, title repositioned, font size lowered.
mtext(side = 2, line = 0, "Y-axis Title\n(units)", cex = 0.7) # font size lowered
mtext(side = 3, line = -0.5, "Main\nTitle",
cex = 0.8, font = 2)
axis(1, seq(0, 1000000*ceiling(max(x)/1000000), 1000000),
labels = seq(0, 1000000*ceiling(max(x)/1000000), 1000000),tck = -0.02, cex.axis = 0.6, padj = -3, pos = floor(min(y4)))
axis(2, seq(floor(min(y)), ceiling(max(y)), 1),
labels = seq(floor(min(y)), ceiling(max(y)), 1), tck = -0.02, cex.axis = 0.6, padj = 2, pos = 0)
par(mar=c(5.1, 4.1, 4.1, 2.1)) # Resetting the default margin sizes
For the axis() commands I've first set the range by seq(0, 1000000*ceiling(max(x)/1000000), 1000000), and then I repeated the command by setting labels = seq(0, 1000000*ceiling(max(x)/1000000), 1000000) to manually force the code to return the tick marks and labels at the correct position.
Then for the x-axis (axis 1), I set the position where it meets the y-axis (axis 2) at the minimum value of the y axis range by setting pos = floor(min(y4). The issue is for the chart produced by the above code, instead of having the y-axis extend down to meet the x-axis, there's a space and no tick or label for the -5 point.
This only happens when the range is between -2 and -5, for my chart where the range is -1 to -6, the two axes meet.
Is there some underlying code in the axis command I need to override?
I have a foresplot in r using the metafor package:
#forestplot
par(mar=c(2.75,0,0,0))
par(font=2,mgp=c(2.3, 0.8, 0), xaxs = "i")
metafor::forest.rma(res, alim=c(-1, 3.75), xlim = c(-5, 3.5), at =
c(-1, -.5, 0 , .5, 1, 1.5, 2, 2.5, 3), xlab = "Cohen's D with 95% CI",
slab = data$Author, annotate = FALSE,
cex=.6, cex.lab = 0.5, refline = 0.68)
I would like to remove the ticks at the end of the confidence intervals for each observation but haven't found a clear way of doing so. In others words, have the confidence intervals just be horizontal lines, no vertical like at the end. Would anyone have any ideas on how to this?
Set the efac argument of metafor::forest.rma to c(0,1):
the first value is the vertical expansion factor for CI limits and arrows; the second is for summary estimates.
library(metafor)
res <- rma(measure="RR", ai=tpos, bi=tneg, ci=cpos, di=cneg, data=dat.bcg,
slab=paste(author, year, sep=", "))
par(mar=c(2.75,0,0,0))
par(font=2,mgp=c(2.3, 0.8, 0), xaxs = "i")
forest(res, alim=c(-1, 3.75), xlim = c(-5, 3.5), at =
c(-1, -.5, 0 , .5, 1, 1.5, 2, 2.5, 3), xlab = "Cohen's D with 95% CI",
annotate = FALSE, cex=0.8, cex.lab=0.8, refline = 0.68, efac=c(0,1))
I have plotted five graphs and a legend. The graphs work just fine, however the legens disappears without an error.
My preview in RStudio looks like this
When I zoom in, the area where the legend should be is blank.
I use the following code:
opar <- par (no.readonly = TRUE)
par (mfrow = c(3, 2))
library(deSolve)
# Plot A
LotVmod <- function (Time, State, Pars) {
with(as.list(c(State, Pars)), {
dx = (b*x) - (b*x*x/K) - (y*(x^k/(x^k+C^k)*(l*x/(1+l*h*x))))
dy = (y*e*(x^k/(x^k+C^k)*(l*x/(1+l*h*x)))) - (m*y)
return(list(c(dx, dy)))
})
}
Pars <- c(b = 1.080, e = 2.200, K = 130.000, k = 20.000, l = 2.000,
h = 0.030, C = 2.900, m = 0.050)
State <- c(x = 0.25, y = 2.75)
Time <- seq(1, 9, by = 1)
out <- as.data.frame(ode(func = LotVmod, y = State, parms = Pars, times = Time))
matplot(out[,-1], type = "l", xlim = c(1, 9), ylim = c(0, 45),
xlab = "time",
ylab = "population",
main = "Compartment A")
mtext ( "Coefficient of Variance 4.96", cex = 0.8 )
x <- c(# Validation data)
y <- c(# Validation data)
lines (Time, x, type="l", lty=1, lwd=2.5, col="black")
lines (Time, y, type="l", lty=1, lwd=2.5, col="red")
# Legend
plot.new()
legend("center", c(expression (italic ("F. occidentalis")*" observed"),
expression (italic ("M. pygmaeus")*" observed"),
expression (italic ("F. occidentalis")*" simulated"),
expression (italic ("M. pygmaeus")*" simulated")),
lty = c(1, 1, 1, 2),
col = c(1, 2, 1, 2),
lwd = c(2.5, 2.5, 1, 1),
box.lwd = 0, bty = "n")
# Plot C to F = same as A
par(opar)
My output doesn't give an error. I have used the exact same code before without any trouble, thus I restarted R, removed all objects, cleared all plots and restarted both RStudio and my computer.
Try to add xpd=TRUE in your legend statement. I.e.
legend("center", c(expression (italic ("F. occidentalis")*" observed"),
expression (italic ("M. pygmaeus")*" observed"),
expression (italic ("F. occidentalis")*" simulated"),
expression (italic ("M. pygmaeus")*" simulated")),
lty = c(1, 1, 1, 2),
col = c(1, 2, 1, 2),
lwd = c(2.5, 2.5, 1, 1),
box.lwd = 0, bty = "n", xpd=TRUE)
By default, the legend is cut off by the plotting region. This xpd parameter enables plotting outside the plot region. See e.g. ?par for more on xpd.
This is due to how the plot canvas is set up and how rescaling that device works. The way you do it, you add the legend in the plotting region of the top right plot. The plotting region is however not the complete device, but only the part inside the space formed by the axes. If you rescale, that plotting region will be rescaled as well. The margins around the plotting region don't change size though, so zooming in makes your plotting region so small that it doesn't fit the legend any longer. It is hidden by the margins around the plotting region.
For that reason AEBilgrau is very right you need to add xpd = TRUE. This allows the legend to extend outside of the plotting region, so it doesn't disappear behind the margins when resizing the plotting device.
I am attempting to create several histograms that display the effects a drug has on the frequency of heart attacks.
Currently, R is organizing my data into the bins [0 - 0.5, 0.5 - 1.0, 1.0 - 1.5, etc.], but I would like for it to only use integer values: [0 - 1, 1 - 2, 2 - 3, etc.].
I have tried using the xaxt="n" argument and the axis() function. They "worked," but they did not solve the problem above. I also tried to use breaks=seq(0,5,l=6), but this converted my y-axis from frequency into density.
Here is the code for my latest two attempts:
hist(fourTrials$red_5, breaks=5, right = FALSE,
xlab = "Number of Heart Attacks",
xlim = c(0, 4), ylim = c(0,4),
main = "Experimental Group 1, n = 400", col = "light blue")
hist(fourTrials$red_5, breaks=seq(0,5,l=6), freq = F, right = FALSE,
xlab = "Number of Heart Attacks",
xlim = c(0, 4), ylim = c(0,4),
main = "Experimental Group 1, n = 400", col = "light blue",yaxs="i",xaxs="i")
Thanks for any help!
I believe that what you want is:
hist(fourTrials$red_5, breaks=0:4, freq = TRUE, right = FALSE,
xlab = "Number of Heart Attacks",
xlim = c(0, 4), ylim = c(0,4),
main = "Experimental Group 1, n = 400",
col = "lightblue", yaxs="i", xaxs="i")
I don't know how to change the code so that the y-axis in the barPlot shows completely? I expect it to show up to 10 as I have a 9.2 in my data points but it shows only up to 8. Any idea what's the hack to this?
Here's the code:
And here's what it shows:
just set the ylim = c(0, 10) like the way you changed the xlim
Whatever the axis that the groups go on isn't drawn by default, so a vertical bar plot won't have x-axis; horizontal won't have y-axis. You can add that of course. Use the return value of barplot:
par(mfrow = c(2, 1))
bp <- barplot(c(8, 5), width = .5, main = 'Feature Exploration', xlim = c(0,4), ylim = c(0, 10),
ylab = 'Errors (%)', xlab = 'ML Models', col = c('gray27','orangered4'))
## this will draw the x-axis but at points 1, 2, 3, ... which is not
## where the centers of your bars are plotted; you get that info in bp
axis(1)
bp <- barplot(c(8, 5), width = .5, main = 'Feature Exploration', xlim = c(0,4), ylim = c(0, 10),
ylab = 'Errors (%)', xlab = 'ML Models', col = c('gray27','orangered4'))
## so try again with a fancy axis, bp is a matrix containing the centers
## of the plotted bars
axis(1, at = bp, labels = c('Model1','Model2'), lwd = 0, lwd.ticks = 1, tcl = -.5)