Plotting values over time in R - r

I have a vector of values: b=read.csv('https://dl.dropbox.com/u/22681355/b.csv')
I would like to plot them with having values 1:2000 on the x-axis representing time and the values of the vector on the y axis.
When I plot them using hist(b) I get the opposite thing with values from 1:2000 on the y axis and the actual values on the x.
How can I reverse this?

Try barplot(b) or plot(b,type="b") instead.
(Your link doesn't work for me.)

Related

Forecast plot with x axis labels as date

I have a dataset like revenue and date.
I used arima to plot the data.
ts_data = ts(dataset$Revenue,frequency = 7)
arima.ts = auto.arima(ts_data)
pred = forecast(arima.ts,h=30)
plot(pred,xaxt="n")
When I plot the data, it produces plot like below.
My expectations are below,
I need to display values in Million for predicted values like 13.1M.
I need to show x-axis as date instead of data points numbers.
I tried several links but couldn't crack it. Below are the experiments I made,
Tried with start date and end date in ts_data that also doesnt work.My start date is "2019-09-27" and end date is "2020-07-02"
tried wit axis_date in plot function that also doesnt work.
Please help me to crack the same.
Thanks a lot.
You can specify axis tick values and labels with axis()
plot(pred,xaxt="n", yaxt="n") # deactivate x and y axis
yl<-seq(-5000000,15000000,by=5000000) # position of y-axis ticks
axis(2, at=yl, label=paste(yl/1000000, "M")) # 2 = left axis
You can specify the desired position of y axis ticks with at and the labels to be associated with label. In order to obtain the values like 10 M I have used the function paste to join the numbers with the letter M.
You could use the same method also for x-axis, even tough more efficient methods probably exist. Not having the specific data available I have used a generic spacing and labels only to give you the idea. Once you have set the desired position you can generate the sequence of dates associated with it (to generate a sequence of dates see https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/seq.Date)
axis(1, at=seq(1,40,by=1), label=seq(as.Date("2019-09-27"),as.Date("2020-07-02"),by="week")) # 1 = below axis
You can also change the format of the dates displayed with format() for example label=format(vector_of_date, "%Y-%b-%d") to have year-month(in letter)-day.

How to plot for repeating values in R

I am trying to implement an array in R but plotting same y-values for all x values. If value is NA, then it shouldn't be plotted
I tried the following plot which shows the histogram for all 10 values.
plot(c(1,2,NA,3,4,5,3,NA,2,4),type='h', ylim=c(0,4))
However, for the case below, when I try to control the y-values, the repeated values are not considered in the plot.
plot(c(1,2,NA,3,4,5,3,NA,2,4), rep(1,10),type='h', ylim=c(0,4))
Is this possible with plot function? Please suggest if the same can be done with an alternative.
Please look again at the help page of ?plot.
In your second line you plot the y value 1 at the x values 1 to 5. The plot you get is exactly the plot you asked for, which is not the plot you cared for. In the first plot, your values are interpreted as the y values, not the x values. The x values in the plot are just the indices in the first example.
If you want to get the lines not plotted at the NA values, just do:
x <- c(1,2,NA,3,4,5,3,NA,2,4)
plot(!is.na(x), type = 'h')
Now you plot a TRUE (which is a value of 1) whenever there is a value, and FALSE (which translates to 0) whenever there is none.
This is the exact same as :
xx <- ifelse(is.na(x),0,1)
plot(xx, type = 'h')
On a sidenote: Please do not call this a histogram. A histogram represents counts for bins, this doesn't even come close to that.
plot(!is.na(c(1,2,NA,3,4,5,3,NA,2,4)),type='h', ylim=c(0,4))

r - how do you add a label to data points exceeding a certain value in a plot?

I want to add labels to data points in a scatter plot only if they exceed a specific value on the y axis. I can't figure out how to do this with the text() function. I'm new to r and any help is appreciated.
Given a random sample of 10 values in a scatter plot:
values <- sample(10)
plot(values)
Labels can indeed be added to specific values with the text() function. The trick is to pass x and y arguments that correspond to the coordinates of those values. For example, this adds the label >5 to the right of each value greater than 5:
text(which(values > 5), values[values > 5], labels='>5', pos=4)

switching the place of x and y axis data in r

I have a vector of data which consists of 20,000 numbers ranging between 0 and 1, i want to plot this data where x axis is the number values and y axis is their frequencies.
|
Freq|
|
|
|______________
values
but when i use plot(vector) in R, it shows frequency on x axis named as index and number values on y.
In the arguments used by plot() function i couldn't find anything helpful.
does anybody know how i could do this?
If you want a plot of frequencies, the best type of plot to make would be a barplot and the easiest way to make a barplot is just to pass a table to barplot(). For example
barplot(table(vector))
or if you just want a needle-style plot
plot(table(vector))
would also work.
If you want to trim outliers from the table, you could try
barplot( table( vector[vector<quantile(vector, .98)] ) )
here we drop samples that are above the 98% quantile.

R Polygon Plot Not Shading to X Axis

Using R and polygon I'm trying to shade the area under the line of a plot from the line to the x-axis and I'm not sure what I am doing wrong here.
The shading is using some point in the middle of the y range to shade from, not 0, the x-axis.
The data set ratioresults is a zoo object but I don't think that's the issue since I tried coercing the y values to as.numeric and as.vector and got the same results.
Code:
plot(index(ratioresults),ratioresults$ratio, type="o", col="red")
polygon(c(1,index(ratioresults),11),c(0, ratioresults$ratio, 0) , col='red')
What's index(ratioresults)? For a simple zoo object I see:
> index(x)
[1] "2003-02-01" "2003-02-03" "2003-02-07" "2003-02-09" "2003-02-14"
which is a vector of Date objects. You are trying to prepend/append values of 1 and 11 to this vector. Its not going to work.
Here's a reproducible example:
x=zoo(matrix(runif(11),ncol=1),as.Date("2012-08-01") + 0:10)
colnames(x)="ratio"
plot(index(x),x$ratio,type="o",col="red",ylim=c(0,1))
polygon(index(x)[c(1,1:11,11)],c(0,x$ratio,0),col="red")
Differences from yours:
I call my thing x.
I set ylim on the plot - I don't know how your plot managed to start at 0 on the Y axis.
I complete the polygon using the x-values of the first and 11th (last) point, rather than 1 and 11 themselves.
#With an example dataset: please provide one when you need help!
ratioresults<-as.zoo(runif(10,0,1))
plot(index(ratioresults),ratioresults, type="o", col="red",
xaxs="i",yaxs="i", ylim=c(0,2))
polygon(c(index(ratioresults),rev(index(ratioresults))),
c(as.vector(ratioresults),rep(0,length(ratioresults))),col="red")
The issue with your question is that the x-axis is not a line defined by a given y value by default, so one way to fill under a curve to the x-axis using polygon would be to define a y values for the x-axis using ylim (here I chose 0). Whatever value you choose you will want to specify that the plot stop exactly at the value using yaxs="i".
You also have to construct your polygon with the value you chose for you x-axis.

Resources