Let say I want to draw this function f(x) = x^2. I want the graph of the function to be any color I want and everything else (except the axis) black. Is this possbile?
graphics.off()
par(bg = "black")
curve(x^2, -2, 2, col = "green", axes = FALSE, ann = FALSE)
title("my plot", xlab = "X", ylab = "X^2", col.lab = "white", col.main = "white")
box(col = "white")
axis(1, seq(-2, 2, 1), seq(-2, 2, 1), col = "white", col.axis = "white")
axis(2, seq(0, 4, 1), seq(0, 4, 1), col = "white", col.axis = "white")
Maybe you can ?plot to see how to make it. Below is just an example
plot(x<-seq(-5,5,0.1),
x**2,
main="square function",
ylab="f(x)=x^2",
type="l",
col=28)
Related
I'm using par(mfrow) to generate a multi-panel plot of three separate graph output objects. Sample code below represents a very simplified version of the objects I have.
How can I save these plots as a single object with ggsave? I've tried naming the par(mfrow) as an object and plotting it, but that doesn't seem to work.
Any advice on alternative ways of generating/saving a multi-panel plot is also welcome! Please let me know if I can clarify the question or example. Thanks!
par(mfrow = c(1,3), mar = c(10, 5, 5, 3), xpd = TRUE)
hist(x = rnorm(100), col = "skyblue", main = "X")
hist(x = rnorm(50), col = "green", main = "Y")
legend("bottom", c("Blue", "Green", "Purple"),
title = "Sample Data", horiz = TRUE, inset = c(0, -0.4),
col = c("skyblue", "green", "purple"), pch = rep(15,2),
bty = "n", pt.cex = 1.5, text.col = "black")
hist(x = rnorm(75), col = "purple", main = "Z")
I would suggest using svglite over svg if you want to edit the graph using Inkscape or a similar program since you will not be able to edit the text (change the text, font, or size) in a file produced by svg. Here is an example with a few edits to your original code:
library(svglite)
svglite("MyPlots.svg", width=8, height=6)
par(mfrow = c(1,3), mar = c(10, 5, 5, 3), xpd = TRUE, mgp=c(1.75, .75, 0))
hist(x = rnorm(100), col = "skyblue", main = "X")
hist(x = rnorm(50), col = "green", main = "Y")
legend("bottom", c("Blue", "Green", "Purple"),
title = "Sample Data", horiz = TRUE, inset = c(0, -0.2),
col = c("skyblue", "green", "purple"), pch = rep(15,2),
bty = "n", pt.cex = 1.5, text.col = "black")
hist(x = rnorm(75), col = "purple", main = "Z")
dev.off()
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")
Any tips to remove the zero labels in between the histogram bars?
hist(links$Survey_Duration, breaks = seq(0,50,5), main = "Survey Duration",
labels = TRUE, border = "black",
xlab = "Survey", ylim = c(0, 15), col = "gray", las = 1, xaxt='n')
axis(side=1, at=seq(0,50,5), labels=seq(0,50,5))
abline(v = mean(links$Survey_Duration), col = "royalblue", lwd = 1.5)
abline(v = median(links$Survey_Duration), col = "red", lwd = 1.5)
legend(x = "topright", c("Mean", "Median"), col = c("royalblue","red"),
lwd = c(1.5,1.5))
How about this?
# modify data so there's zero in one of the bins
mtcars$mpg <- ifelse(mtcars$mpg >= 25 & mtcars$mpg <= 30, NA, mtcars$mpg)
# save plot parameters
h <- hist(mtcars$mpg, plot = FALSE)
# produce plot
plot(h, ylim = c(0, 14))
# add labels manually, recoding zeros to nothing
text(h$mids, h$counts + 1, ifelse(h$counts == 0, "", h$counts))
A slightly different answer using the labeling in hist instead of adding text afterwards.
You do not provide your data, so I will use some data that is handy to illustrate.
The labels argument can specify the individual labels
H1 = hist(iris$Sepal.Length, breaks = 3:8, plot=FALSE)
BarLabels = H1$counts
BarLabels[BarLabels == 0] = ""
hist(iris$Sepal.Length, breaks = 3:8, labels = BarLabels)
Thanks #Daniel Anderson, it Ok now (Thumbs Up)
links$Survey_Duration <- ifelse(links$Survey_Duration > 15 &
links$Survey_Duration <= 25,
NA,
links$Survey_Duration)
h <- hist(links$Survey_Duration, breaks = seq(0,50,5), plot = FALSE)
plot(h, ylim = c(0, 14), main = "Survey Duration", xlab = "Time", col = "gray", las = 1)
text(h$mids, h$counts + 1, ifelse(h$counts == 0, "", h$counts))
axis(side=1, at=seq(0,50,5), labels=seq(0,50,5))
abline(v = mean(links$Survey_Duration), col = "royalblue", lwd = 1.5)
abline(v = median(links$Survey_Duration), col = "red", lwd = 1.5)
legend(x = "topright",
c("Mean", "Median"),
col = c("royalblue","red"),
lwd = c(1.5,1.5))
I am trying to get rid of the X and Y axes lines in R but it seems impossible. I am running the below code:
par(mar=c(5,5,5,5), cex.main = 1.5, col.main = "gray30", bg="white", cex =
0.8, family="Calibri", las=1)
plot(y1~x, col=blk_blue, type="l", lwd=2, ylim = y1_limits, xlab = x_label,
ylab = y1_label, las=1, axes=FALSE)
rect(par("usr")[1], par("usr")[3], par("usr")[2], par("usr")[4], col =
"gray90", border=NA)
grid(nx = 5, ny = 5, col = "white", lty = "solid", lwd = 1)
grid(nx = 10, ny = 10, col = "white", lty = "dotted", lwd = 1)
par(new=TRUE, las=1, bty="n")
plot(y1~x, col=blk_blue, type="l", lwd=2, ylim = y1_limits, xlab = x_label,
ylab = y1_label)
axis(side=1, col = "white", labels = FALSE)
axis(side=2, col = "grey90", labels = FALSE, ylim = y1_limits)
axis(side=3, col = "grey90", labels = FALSE, tick = FALSE)
mtext(y2_label, side=4, line=3)
par(new=TRUE, bty="n", las=1, bty="n")
plot(y2~x, col=blk_red, type="l", lwd = 2, axes=FALSE, xlab = "", ylab="",
ylim=y2_limits, las=1, xaxt="n")
axis(side = 4, ylim = y2_limits, col="grey90")
and the resulting chart looks like the below:
As you can see there are still some remainder black segments in the x-axis.
How can I remove those?
In your calls to plot(...), set axes=FALSE
In your calls to axis(...), set labels=TRUE
I believe this solves the issue, it prevents the black axis lines from being plotted in the first place but it still plots the numbers along the axes.
I've constructed an Ecdf plot from the Hmisc package with the following call:
require(Hmisc)
Ecdf(latency_targ1, group = CONDITION, lty = c(1, 2, 3, 4),
lwd = 4, label.curves = list(method = 'arrow',
keys = "lines", lwd = 2), xlim = c(0,500),
subtitles = FALSE, xlab = "Latency",
ylab = "Proportion latency <= x")
I have been unable to find how to change the size of the axis labels of the plot and the default size is rather small.
Try this:
Ecdf(latency_targ1, group = CONDITION, lty = c(1, 2, 3, 4),
lwd = 4, label.curves = list(method = 'arrow',
keys = "lines", lwd = 2), xlim = c(0,500),
subtitles = FALSE, xlab = "Latency",
ylab = "Proportion latency <= x",
cex.lab=1.5, xaxt="n", yaxt="n")
axis(2, cex.axis=1.5)
axis(1, cex.axis=1.5)