I am trying to make the following graph with R-plot:
I have the following code in R:
x = 0:8
f = log2(x)
plot(x, f, main="Função Logarítmica", xlab="X - Abscissas", ylab="Y - Ordenadas", t='l', ylim=c(-3,3), xlim=c(0,8), col=4, axes=F)
axis(1, pos=0, at=seq(from=0, to=8, by=0.2))
axis(2, pos=0)
# Inclui linhas de grade
abline(h=seq(-3,3,0.5),v=seq(-3,8,0.5),lty=3,col="gray", lwd=2)
The graph generated with the R-plot:
How should I make my R graph look like the figure?
I'm not sure what it is you want to change in the figure, but here's a reasonable replica:
x = seq(0.1, 8, 0.1)
f = log2(x)
plot(x, f, main = "Função Exponencial",
xlab = "X - Abscissas", ylab = "Y - Ordenadas", t = 'n',
ylim= c(-3, 3), xlim = c(0, 8), col = 4, axes = FALSE)
polygon(x = c(0, 8, 8, 0), y = c(-3, -3, 3, 3), col= "#e3e9ff")
abline(h = seq(-3, 3, 0.1), v = seq(-3, 8, 0.1), col = "white", lwd = 2)
lines(x, f, lwd = 2, col = "red3")
axis(1, pos = 0, at = 0:8)
axis(2, pos = 0)
text(label = c("\u215b", "\u00bc", "\u00bd"),
c(0.125, 0.25, 0.5), c(0.2, -0.2, -0.2), cex = 1.3)
for(i in seq(-3, 3)) {
lines(c(0, 2^i, 2^i), c(i, i, 0), lty = 2)
points(2^i, i, pch = 16, cex = 1.5)
}
Related
how would I go about plotting this in R?
Here is the code I have already tried but it doesn't work!
x <- seq(-5, 5, length=200)
y <- dnorm(x, mean= -1, sd=1)
t = sqrt(0.5)
z <- seq(-5, 5, length=200)
a <- dnorm(z, mean=1, sd=t)
plot(z, a, type="l", lwd=2, col='red', xlim = c(-5,5), ylim = c(0,0.65), xlab = "Observed Value", ylab = "Probability Density")
lines(x,y, col='blue', lwd=2)
abline(v = 0, col = "darkgreen")
Modify your call to plot as follows:
plot(
z,
a,
type = "l",
lwd = 2,
col = 'red',
xlim = c(-5, 5),
ylim = c(0, 0.65),
xlab = "Observed Value",
ylab = "Probability Density",
main = expression(paste(pi[1], "= .3, ", pi[2], "= .7"))
)
lines(x, y, col = 'blue', lwd = 2)
abline(v = 0, col = "darkgreen", lty=2, lwd = 1.5)
Given the following empty plot:
plot(1, type="n", xlab="x1", ylab="x2", xlim=c(0, 10), ylim=c(0, 10), axes = F)
axis(1, seq(0,10,1), pos = 0)
axis(2, seq(0,10,1), pos = 0)
lines(x = c(0,10), y = c(10,10))
lines(x = c(10,10), y = c(0,10))
I would like to plot a smooth curve in which x1*x2 = 38, assuming x1 and x2 are both between 0 and 10.
What kind of function could I use to accomplish this?
You may try
plot(1, type="n", xlab="x1", ylab="x2", xlim=c(0, 10), ylim=c(0, 10), axes = F)
axis(1, seq(0,10,1), pos = 0)
axis(2, seq(0,10,1), pos = 0)
lines(x = c(0,10), y = c(10,10))
lines(x = c(10,10), y = c(0,10))
t <- seq(from = 3.8, to = 10, by = .1)
lines(x = t, y = 38/t)
Using curve.
curve(38/x, xlim=c(0, 10), ylim=c(0, 10), xlab='x1', ylab='x2')
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)
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
I have one plot and i need to adjust legends in a row. How can i do it?
plot(x,y)
legend(c("x","y"))
I need legend should be in one line
----- x --------- y
Regards
You want to set horiz=TRUE in legend. Here's a comparison of the default behavior (horiz=FALSE) to horiz=TRUE.
This plot is based on the second example from the legend documentation:
layout(matrix(1:2,nrow=1))
# `horiz=FALSE` (default behavior)
plot(x, sin(x), type = "l", ylim = c(-1.2, 1.8), col = 3, lty = 2, main="horiz=FALSE (Default)")
points(x, cos(x), pch = 3, col = 4)
lines(x, tan(x), type = "b", lty = 1, pch = 4, col = 6)
legend(-1, 1.9, c("sin", "cos", "tan"), col = c(3, 4, 6),
text.col = "green4", lty = c(2, -1, 1), pch = c(NA, 3, 4),
merge = TRUE, bg = "gray90")
# `horiz=TRUE`
plot(x, sin(x), type = "l", ylim = c(-1.2, 1.8), col = 3, lty = 2, main="horiz=TRUE")
points(x, cos(x), pch = 3, col = 4)
lines(x, tan(x), type = "b", lty = 1, pch = 4, col = 6)
legend(-1, 1.9, c("sin", "cos", "tan"), col = c(3, 4, 6),
text.col = "green4", lty = c(2, -1, 1), pch = c(NA, 3, 4),
merge = TRUE, bg = "gray90", horiz=TRUE)