Related
I am able to plot data and and everything seems to work. The only problem is that R seems to decide if line markers are inserted or not. I have several different datasets, for the dataset with 1500 the plot works fine and I can see the markers. Any other dataset, all of them with 3000+ points the plot ignores all markers and just the line can be seen.
Bellow you guys can see the code used to plot the data and example plot Figures.
My question is, how can I assure that R will plot the lines with markers? Am I doing something wrong?
Thanks for your time and help.
png(filename="figures/all.normdtime.png", width=800, height=600)
plot(ecdf(data1[,10]), col="blue", ann=FALSE, pch=c(1,NA,NA,NA,NA,NA,NA,NA,NA), cex=2)
lines(ecdf(data2[,10]), col="green", pch=c(3,NA,NA,NA,NA,NA,NA,NA,NA), cex=2)
lines(ecdf(data3[,10]), col="red", pch=c(8,NA,NA,NA,NA,NA,NA,NA,NA), cex=2)
lines(ecdf(data4[,10]), col="orange", pch=c(2,NA,NA,NA,NA,NA,NA,NA,NA), cex=2)
title(xlab="Transfer rate (bytes/ms)")
title(main="ECDF Normalized Download Time")
dev.off()
No markers, 21100 points plotted
With markers, 1400 points plotted
I would try something like this:
data1 <- dnorm(seq(-5,5,.001))
x <- ecdf(data1)
plot(ecdf(data1), col="blue", ann=FALSE, pch=c(1,rep(NA,10000)), cex=2)
points(x=knots(x)[seq(1,length(knots(x)),5)], y=ecdf(data1)(knots(x)[seq(1,length(knots(x)),5)]), col="red",pch=3)
title(xlab="Transfer rate (bytes/ms)")
title(main="ECDF Normalized Download Time")
The original ECDF is not visible since we plotted approx. 1500 points.
If you want less just change the value 5 inside the x and y argument of pointsto a bigger number i.e. 100. Then we have ~70 points plotted:
I don't have your data available but I think this should work for you:
ecdf1 <- ecdf(data1[,10])
ecdf2 <- ecdf(data2[,10])
ecdf3 <- ecdf(data3[,10])
ecdf4 <- ecdf(data4[,10])
knots1 <- knots(ecdf1)
knots2 <- knots(ecdf2)
knots3 <- knots(ecdf3)
knots4 <- knots(ecdf4)
n <- 10 # every 10th point
png(filename="figures/all.normdtime.png", width=800, height=600)
plot(ecdf1, col="blue", ann=FALSE)
points(x=knots1[seq(1,length(knots1),n)], y=ecdf1(knots1[seq(1,length(knots1),n)]), col="blue",pch=1)
lines(ecdf2, col="green")
points(x=knots2[seq(1,length(knots2),n)], y=ecdf2(knots2[seq(1,length(knots2),n)]), col="green",pch=3)
lines(ecdf3, col="red",)
points(x=knots3[seq(1,length(knots3),n)], y=ecdf3(knots3[seq(1,length(knots3),n)]), col="red",pch=8)
lines(ecdf4, col="orange")
points(x=knots4[seq(1,length(knots4),n)], y=ecdf4(knots4[seq(1,length(knots4),n)]), col="orange",pch=2)
title(xlab="Transfer rate (bytes/ms)")
title(main="ECDF Normalized Download Time")
dev.off()
I have the following code, and all I can graph is the "red" line (unemployement). If anyone could help that would be great. Thank you:
library(lubridate)
data<-read.csv("BLSdata.csv")
summary(data)
data$DATE <- as.date(mdy(data$Date))
class(data$DATE)
data$DATE <- mdy(as.character(data$Date))
data$DATE
plot(data$DATE, data$Unemployed, type="l", lwd=2, col="red",xlab="Year",
ylab="Jobs", ylim=c(6000,17000))
points(data$DATE, data$Employed, type="l", lwd=2, col="green")
GOAL: I am trying to graph both green and red lines on a graph with one axis.
Looks like you want to use lines() instead of points(), but that won't matter because you put in type="l". The main thing is to force the range to include both sets of data:
plot(data$DATE, data$Unemployed, type="l", lwd=2, col="red",xlab="Year",
ylab="Jobs", ylim=range(c(data$Unemployed,data$Employed)))
lines(data$DATE, data$Employed, lwd=2, col="green")
Struggling with this one. I have 25 data items that I want plotted with bubbles in 5 columns.
The plot can be re-created thus:
xcord <- c(1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,4,4,4,4,4,5,5,5,5,5)
ycord <- c(1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,4,5)
zsize <- c(2,1,2,3,6,8,9,1,4,5,5,6,7,8,8,9,5,5,5,5,1,8,1,1,12)
Save the parameters before I change them:
op <- par()
dev.off()
Change the parameters:
par (mfrow=c(1,0), mar=c(2,2,2,2), oma=c(2,2,2,2))
Plot using symbols:
symbols(xcord, ycord, zsize, inches=0.3, fg="white", bg="red", xlab="Events", ylab="Diagnoses", tck=0, xaxt="n", yaxt="n")
mtext("Rheumatic diagnoses by cerebrovasular events", line=2, font=2, cex=1.2)
I am happy with the above plot and deliberately used tck=0, xaxt="n", yaxt="n" to clear the axes. I want to manually overlay custom text, controlled with custom co-ordinates (which work with the sysmbols plot), but have not been able to do it. I have tried some of the par arguments and the axes function.
I also tried leaving the axes on:
symbols(xcord, ycord, zsize, inches=0.3, fg="white", bg="red", xlab="Events", ylab="Diagnoses")
but do not know how to change the output (1,2,3,4,5) to my own custom axes labels.
Thank you.
You are looking for the axis function (see ?axis for details), e.g. to replace the 1:5 with A, B, C, D, E:
axis(side=1, at=1:5, labels=LETTERS[1:5])
I'm looking to display two graphs on the same plot in R where the two graphs have vastly different scales i.e. the one goes from -0.001 to 0.0001 and the other goes from 0.05 to 0.2.
I've found this link http://www.statmethods.net/advgraphs/axes.html
which indicates how to display two y axes on the same plot, but I'm having trouble.
My code reads as follows:
plot(rateOfChangeMS[,1],type="l",ylim=c(-0.01,.2),axes = F)
lines(ratios[,1])
x = seq(-0.001,0.0001,0.0001)
x2 = seq(0.05,0.2,0.01)
axis(2,x)
axis(4,x2)
The problem I'm having is that, although R shows both axes, they are not next to each other as I would like, with the resulting graph attached. The left axis is measuring the graph with the small range, while the right is measuring the graph from 0.05 to 0.2. The second graph is, in fact, on the plot, but the scaling is so small that you can't see it.
Not sure if there is some etiquette rule I'm violating, never uploaded an image before so not quite sure how best to do it.
Any help would be greatly appreciated!
Thanks
Mike
Since you don't provide a reproducible example, or a representative dataset, this is a partial answer.
set.seed(1)
df <- data.frame(x=1:100,
y1=-0.001+0.002/(1:100)+rnorm(100,0,5e-5),
y2=0.05+0.0015*(0:99)+rnorm(100,0,1e-2))
ticks.1 <- seq(-0.001,0.001,0.0001)
ticks.2 <- seq(0.05,0.2,0.01)
plot(df$x, df$y1, type="l", yaxt="n", xlab="X", ylab="", col="blue")
axis(2, at=ticks.1, col.ticks="blue", col.axis="blue")
par(new=T)
plot(df$x, df$y2, type="l", yaxt="n", xlab="", ylab="", col="red")
axis(4, at=ticks.2, col.ticks="red", col.axis="red")
The reason your left axis is compressed is that both axes are on the same scale. You can get around that by basically superimposing two completely different plots (which is what having two axes does, after all). Incidentally, dual axes like this is not a good way to visualize data. It creates a grossly misleading visual impression.
I am starting on a bit of analysis on pairs of stocks (pairs trading) and here is the function I wrote for producing a graph (pairs.report - listed below).
I need to plot three different lines in a single plot. The function I have listed does what I want it to do, but it will take a bit of work if I want a fine customisation in the x-axis (the time line). As it is, it prints just the years (for 10 years of data) or the months (for 6 months of data) in the x-axis, with no formatting for ticks.
If I use an xts object, i.e., if I use
plot(xts-object-with-date-asset1-asset2, ...)
instead of
plot(date, asset2, ...)
I get a nicely formatted x-axis right away (along with the grid and the box), but subsequent additions to the plot using functions like points(), text(), lines() fails. I suppose points.xts() and text.xts() are not coming out any time soon.
I would like the convenience of xts objects, but I will also require a fine grained control over my plot. So what should my work-flow be like? Do I have to stick to basic graphics and do all the customisation manually? Or is there a way I can make xts work for me?
I am aware of lattice and ggplot2, but I don't want to use them now. Here is the function I mentioned (any criticism/ suggestions for improvement of the code is welcome) -
library(xts)
pairs.report <- function(asset1, asset2, dataset) {
#create data structures
attach(dataset)
datasetlm <- lm(formula = asset1 ~ asset2 + 0, data = dataset)
beta = coef(datasetlm)[1]
#add extra space to right margin of plot within frame
par(mar=c(5, 4, 4, 4) + 0.1)
# Plot first set of data and draw its axis
ylim <- c(min(asset2,asset1), max(asset2,asset1))
plot(date,
asset2,
axes=T,
ylim=ylim,
xlab="Timeline",
ylab="asset2 and asset1 equity",
type="l",
col="red",
main="Comparison between asset2 and asset1")
lines(date, asset1, col="green")
box()
grid(lwd=3)
# Allow a second plot on the same graph
par(new=T)
# Plot the second plot and
ylim <- c(min(asset1-beta*asset2), max(asset1-beta*asset2))
plot(date,
asset1-beta*asset2,
xlab="", ylab="",
ylim=ylim,
axes=F,
type="l",
col="blue")
#put axis scale on right
axis(side=4,
ylim=ylim,
col="blue",
col.axis="blue")
mtext("Residual Spread",side=4,col="blue",line=2.5)
abline(h=mean(asset1-beta*asset2))
}
plot.xts is a base plot function, which means you can use points.default() and lines.default() if you used the same x arguments as plot.xts uses. But that is not necessary. It is already hashed out in the xts and zoo packages because when those packages are loaded, and you execute methods(lines) and methods(points) you see such functions are already available. points.zoo is documented on the ?plot.zoo page.