How to add axis labels to a simple.scatterplot (UsingR) - r

The package UsingR has some great out of the box plotting tools. However, using the simple.scatterplot function I can't figure out how to add axis labels.
library(UsingR)
simple.scatterplot(iris$Sepal.Length, iris$Sepal.Width, xlab='hello axis')
Error in plot.default(x, y, xlim = xrange, ylim = yrange, xlab = "x", :
formal argument "xlab" matched by multiple actual arguments
The graph can of course be produced without using the xlab arg, and I tried using the mtext command, but the label ends up in the middle of the page.
mtext(side=1, text='hello axis')
I tried editing the function itself without success either:
mysimple.scatterplot <- function (x, y)
{
def.par <- par(no.readonly = TRUE)
n <- length(x)
xhist <- hist(x, sqrt(n), plot = FALSE)
yhist <- hist(y, sqrt(n), plot = FALSE)
top <- max(c(xhist$counts, yhist$counts))
xrange <- c(min(x), max(x))
yrange <- c(min(y), max(y))
nf <- layout(matrix(c(2, 0, 1, 3), 2, 2, TRUE), c(3, 1),
c(1, 3), TRUE)
layout.show(nf)
par(mar = c(3, 3, 1, 1))
plot(x, y, xlim = xrange, ylim = yrange, xlab = 'Hello X-axis', ylab = 'Hello Y-axis',
...)
abline(lm(y ~ x))
par(mar = c(0, 3, 1, 1))
barplot(xhist$counts, axes = FALSE, ylim = c(0, top), space = 0,
col = gray(0.95))
par(mar = c(3, 0, 1, 1))
barplot(yhist$counts, axes = FALSE, xlim = c(0, top), space = 0,
col = gray(0.95), horiz = TRUE)
par(def.par)
}

The cause is here:
layout.show(nf)
par(mar = c(3, 3, 1, 1)) # <-- Here
plot(x, y, xlim = xrange, ylim = yrange, xlab = 'Hello X-axis', ylab = 'Hello Y-axis',
...)
The margins are changed to a small value thus the labels are not in the plot, they are outside of the area.

Related

Rotate plot 90deg for marginal distribution

I am trying to get marginal distribution plots in base R (if not possible, non-base R code is okay, but only for doing the rotating).
How do you rotate the y-axis marginal plot (in yellow)?
The code I have to make this plot:
# plot layout
layout_mat <- matrix(c(2, 0,
1, 3),
nrow = 2,
byrow = T)
layout(layout_mat, c(3, 1), c(1, 3))
par(mar = c(3,3,1,1))
# main scatterplot
plot(x = mtcars[order(mtcars$qsec), c("wt", "mpg")],
xlab = "Vehicle Weight (1000 lbs)",
ylab = "Miles Per Gallon (MPG)",
pch = 16,
cex = seq(3.5, 1.25, length.out = nrow(mtcars)),
col = rgb(0, 0, 0, .5),
axes = F,
xlim = c(1, 6),
ylim = c(8, 35))
box(lwd = 1.5)
axis(side = 1, at = 1:6, labels = 1:6, lwd = 0, lwd.ticks = 1)
axis(side = 2, at = seq(10, 35, 5), label = seq(10, 35, 5), lwd = 0, lwd.ticks = 1, las = 1)
dx <- density(mtcars[order(mtcars$qsec), "wt"])
dy <- density(mtcars[order(mtcars$qsec), "mpg"])
# x axis plot
par(mar = c(0,3,1,1))
plot(dx, axes = F, main = "", xlab= "", ylab = "", lwd =2)
# y-axis plot
par(mar = c(3,0,1,1))
plot(dy, axes = F, main = "", xlab= "", ylab = "", lwd =2)
rect(xleft = par("usr")[1],
ybottom = par("usr")[3],
xright = par("usr")[2],
ytop = par("usr")[4],
col = rgb(235, 216, 52, .5*255, maxColorValue = 255))
Thanks to #user20650 for this answer
Passing the y-axis points for y's density to x and the x-axis points for y's density to y will plot it flipped:
plot(dy$y, dy$x, type="l", axes = F, main = "", xlab= "", ylab = "", lwd = 2)

Plot density lines without histogram

I want to plot density lines without showing the histogram, I used this code:
hist(www, prob=TRUE, xlab = "X", main = "Plot",xlim=c(0,11), ylim=c(0,1), breaks =100)
lines(density(x, adjust=5), col="red", lwd=2)
lines(density(y, adjust=5), col="blue", lwd=2)
lines(density(z, adjust=5), col="green", lwd=2)
And the result is showing in the the picture.
How can I remove the Histogram? Thank you in advance!
You could use plot(density(...)) instead of hist:
set.seed(123)
x <- rnorm(100, 0, 1)
y <- rnorm(100, 0.5, 2)
z <- rnorm(100, 1, 1)
dens <- lapply(list(x=x, y=y, z=z), density)
ran <- apply(do.call(rbind, sapply(dens, function(i) list(data.frame(x=range(i$x), y=range(i$y))))), 2, range)
plot(dens[[1]], xlim=ran[,1], ylim=ran[,2], type = 'n', main="Density")
lapply(seq_along(dens), function(i) lines(dens[[i]], col=i))
legend("topright", names(dens), col=seq_along(dens), lty=1)
Created on 2021-01-31 by the reprex package (v1.0.0)
Even easier is plotting with the ggplot2 package:
library(ggplot2)
dat <-data.frame(group=unlist(lapply(c("x", "y", "z"), function(i) rep(i, length(get(i))))),
value=c(x, y, z))
ggplot(dat, aes(x=value, colour=group))+
geom_density()
Using three toy vectors, try this:
x <- rnorm(100, 0, 1)
y <- rnorm(100, 0.5, 2)
z <- rnorm(100, 1, 1)
plot(density(x, adjust = 5), col = "red", lwd = 2,
xlim = c(-20, 20), ylim = c(0, 0.25), xlab = "X")
par(new=T)
plot(density(y, adjust = 5), col = "blue", lwd = 2,
xlim = c(-20, 20), ylim = c(0, 0.25), xlab = "")
par(new=T)
plot(density(z, adjust = 5), col = "green", lwd = 2,
xlim = c(-20, 20), ylim = c(0, 0.25), xlab = "")
You will need to adjust xlim and ylim in the right way

Plotting CDF in histogram form in R

I am not an expert on stats, but I've been trying to plot a cdf out of an array of points. I've tried R and Python both. These are my example set of points:(1.5,1.5,2.5,3.5,3.5,3.5,4.5,5.5,5.5,6)
Using the ecdf function in R, I manage to get this:
This was my code:
data <- c(1.5,1.5,2.5,3.5,3.5,3.5,4.5,5.5,5.5,6)
plot(ecdf(data))
Is there a way to get the same plotted as a histogram or would that be fundamentally wrong?
Is this what you mean?
par(mar = c(5,5,2,5))
data <- c(1.5,1.5,2.5,3.5,3.5,3.5,4.5,5.5,5.5,6)
h <- hist(
data,
breaks = seq(0, 10, 1),
xlim = c(0,10))
par(new = T)
ec <- ecdf(data)
plot(x = h$mids, y=ec(h$mids)*max(h$counts), col = rgb(0,0,0,alpha=0), axes=F, xlab=NA, ylab=NA)
lines(x = h$mids, y=ec(h$mids)*max(h$counts), col ='red')
axis(4, at=seq(from = 0, to = max(h$counts), length.out = 11), labels=seq(0, 1, 0.1), col = 'red', col.axis = 'red')
mtext(side = 4, line = 3, 'Cumulative Density', col = 'red')

lines() function is not connecting the actual points in the graph

I have been practicing the graphs and how to plot in R with the following code
theta = 1:100
x = sin(theta)
y = cos(theta)
op = par(bg = 'white', mar = rep(1, 4))
plot.new()
plot(x,y)
plot.window(xlim = c(-1, 1), ylim = c(-1, 1))
lines(x, y, col = hsv(0.95, 1, 1))
to get the following output
Now I wanted to trace just how exactly the lines connect to form this pattern and so used the following code.
theta = 1:3
x = sin(theta)
y = cos(theta)
op = par(bg = 'white', mar = rep(1, 4))
plot(x,y, xlab = "Sin", ylab = "Cos", type = "p")
plot.window(xlim = c(-1, 1), ylim = c(-1, 1))
lines(x, y, col = hsv(0.95, 1, 1))
And I get the following output
Shouldn't the lines connect the dots? I get the output where the lines connect the dots using this code
theta = 1:3
x = sin(theta)
y = cos(theta)
op = par(bg = 'white', mar = rep(1, 4))
plot(x,y, xlab = "Sin", ylab = "Cos", type = "l")
And if I add the points later, they don't work as well.
theta = 1:3
x = sin(theta)
y = cos(theta)
op = par(bg = 'white', mar = rep(1, 4))
plot(x,y, xlab = "Sin", ylab = "Cos", type = "l")
plot.window(xlim = c(-1, 1), ylim = c(-1, 1))
points(x, y)
Here is the output.
Why is there such difference in output?
You are setting plot.window limits after you have plotted your points plot(...) and before plotting your lines lines(...) causing the mismatch. Try the following:
theta = 1:3
x = sin(theta)
y = cos(theta)
op = par(bg = 'white', mar = rep(1, 4))
plot.window(xlim = c(-1, 1), ylim = c(-1, 1))
plot(x,y, xlab = "Sin", ylab = "Cos", type = "p")
lines(x, y, col = hsv(0.95, 1, 1))

How can I set the margin parameters of a plot graphic to avoid labels being cut?

I'm plotting a series of data, using the following code:
x <- 2008:2016
y <- c(757221, 768845, 672496, 672387, 682506, 657828, 630625, 639334, 1178876)
par(mar = c(0, 0, 0, 0))
par(oma = c(2.5, 0.5, 0.5, 0.5))
plot(x, y, bty = "n", yaxt = "n", xlab = "", ylab = "", pch = 18, col = "#5889C1", xaxp = c(2008, 2016, 8), cex = 1.5)
yl <- format(y, big.mark = ".", decimal.mark = ",")
poslab <- c(1, 3, 1, 1, 3, 3, 1, 1, 3)
text(x, y, labels = yl, pos = poslab, cex = 0.9)
lines(x, y, col = "#5889C1", lwd = 3)
But some labels, in the borders of the screen are not entirely shown (unfortunately, I couldn’t upload an image, but will continue trying): anyone who rerun this code will see the first label cut at left, the seventh cut at bottom and the ninth (last) cut at top.
So, I wish to know how can I reset the margin parameters to avoid it and allow all labels to be entirely shown.

Resources