par(mar=c(5,5,2,5), xpd=TRUE)
x <- seq(0,5,0.5)
y <- seq(0,5,0.5)
plot(x,y, xlab="", ylab="")
Is there a way to replace the x-axis labels (0,1,2,3,4,5) with symbols pch=2,pch=4,pch=6,pch=8,pch=10,pch=12?
A fairly manual solution, but that's often what you get with base graphics:
par(mar=c(5,5,2,5), xpd=TRUE)
x <- seq(0,5,0.5)
y <- seq(0,5,0.5)
plot(x,y, xlab="", ylab="",xaxt = "n")
axis(side = 1,at = 0:5,labels = FALSE)
points(x = 0:5,y = rep(-0.5,6),pch = c(2,4,6,8,10,12))
Related
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)
I want to plot my points on a graph and then show the density distribution on the x-axis and on the y-axis at the same time.
I'm able to do it on the x axis but not on the y axis.
par(mfrow=c(1,1))
plot(rnorm(100))
par(new=TRUE)
plot(density(rnorm(100,10,123)), ann = FALSE, xlab = "", ylab ="",xaxt='n', yaxt='n')
par(new=TRUE)
plot(density(rnorm(100, 10,12)), col = "red", ann = FALSE, xlab = "", ylab ="",xaxt='n', yaxt='n')
There is no reason you can't.
set.seed(0)
d1 <- density(rnorm(100, 10, 123))
d2 <- density(rnorm(100, 10, 130))
## shared x, y, range / limit
xlim <- c(min(d1$x[1], d2$x[1]), max(d1$x[512], d2$x[512])) ## default: n = 512
ylim <- c(0, max(d1$y, d2$y))
## conventional plot
plot(d1$x, d1$y, type = "l", xlim = xlim, ylim = ylim)
lines(d2$x, d2$y, col = 2)
## rotated plot
plot(d1$y, d1$x, type = "l", xlim = ylim, ylim = xlim)
lines(d2$y, d2$x, col = 2)
Remarks:
never use par(new = TRUE); set xlim and ylim yourself;
customize the plot with title, axis display yourself.
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"))
I have created a barplot with 24 bars on the x-axis (0-23) and background shading using the following code:
#Data
Hours = seq(from=0, to=23)
Mean = rnorm(24, mean=5, sd=2)
#Create number seq for tick mark locations
at_tick = seq_len(length(Hours)+1)
#Plot with background rectangle shading
x=barplot(Mean,names.arg=Hours, border="white", ylab="Freq", xlab="Hour",
ylim=c(0,10), axes=FALSE, space=0, col="grey50")
X = c(0,5)
Y = c(0,10)
rect(X[1], Y[1], X[2], Y[2], border = "gray80", col = "gray80")
X2 = c(19,24)
Y2 = c(0,10)
rect(X2[1], Y2[1], X2[2], Y2[2], border = "gray80", col = "gray80")
barplot(Mean,names.arg=Hours, ylim=c(0,10), border="white", ylab="", xlab="", axes=FALSE, space=0, col="gray50", add=TRUE)
axis(2, las=2, pos=0)
axis(1, at = at_tick -1, pos=0, labels = FALSE)
box(which="plot", bty="]") #add a box around the plot
This creates a plot with a surrounding box that extends beyond the limits of the x-axis in both directions. Instead, I would like to add a box around the plot that aligns with the axis limits (i.e. x-axis: 0-23, y-axis: 0-10). I have spent ages trying to find a way to do this with no luck. Any help would be really appreciated. Thanks!
How about drawing individual lines? You can use the segment function instead of box to do this:
segments(24,10, 24,0)
segments(0,10, 24,10)
Complete code:
#Data
Hours = seq(from=0, to=23)
Mean = rnorm(24, mean=5, sd=2)
#Create number seq for tick mark locations
at_tick = seq_len(length(Hours)+1)
#Plot with background rectangle shading
x=barplot(Mean,names.arg=Hours, border="white", ylab="Freq", xlab="Hour",
ylim=c(0,10), axes=FALSE, space=0, col="grey50")
X = c(0,5)
Y = c(0,10)
rect(X[1], Y[1], X[2], Y[2], border = "gray80", col = "gray80")
X2 = c(19,24)
Y2 = c(0,10)
rect(X2[1], Y2[1], X2[2], Y2[2], border = "gray80", col = "gray80")
barplot(Mean,names.arg=Hours, ylim=c(0,10), border="white", ylab="", xlab="", axes=FALSE, space=0, col="gray50", add=TRUE)
axis(2, las=2, pos=0)
axis(1, at = at_tick -1, pos=0, labels = FALSE)
segments(24,10, 24,0)
segments(0,10, 24,10)
I wish to draw very very simple line with three numbers. It would be like below
|------|--------------|
0.5 1.5 3.4
Is it too simple to ask?
First, plot nothing, remove the axes, and add an x-axis back in at the specified points:
x <- c(.5, 1.5, 3.4)
plot(0, xlim = c(0, 3.5), axes=FALSE, type = "n", xlab = "", ylab = "")
axis(1, at = x, labels = x)
plot(1:10, rep(0,10), type='b', pch='|', axes=F, xlab="", ylab="", xlim=c(0,10))
text(1:10, rep(-0.1,10), labels=1:10)
margins and plot size can be tweaked with X11 and par
You can do it in grid,
library(grid)
grid.newpage()
grid.xaxis(at=c(0.5, 1.5, 3.4),
vp=vpStack(viewport(height=unit(2,"lines")),
viewport(y=1, xscale = c(0.4, 3.5), just="bottom")))