Plot density lines without histogram - r

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

Related

Creating a simple Graph in R

how can I create this in R?:
I can't seem to get the data.frame right in order to display the variables correctly.
It can be created step by step with "base R" functions. It can be done with data frames, but simple vectors will also do here.
create data sets for A and B
plot both with steps type="s"
optionally: suppress default axes and make your own, with labels from 0.95 to 1.00 only
add a legend
optionally: add the two hand-drawn red points
x <- seq(0.94, 1.0, 0.01)
A <- seq(0, 1, length.out = length(x))
x2 <- c(0.94, 1, 1.01)
B <- c(0, 1, 1)
plot(x, A, type = "s", xlim = c(0.94, 1.01), ylab = "F(x)", axes = FALSE)
lines(x2, B, type = "s", col = "blue")
axis(1, at=pretty(c(0.95, 1)))
axis(2)
box()
legend("topleft", lty=1,
legend = c("Lotterie A", "Lotterie B"),
col = c("black", "blue"))
points(c(1, 1), c(0, 1), col = "red")
Something like this?
df1 <- data.frame(x = rep(seq(0.95:1, by = 0.01), 2), cat = "A")
df1 <- df1[order(df1$x), ]
y <- c(rep(seq(0, 1, by = 1/6), 2))
df1$y <- sort(y)[2:13]
df2 <- data.frame(x = c(0, 1, 1, 1.5), y = c(0, 0, 1, 1), cat = "B")
df <- rbind(df1, df2)
library(ggplot2)
ggplot(df, aes(x, y, colour = cat)) +
geom_path() +
coord_cartesian(xlim = c(0.95, 1)) +
scale_y_continuous(breaks = seq(0, 1, by = 0.2))

Plot curved line on R plot using a condition given two vectors

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')

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')

Custom label on secondary y-axis in R

How can I put a custom label on the secondary y-axis in R using the following code?
library(zoo)
#--------------random data
x.Date <- as.Date(paste(2003, 02, c(1, 3, 7, 9, 14), sep = "-"))
z <- zoo(rnorm(5), x.Date)
z2 <- zoo(rnorm(5, sd = 0.2), x.Date)
#--------------create plot
png(filename = "Google.png", width = 480, height = 480)
plot(z, type="l", xlab="Year", lt=1, lwd=1, ylab="Google Trend")
grid(nx = NULL, ny = NULL, col = "lightgray", lty = "dotted",
lwd = par("lwd"), equilogs = TRUE)
par(new=TRUE)
plot(z2, type="l", ann=FALSE, lt=1, lwd=3, yaxt="n")
axis(4, ylab="Test") # Here?!
legend("topright", c("z", "z2"), lty=c(1,1),
lwd=c(1,3),col=c("black","black"),
box.col="black",bg="white")
title("Google")
dev.off()
The label "Test" does not appear in my plot...
Furthermore, how to overwrite the gridlines with the z2 and the z lines? In other words, how to bring the plotted lines to the "front"?
You need mtext for this to work:
library(zoo)
#--------------random data
x.Date <- as.Date(paste(2003, 02, c(1, 3, 7, 9, 14), sep = "-"))
z <- zoo(rnorm(5), x.Date)
z2 <- zoo(rnorm(5, sd = 0.2), x.Date)
#--------------create plot
#png(filename = "Google.png", width = 480, height = 480)
plot(z, type="l", xlab="Year", lt=1, lwd=1, ylab="Google Trend")
grid(nx = NULL, ny = NULL, col = "lightgray", lty = "dotted",
lwd = par("lwd"), equilogs = TRUE)
par(new=TRUE)
plot(z2, type="l", ann=FALSE, lt=1, lwd=3, yaxt="n")
axis(4)
#------here is the only part I added
#you specify the text, location and usually line=2 to place next to
#the y-axis labels
mtext('Test', side=4, line=2)
#-----------------------------------
legend("topright", c("z", "z2"), lty=c(1,1),
lwd=c(1,3),col=c("black","black"),
box.col="black",bg="white")
title("Google")
#dev.off()
Output:
As for z and z2 they dont seem to be behind the gridlines here.

How to show sample error bars in the legend in R?

I'm plotting data with colored error bars in R. I'd like to show "sample error bars" (with the colour used in the plot) in the legend, but how?
library("Hmisc")
d1=data.frame(x=c(1,2,3,4,5), meanY=c(1,2,3,4,5), sdY=c(1,1,1,1,1))
d2=data.frame(x=c(1,2,3,4,5), meanY=c(2.1,3.3,4.1,5.2,6.1), sdY=c(1.3,1.2,1.4,1.1,1.2))
plot(1, 1, type="n", xlab="X values", ylab="Y values", xlim=c(1,5), ylim=c(0,7))
with ( data = d1, expr = Hmisc::errbar(x, meanY, meanY+sdY, meanY-sdY, pch=1, cex=.5, cap=.0025, add=T, errbar.col="red") )
with ( data = d2, expr = Hmisc::errbar(x, meanY, meanY+sdY, meanY-sdY, pch=1, cex=.5, cap=.0025, add=T, errbar.col="green") )
legend(x="bottomright", legend=c("d1", "d2"), pch=1, pt.cex=.5)
Somewhat manual build of legend...
# bind data together to simplify plot code
df <- rbind(d1, d2)
# plot
with(df,
errbar(x = x + c(rep(0.05, nrow(d1)), rep(-0.05, nrow(d2)), # dodge points to avoid overplotting
y = meanY,
yplus = meanY + sdY,
yminus = meanY - sdY,
pch = 1, cex = 0.5, cap = .0025,
errbar.col = rep(c("red", "green"), times = c(nrow(d1), nrow(d2))),
xlab = "X values", ylab = "Y values",
xlim = c(1, 5), ylim = c(0, 7)))
# create data for legend
df_legend <- data.frame(x <- c(4.5, 4.5),
y <- c(1, 2),
sdy <- c(0.3, 0.3))
# add symbols to legend
with(df_legend,
errbar(x = x,
y = y,
yplus = y + sdy,
yminus = y - sdy,
pch = 1, cex =.5, cap = .0025,
errbar.col = c("red", "green"),
add = TRUE))
# add text to legend
with(df_legend,
text(x = x + 0.2,
y = y,
labels = c("d2", "d1")))
# add box
with(df_legend,
rect(xleft = x - 0.2,
ybottom = y[1] - 0.5,
xright = x + 0.4,
ytop = y[2] + 0.5))

Resources