Related
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 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)
}
I am trying to use two different Y axis with the same X Axis and when I set both axes to false the Year wont show up
library(lubridate)
x <- dataset$Date
y <- dataset$AvgCostPerKwh
z <- dataset$ActualkWhSold
par(mar=c(5, 4, 4, 6) + 0.1)
plot(year(x),y, pch = 16, axes = FALSE, ylim = c(0.030,0.090), xlab = "", ylab = "",
type = "b", col="black", main = "Wholesale Power cost")
axis(2, ylim =(range(c(y))), col = "black", las =1)
mtext("$ per KWh", side = 2, line = 2.5)
box()
par(new = TRUE)
plot(year(x),z, pch = 15, xlab = "", ylab = "",ylim=c(5000000,45000000),
axes = FALSE, type="b", col="red")
mtext("Kwh's Sold", side=4, col="red", line=4)
axis(4, ylim=(range(c(z))), col = "red", las=1)
mtext("Year", side = 1, col="black",line=2.5)
legend("topleft", legend = c("AvgCostPerKwh", "ActualKwhSold"),
text.col = c("black", "red"),
pch=c(15,15),col=c("black", "red"))
Image 1
When I set one of the plots to true, I get overlapping values one one side, but the year on the bottom shows up.
library(lubridate)
x <- dataset$Date
y <- dataset$AvgCostPerKwh
z <- dataset$ActualkWhSold
par(mar=c(5, 4, 4, 6) + 0.1)
plot(year(x),y, pch = 16, axes = TRUE, ylim = c(0.030,0.090), xlab = "", ylab = "",
type = "b", col="black", main = "Wholesale Power cost")
axis(2, ylim =(range(c(y))), col = "black", las =1)
mtext("$ per KWh", side = 2, line = 2.5)
box()
par(new = TRUE)
plot(year(x),z, pch = 15, xlab = "", ylab = "",ylim=c(5000000,45000000),
axes = FALSE, type="b", col="red")
mtext("Kwh's Sold", side=4, col="red", line=4)
axis(4, ylim=(range(c(z))), col = "red", las=1)
mtext("Year", side = 1, col="black",line=2.5)
legend("topleft", legend = c("AvgCostPerKwh", "ActualKwhSold"),
text.col = c("black", "red"),
pch=c(15,15),col=c("black", "red"))
image 2
I am not sure, I have followed other examples on here about 2 Y axis and I can't get mine to work.
I have answered my own question for anyone interested, I forgot to add
year(as.Date(dataset$Date, format = "%m/%d/%Y"),"%Y")
at the top before adding
plot(year(x), y, pch=16, axes=FALSE, ylim=c(0.030,1), xlab="", ylab="",
type="b",col="black", main="Wholesale Power Cost")
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.
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))