Mean in Scatterplot not illustrated - r

I want to assign the means of x and y to a scatterplot in R without ggplot, just basic R. However, in the way I try (using abline), it does not illustrate the lines in the plot. I already had to do another task earlier, hence the slightly longer code.
I tried using abline for either x and y to assign a horizontal and a vertical dashed line to the plot. The result was that the plot inclusive the blue dot is illustrated, but the means don't show up.
plot(x, y, axes = TRUE, pch = 16, xlim = c(40,120)
points(46, 150, col = "blue", pch = 16), main = abline(v = mean(y), lty = 2, lwd = 4)
abline(h = mean(x), lty = 2, lwd = 4))

You have a few syntax errors in the supplied code, but ultimately I think your problem is that you are trying to plot a vertical line at the mean y value when it should be at the mean x value, and a horizontal line at the mean x value, when it should be at the mean y value:
set.seed(1)
x <- rnorm(10, 47, 5)
y <- rnorm(10, 150, 5)
plot(x, y, axes = T, pch = 16)
points(46, 150, col = "blue", pch = 16)
abline(v = mean(x), lty = 2, lwd = 4)
abline(h = mean(y), lty = 2, lwd = 4)
Created on 2022-10-30 with reprex v2.0.2

Related

R -Smoothscatter plot curve and diagonal axis

I have file from that wanted plot the smoothscatterplot using R. plot must have the dots, diagonal axis and a curve for that I have formula, I am creating smoothscatterplot but not able plot diagonal and curve any suggestion and help will be appreciated
https://drive.google.com/file/d/1KknqYcRBCGm8Xrj1XKh3mE7rb7LK9iny/view?usp=sharing
what I tried
diagonal axis
df$P0+df$P2 =1
curve
p2 = (√df$P0 − 1)^2
df=read.table("scale_out",sep='\t', header=TRUE)
df = data.frame(df)
smoothScatter(df$P0,df$P2, cex=10)
what I got
what I want
Thank you
The data doesn't seem to contain what you think it contains.
For example, if we just do a straight plot of P0 and P2, we get this:
plot(df$P0, df$P2, pch = 18, cex = 0.5)
The desired plot that you show in your question suggests that this should be a scatter plot of noisy data, but it isn't. If we plot all the numeric variables in your data frame against each other, we get this:
The only plot here that looks like a scatter plot of the correct shape is P0 versus B.prop.
Assuming that this is what you want, you can create the desired plot like this:
smoothScatter(df$P0, df$B.prop, cex = 2, xlab = "P0", ylab = "P2")
curve((sqrt(x) - 1)^2, 0, 1, lty = 2, lwd = 5, col = "red", add = TRUE)
lines(0:1, 1:0, lty = 2, lwd = 5, col = "deepskyblue4")
legend(0.35, 1, c("Coordination", "Independence"),
col = c("deepskyblue4", "red"), bg = "#FFFFFFAA",
lty = 2, lwd = 5, box.col = "#FFFFFF00")

R is plotting y axis upside down. How to flip rightside up?

