How to remove text that appeared in the chart? - r

Am trying to plot 2 different columns of a time series data type in a single plot, however the year appears which seems to block the view of the chart. How do I remove the year in the chart so that I can see the charts nicely?
The below code is how i created the chart
data('fertil3')
fertil = ts(fertil3, frequency = 1, start = 1913)
plot(x = fertil[,"year"], y = fertil[,"gfr"], type="l", col="red", ylim = c(0, 250), xlab="Time", ylab="")
par(new=TRUE)
plot(x = fertil[,"year"], y = fertil[,"pe_1"], type="l", col="blue", ylim = c(0, 250),xlab="Time", ylab="")

You should convert the time series object ts to as.numeric which then is a vector and can be used to plot it like lines like this:
library(wooldridge)
data('fertil3')
fertil = ts(fertil3, frequency = 1, start = 1913)
plot(x = as.numeric(fertil[,"year"]), y = as.numeric(fertil[,"gfr"]), type="l", col="red", ylim = c(0, 250), xlab="Time", ylab="")
par(new=TRUE)
plot(x = as.numeric(fertil[,"year"]), y = as.numeric(fertil[,"pe_1"]), type="l", col="blue", ylim = c(0, 250),xlab="Time", ylab="")
Created on 2022-10-15 with reprex v2.0.2

Perhaps plot as a base R time series...
Saves a bit of typing too!
library(wooldridge)
plot.ts(fertil[, c("gfr", "pe_1")], plot.type = "single", col=c("red", "blue"), ylim = c(0, 250), xlab="Time", ylab="" )
Created on 2022-10-15 with reprex v2.0.2

Related

How to change shape of specific point in a plot in R

How can I change the shape of specific points in an R plot? I have a plot with four points, and I would like to change the shape of two points (0, 0.4991) and (1, 1.2258)in the following plot:
x = c(0,0,1,1,1)
y = c(0.4991,1.1423,1.2258,1.158,0.5148)
dat<-cbind(x,y)
myTicks<-c(0,1)
plot(dat[,1],dat[,2], yaxt="n", xaxt="n", xlab="", ylab="")
abline(0.4991,0.7267)
abline(1.1423,0.0157)
abline(0.4991,0.0157,lty=2)
axis(side = 1, at = myTicks)
axis(side = 2, at = myTicks)
You can specify the shape with pch parameter, which accept vectors, for example
x = c(0,0,1,1,1)
y = c(0.4991,1.1423,1.2258,1.158,0.5148)
point_shape = c(10,15,10,15,15)
dat<-data.frame(x,y,point_shape)
myTicks<-c(0,1)
plot(dat[,1],dat[,2], yaxt="n", xaxt="n", xlab="", ylab="", pch = dat$point_shape)
abline(0.4991,0.7267)
abline(1.1423,0.0157)
abline(0.4991,0.0157,lty=2)
axis(side = 1, at = myTicks)
axis(side = 2, at = myTicks)

R: Make plot axis as in histogram

I want the y axis in regular plot() function to start at the bottom of the plot area similar to hist() function. In other words I would like to have zeros of both axis at the same level. Here is my working example
set.seed(1)
data <- data.frame(
type = as.factor(sample(c('A', 'B', 'C'), size = 100, replace = T)),
value = rexp(100, 1/3)
)
plot(data$type)
par(new=TRUE)
plot(tapply(data$value, data$type, mean),
xaxt="n", yaxt="n", xlab="", ylab="",
xlim=c(0.55,3.45), ylim=c(0, 5), bty='n', pch=24, bg='black')
axis(4)
I tried to use parameter yaxs = 'i' in the plot() function, but it moved the axis too low. Is there any solution to this?
Find one variant
set ylim in first plot
try
plot(data$type, yaxs = 'i',ylim=c(0,max(apply(data, 2, table)[[1]])))
par(new=TRUE)
plot(tapply(data$value, data$type, mean),
xaxt="n", yaxt="n", xlab="", ylab="",
xlim=c(0.55,3.45), ylim=c(0, 5), bty='n', pch=24, bg='black',yaxs = 'i')
axis(4)
axis(1,at=c(0,5)) #only for show that one lvl

