Do I always need lines() with plot()? - r

When I use plot() to plot a time serious variable, it only shows dots. I use second code, lines(), to link all the dots. Is this really necessary? Or I did something wrong...
The data is as following. I use the plot() and lines() to draw the graph to see the trend.
YYYYMM<-c("200907","200908","200909","200910","200911","200912","201001","201002","201003","201004","201005","201006","201007","201008","201009","201010","201011","201012","201101","201102","201103","201104","201105","201106")
a<-c(1158,1455,1134,1371,1352,1277,1408,1270,1000,1462,1419,0,0,0,0,0,0,0,0,0,0,0,0,0)
a_number_trend<-data.frame(YYYYMM,a)
a_number_trend
plot(a_number_trend$YYYYMM,a_number_trend$a,las=2,type="l",col="blue")
lines(a_number_trend$YYYYMM,a_number_trend$a,las=2,type="l",col="blue")
The plot is like this at beginning.
Then become this.
But I want the line only without the short bar. Or to change the short bars into points.

Convert your YYYYMM column to an actual R ?Date object. Then you can get everything lining up properly:
a_number_trend$date <- as.Date(
paste0(a_number_trend$YYYYMM,"01"),
format="%Y%m%d"
)
plot(a ~ date, data=a_number_trend, type="l", xaxt="n", ann=FALSE)
The below axis is not stricly necessary (remove xaxt="n" above if you want the default Date axis calculations instead).
axis.Date(
1,
at=seq(min(a_number_trend$date), max(a_number_trend$date), by="1 month"),
format="%Y%m",
las=2
)

Related

Change axis in R with different number of datas

I want to change x-axis in my graphic, but it doesn't work properly with axis(). Datas in the graphic are daily datas and I want to show only years. Hope someone understands me and find a solution. This is how it looks like now: enter image description here and this is how it looks like with the code >axis (1, at = seq(1800, 1975, by = 25), las=2): enter image description here
Without a reproducible code is not easy to get what could be the problem. I try a "quick and dirt" approach.
High level plots are composed by elements that are sub-composed themselves. Hence, separate drawing commands could turn in use by allowing a finer control on the plotting procedure.
In practice, the first thing to do is plot "nothing".
> plot(x, y, type = "n", xlab = "", ylab = "", axes = F)
type = "n" causes the data to not be drawn. axes = F suppresses the axis and the box around the plot. In spite of that, the plotting region is ready to show the data.
The main benefit is that now the plotting area is correctly dimensioned. Try now to add the desired x axis as you tried before.
> points(x, y) # Plots the data in the area
> axis() # Plots the desired axis with your scale
> title() # Plots the desired titles
> box() # Prints the box surrounding the plot
EDITED based on comment by #scoa
As a quick and dirty solution, you can simply enter the following line after your plot() line:
# This reads as, on axis x (1), anchored at the first (day) value of 0
# and last (day) value of 63917 with 9131 day year increments (by)
# and labels (las) perpendicular (2) to axis (for readability)
# EDITED: and AT the anchor locations, put the labels
# 1800 (year) to 1975 (year) in 25 (year) increments
axis (1, at = seq(0, 63917, by = 9131), las=2, labels=seq(1800, 1975, by=25));
For other parameters, check out ?axis. As #scoa mentioned, this is approximate. I have used 365.25 as a day-to-year conversion, but it's not quite right. It should suffice for visual accuracy at the scale you have provided. If you need precise conversion from days to years, you need to operate on your original data set first before plotting.

How to add dates on axis with persp3D from plot3D package?

I'm trying to make a persp3D plot but I can't figure out how to add dates on the x-axis. It only lets me change the lable of the axis, but I haven't found a way to get certain values on the axis.
persp3D(x= (1:iNumberOfDays),y= (1:iNumberOfVariables) ,z= zValue, xlab = "Time")
I also have a date vector vDates with iNumberOfDays elements which I would like to use on the x-axis as on any regular 2d lineplot or something.
You can try to pass vDates to the x argument and set ticktype = "detailed" as follows:
persp3D(x=vDates, y=1:iNumberOfVariables, z=zValue, xlab="Time", ticktype="detailed")

How do I make R lattice xyplot ignore gaps in time and make a continuous timeseries plot?

I have an xts series that I'm trying to plot. This series contains of intra-day date for a month with gaps in the data on the weekend. I use xyplot (lattice) in R to plot the time series and am very pleased with the results.
Unfortunately the plots keep the weekend gaps. I'd like to ignore the weekend gaps and make my timeseries plot continuous and would appreciate if someone pointed me in the right direction.
The current command is :
xyplot(close~MyTime, type='l', col='black',ylab='',xlab='', main='Test')
I tried JohnPaul's method and it 'nearly' works. The labels while present, don't render correctly. The last label only goes up to the 3rd of January, while the actual data extends all the way up to February. The command used was:
PlotOrd<-order(Mytime)
xyplot(close~PlotOrd, type='l', col='black',ylab='',xlab='', main='Close',scales=list( x=list( labels=MyTime)) )
If I understand this correctly, what you wish to do is have the weekends not appear in the plot at all. One way to do this is to make another vector which is the order in which you want close plotted - a vector that does not include weekends. Assuming that weekends are not included in time this should work:
PlotOrd<-order(Mytime)
xyplot(close~PlotOrd, type='l', col='black',ylab='',xlab='', main='Test')
This will give you the correct plot, but your x axis tick labels will just be the number from PlotOrd. If you want to keep them as dates add a scales argument for the x axis, like so:
xyplot(close~PlotOrd, type='l', col='black',ylab='',xlab='', main='Test',
scales=list( x=list( labels=Mytime )) )
EDIT
One way to control the axis labels is to use the at argument as well. It is kind of clunky here and I wish I could come up with a more elegant idea, but this will work:
xyplot(close~PlotOrd, type='l', col='black',ylab='',xlab='', main='Test',
scales=list( x=list(at=c(50,100,150,200), labels=Mytime[c(50,100,150,200)] )) )
This will put ticks at observations 50,100,150 and 200, and will give corresponding values from MyTime as the label. The downside is you have to write in the ticks yourself. You could add some code to make a sequence of values. Say you want a label every 15 days. If you figure out how many measurements that corresponds to, you can make a sequence of numbers that far apart (call it MyTicks). Then feed to at=MyTicks and labels=MyTime[MyTicks]. Still it would be nicer to have lattice just pick the ticks for you...