I've inherited this R code that plots a simple line graph. However, it does it so that the y axis values are plotted downwards below 0 (plots it in the 4th quadrant with 0 at the top and +3600 at the bottom). I want to plot the data right-side up (1st quadrant) so the y axis data goes from 0 up to +3600 at the top like a typical grade-school plot.
I've tried ylim = rev(y) but it returns an error...
I've also tried flipping the seq() command but no luck there.
list.vlevel = numeric(9) # placeholder
plot(
rep(0, length(list.vlevel)),
seq(1, length(list.vlevel)),
type = "n",
xlim = biaslim,
axes = F,
main = paste(list.var.bias[vv], list.score.bias[vv]),
xlab = "",
ylab = ""
)
abline(h = seq(1, length(list.vlevel)),
lty = 3,
col = 8)
axis(2,
labels = list.vlevel,
at = seq(length(list.vlevel), 1, -1),
las = 1)
axis(1)
box()
legend(
x = min(biasarray.var.runhour),
y = length(list.vlevel),
legend = expname,
lty = 3,
lwd = 3,
col = expcol
)
for (exp in seq(length(expname), 1, -1)) {
lines(
biasarray.var.runhour[exp, ],
seq(length(list.vlevel), 1, -1),
col = expcol[exp],
lwd = 3,
lty = 3
)
}
abline(v = 0, lty = 3)
The plot should end up in the first quadrant with yaxis values increasing from 0 upwards to +###.
The axis(2, ...) line draws the y axis. You can see that is the labels follow a descending sequence: seq(length(list.vlevel), 1, -1). seq(1, length(list.vlevel))
Similarly, inside lines(), probably you need to make the same change from seq(length(list.vlevel), 1, -1) to ``seq(1, length(list.vlevel))`
That's as much as we can tell with the info you've provided - can't run any of yoru code without values for all the constants you use, e.g., biasarray.var.runhour, list.var.bias, vv, etc.

Plot in R not recognizing pch numbers

I am trying to plot in base R with the regular plot() fcn. However, when passing a vector of which pch to use, it will not plot the pch, it will only plot the number '1' instead of the shape of the pch I am calling.
Generating some data (my real data has over 400 rows for both the loads and meta objects:
loads <- data.frame(PC1 = c(11.32, 13.18, 12.82, 24.70), PC2 = c(-23.05, -24.71, -20.28, 10.09))
row.names(loads) <- c("100_A", "100_B", "100_C", "100_Orig")
meta <- data.frame(pch = c(17, 17, 17, 16), color = c("red", "red", "blue", "blue"))
row.names(meta) <- row.names(loads)
To plot:
x <- loads[, 1] ; y <- loads[, 2]
pch <- meta$pch
col <- meta$color
plot(x, y,
col = col, pch = pch, cex = 2, lwd = 4,
xlab = paste("PC1"), ylab = paste("PC2"))
Now, this will graph the correct color (red and blue) in the order I have them in the vector; the real issue becomes the plotting the pch. Instead of a circle (pch = 16) or a triangle (pch = 17) it's plotting a red or blue number 1 instead! I have included a pic of what my data is actually doing.
Thinking that the pch vector I am passing cannot have quotes around it, I have removed the quotes with the following code:
pch <- meta$pch
pch <-as.vector(noquote(pch))
class(pch)
[1] "character"
However, this generates the same results (getting a number 1 plotted). Interestingly, when use this code, it works fine. It turns all my colors to blue, and I get nice blue circles.
plot(x, y,
col = "blue, pch = 16, cex = 2, lwd = 4,
xlab = paste("PC1"), ylab = paste("PC2"))
This tells me that the plot function isn't recognizing my long vector composed of pch 16 and 17's mixed in.
Alternatively, if I use the rep function to generate my pch vector, a test shows it works fine. But I have over 400 rows. I cannot manually type rep for each pch. I will be here for eternity typing that out.
Any suggestions on what to do?????
Try defining the col as character and the pch as numeric like this:
plot(x, y,
col = as.character(col), pch = as.numeric(pch), cex = 2, lwd = 4,
xlab = paste("PC1"), ylab = paste("PC2"))

Legend disappaers when plotting in R

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.

How to remove ticktypes with prespbox() (plot3d)

I am trying to construct a 3d-plot with grid lines using the function prespbox() and adding an additional plot of a figure based on some data using the surf3d() function.
This is my code:
library(plot3D) # added by comment
perspbox (x = seq(0.05, 0.5, length.out = nrow(M$x)),
y = seq(0.05, 0.5, length.out = ncol(M$y)), z,
bty = "u",
col.axis = "black", col.panel = "white", lwd.panel = 1,
col.grid = "grey", lwd.grid = 1, las=1,
phi = 40, theta = 40, col = NULL,ps=30,
nticks=4, ticktype="detailed", xlab="\n\n h1", ylab="\n\n h2", zlab="\n\n Fund",
colkey = TRUE,cex.axis=0.5, plot = TRUE)
surf3D(M$x, M$y, z, colvar = z, colkey = TRUE,add=TRUE)
My problem is, that the tick lines at the axis are too long.
Do you have an idea, how I could get rid of the tick lines? I want to keep the axis labels, but completely remove the tick lines. I already tried the tck option, but it seems not to have any effect here.

Resources