Time series double y axis plot with x axis in “year”-“month” in R

I have the following code for a double y axis plot in r. Everything works fine, but I want to change the x axis from every 18 months to every 12 months. I have tried every "axis(side=1, at=....)" I could think of. Any random number between 0:1.5 will work for "Data$Monthly_Gen" and 0:100 for Data$Ave_GenXXXX" for reproducing. Thanks.
Data2 <- ts(Data$Monthly_Gen, start=c(2005,1),end=c(2012,12),frequency=12)
Data2B <- ts(Data$Ave_Gen_MonthSOCO, start=c(2005,1),end=c(2012,12),frequency=12)
Data2C <- ts(Data$Ave_Gen_MonthTVA, start=c(2005,1),end=c(2012,12),frequency=12)
Data2D<-ts(Data$Monthly_Gen_Othersx10,start=c(2005,1),end=c(2012,12),frequency=12)
Data2E <- ts(Data$Monthly_Gen_Othersx9, start=c(2005,1),end=c(2012,12),frequency=12)
par(mar=c(4, 4, 2, 4) + 0.1)
plot(as.xts(Data2), major.format = "%Y-%m",ylab="",las=1,ylim=c(0,2))
lines(as.xts(Data2D), major.format = "%Y-%m", xlab="", ylab="",
type="l", col="black",main="",lty="dotted" ,lwd=2)
lines(as.xts(Data2E), major.format = "%Y-%m", xlab="", ylab="",
type="l", col="black",main="",lty="longdash" ,lwd=2)
mtext("Generation",side=2,line=3)
box()
par(new=TRUE)
plot(as.xts(Data2B), major.format = "%Y-%m", xlab="", ylab="",
axes=FALSE, type="l", col="#E69F00",main="",lwd=2,ylim=c(0,130))
mtext("Monthly Average Lambda",side=4,col="black",line=2.5)
axis(4, col="black",col.axis="black",las=1)
lines(as.xts(Data2C), major.format = "%Y-%m", xlab="", ylab="",
type="l", col="#56B4E9",main="",lwd=2.5)
mtext("Date",side=1,col="black",line=3)
legend("topright",legend=c("Total Generation","X1 Generation","X2 Generation","Area Lambda","X2 Area Lambda"),text.col=c("black","black","black","#E69F00","#56B4E9"),col=c("black","black","black","#E69F00","#56B4E9"),cex=.75,lty=c("solid","longdash","dotted","solid","solid"))`
This is easy to do with zoo. In my opinion, zoo is more powerful than ts.
#Create zoo series
Data2 <- zooreg(runif(96,0,1.5), start = as.yearmon(2005),end = as.yearmon(2012), freq = 12)
Data2B <- zooreg(runif(96,0,100), start = as.yearmon(2005),end = as.yearmon(2012), freq = 12)
#Create 12-month sequence for axis
twelve <-seq(1,length(Data2),12)
plot(Data2,ylab="",las=1,ylim=c(0,2),xaxt = "n")
#add x-axis at 12 months
axis(1,at=index(Data2)[twelve],labels=format(index(Data2)[twelve],"%Y-%m"))
par(new=TRUE)
plot(Data2B,xlab="",ylab="",las=1,ylim=c(0,130),xaxt = "n",yaxt = "n",col="blue")
#add yy axis
axis(4, col="black",col.axis="black",las=1)
Here's a solution using only the base plot() functions. I think what you really want is seq.Date() for something like this
set.seed(123)
nn <- 7*12 # months
x1 <- ts(rnorm(nn),start=c(2005,1),freq=12)
x2 <- ts(rpois(nn,20),start=c(2005,1),freq=12)
tt <- seq.Date(from=as.Date("2005-01-01"),by="month",length.out=nn)
plot(tt, x1, col="blue", type="l", xaxt="n", xlab="", ylab="")
par(new=TRUE)
plot(tt, x2, col="red", type="l", xaxt="n", yaxt="n", xlab="", ylab="")
axis(4)
dates <- seq(from=as.Date("2005-01-01"), to=as.Date("2012-12-01"), by="18 month")
axis(1, at=dates, labels = format(dates, "%Y-%m"))

overlaid plots using different axes in R

I am trying to overlay two graphs onto the same axes. I set up my axes limits and labels first, but then when I plot the graphs they resize and are not on my pre-determined scale.
I have pared my code down into a simple example. You can see that 100 and 10 are showing up at the same place on the y axis. Please help!
x<- 1:3
y1<- c(100, 75, 20)
y2<- c(10, 9, 4)
plot.new()
plot(0, type="n",
xlim=c(1,max(x)), ylim=c(0,max(y1,y2)),
xlab= "x label", ylab= "y label", main= "This stupid graph doesn't work!")
par(new=TRUE)
par(new=TRUE)
plot(x,y1, type="b", pch=19, col="orchid",
axes=FALSE, ann=FALSE)
par(new=TRUE)
plot(x,y2, type="b", pch=19, col="slateblue",
axes=FALSE, ann=FALSE)
legend("topright",c("This is","Annoying"), col=c("orchid","slateblue"), pch=19)
You want to use lines to add the second line, meanwhile making sure that ylim allows for all the values being plotted to fit within the plotting region.
plot(y1 ~ x, ylim = range(c(y1, y2)), xlab = "x label", ylab = "y label",
main = "This one might work!", type = 'b', pch = 19, col = "orchid")
lines(y2, type = 'b', pch = 19, col = 'slateblue')
legend("topright", c("R is", "awesome"), col = c("orchid","slateblue"), pch = 19)

R: plotting untransformed data on a log x axis (similar to plotting on log graph paper)

I have 3 sets of data that I am trying to plot on a single plot. The first data set x values range from ~ 1 to 1700 whereas the other two data sets x values are less than 20. Therefore I want to plot them on a log axis to show variations in all the data sets. However I do not want to transform the data as I want to be able to read the values off the graph. The x axis labels I would like are 1, 10, 100 and 1000 all equally spaced. Does anyone know how to do this? I can only find examples where the data is log as well as the axis. I have attached the code I am currently using below:
Thanks in advance for any help given.
Holly
Stats_nineteen<-read.csv('C:/Users/Holly/Documents/Software Manuals/R Stuff/Stats_nineteen.csv')
attach(Stats_nineteen)
x<-Max
x1<-Min
x2<-Max
y1<-Depth
y2<-Depth
par(bg="white")
par(xlog=TRUE)
plot(x2,y1, type="n", ylim=c(555,0), log="x", axes=FALSE, ann=FALSE)
box()
axis(3, at=c(1,10,100,1000), label=c(1,10,100,1000), pos=0, cex.axis=0.6)
axis(1, at=c(1,10,100,1000), label=c(1,10,100,1000), cex.axis=0.6)
axis(2, at=c(600,550,500,450,400,350,300,250,200,150,100,50,0), label=c
(600,"",500,"",400,"",300,"",200,"",100,"",0), cex.axis=0.6)
mtext("CLAST SIZE / mm", side=3, line=1, cex=0.6, las=0, col="black")
mtext("DEPTH / m", side=2, line=2, cex=0.6, las=0, col="black")
grid(nx = NULL, ny = NULL, col = "lightgray", lty = "solid",
lwd = par("lwd"), equilogs = TRUE)
par(new=TRUE)
lines(x1,y1, col="black", lty="solid", lwd=1)
lines(x2,y2, col="black", lty="solid", lwd=1)
polygon(c(x1,rev(x2)), c(y1,rev(y2)), col="grey", border="black")
par(new=TRUE)
plot(x=Average,y=Depth, type="o",
bg="red", cex=0.5, pch=21,
col="red", lty="solid",
axes=FALSE, xlim=c(0,1670), ylim=c(555,0),
ylab = "",xlab = "")
par(new=TRUE)
plot(x=Mode,y=Depth, type="o",
bg="blue", cex=0.5, pch=21,
col="blue", lty="solid",
axes=FALSE, xlim=c(0,1670), ylim=c(555,0),
ylab = "",xlab = "")
You can do this in ggplot using scale_x_log
so something like:
myplot <- ggplot( StatsNinetee,
aes (x = myResponse,
y = myPredictor,
groups = myGroupingVariable) ) +
geom_point() +
scale_x_log()
myplot
also, avoid attach() it can give odd behavior.

Resources