plot multiple line segments on one graph using R

How can I duplicate this style of graph, with multiple plots on one graph, and, preferably, legends attached as below.
I have tried the concept of "facet" but ggplot2 and trellis:xyplot both think of facets as separate panels rather than overlaid plots.
I can do it using plain Jane plot() and line().. but was using ggplot2 and woudl like to get multiple lines on one plot in that package.
Here is some example data in long form (captured from the plot using a nifty app called "Graphclick")
comp <- read.table(pipe("pbpaste"), header=T, sep=',')
company, year, sales
Apple,1975.003,17298.457
Apple,1977.302,16784.502
Apple,1978.314,17298.457
Apple,1980.246,20730.098
Apple,1981.533,27608.426
Apple,1984.293,40862.852
Apple,1986.408,50468.617
Apple,1987.328,48236.188
Apple,1988.892,35676.547
Apple,1989.904,34616.582
Apple,1991.192,44732.742
Apple,1992.387,44732.742
Apple,1993.399,39055.324
Apple,1995.791,37894.922
Apple,1996.895,39648.746
Apple,1998.274,52804.367
Apple,1999.378,61399.512
Apple,2001.770,2.350e5
Apple,2005.265,7.735e5
Toshiba,1999.378,86856.6
Toshiba,2001.862,1.192e5
Toshiba,2004.069,1.495e5
Toshiba,2004.069,1.495e5
IBM,1975.003,22019.092
IBM,1975.830,27195.193
IBM,1976.934,30682.320
IBM,1978.130,31148.527
IBM,1980.430,35676.547
IBM,1981.625,35676.547
IBM,1983.005,39648.746
IBM,1985.305,40862.852
IBM,1986.408,46102.508
IBM,1987.512,64241.156
IBM,1989.996,75832.898
IBM,1991.100,84276.039
IBM,1992.295,85556.641
IBM,1993.307,79342.539
IBM,1994.779,79342.539
IBM,1995.791,84276.039
IBM,1996.895,95082.484
IBM,1996.895,95082.484
Commodore,1975.003,33588.051
Commodore,1975.830,34616.582
Commodore,1977.118,25219.982
Commodore,1978.130,23388.229
Commodore,1979.326,25992.234
Commodore,1980.521,21689.514
Commodore,1981.717,25219.982
Commodore,1984.201,6999.029
Commodore,1985.213,1670.460
Commodore,1986.408,1458.447
(source: asymco.com)
If you're looking for the most control, you could just use the low-level plot and lines commands. Use "plot" to generate the first graph (with title, xlimits, and ylimits), then use "lines" to add lines to that graph.
plot(0,type="n", xlim=c(0,10), ylim=c(0,10), xlab="X Label", ylab="Y Label", main="Title")
Then add lines using the lines command:
lines(1:10, 1:10, type="l", lty=2)
lines(2:4, 10:8, col=2, type="l")
lines(6:9, c(5,6,5,6), col=3, type="l")
You can fine-tune the look by using all of the parameters listed in the "par" help file ("?par")
so, in ggplot2, this code works
qplot(year, sales, data=comp, colour=as.factor(company), group= company, geom="path", log="y")
The only things left now is to format the value on the Y axis as numeric (not sci notation), and the labels are in an off-graph legend, rather than on the plots... Final suggestions welcomed.
This is a lot easier in the end than plot() + lines(), as that required support code to get the ranges, iterate over the group levels etc.

How do I set what plot() labels the x-axis with?

I have a plot() that I'm trying to make, but I do not want the x-values to be used as the axis labels...I want a different character vector that I want to use as labels, in the standard way: Use as many as will fit, drop the others, etc. What should I pass to plot() to make this happen?
For example, consider
d <- data.frame(x=1:5,y=10:15,x.names=c('a','b','c','d','e'))
In barplot, I would pass barplot(height=d$y,names.arg=d$x.names), but in this case the actual x-values are important. So I would like an analog such as plot(x=d$x,y=d$y,type='l',names.arg=d$x.names), but that does not work.
I think you want to first suppress the labels on the x axis with the xaxt="n" option:
plot(flow~factor(month),xlab="Month",ylab="Total Flow per Month",ylim=c(0,55000), xaxt="n")
then use the axis command to add in your own labels. This example assumes the labels are in an object called month.name
axis(1, at=1:12, labels=month.name)
I had to look up how to do this and I stole the example from here.

Resources