I need help plotting this graph in R - r

Here is my data:
t2=[89221345,87542657,90886527]
t1=[0.5,0.5,0.1]
I need to plot a graph so that the x axis has labels set to the values in t1 and the y axis are the corresponding t2 values. The x axis correspond to VIN numbers, so they need to be evenly spaced, and discrete points on the axis that are labaled. When i try plotting them by using plot(t2,t1), I see three big blocks on screen that don't make any sense to me.
Thanks!!

How about this?
require(ggplot2)
t2<-c(89221345,87542657,90886527)
t1<-c(0.5,0.5,0.1)
qplot(factor(t2),t1)+geom_point(size=5)+
theme(axis.text.x=element_text(angle=-90))

See this article.
t1=c(0.5,0.5,0.1)
t2=c(89221345,87542657,90886527)
plot(1:3,t2,xaxt="n",xlab="VIN")
axis(1,at=1:3,labels=t1)

I'm assuming that t2 are your VINs. Start of by defining the data like R would expect:
t2 <- c(89221345, 87542657, 90886527)
t1 <- c(0.5, 0.5, 0.1)
plot(t2,t1,xlab="VIN",ylab="???")
This will generate a plot with points at (89221345, 0.5), (87542657, 0.5), and (90886527, 0.1). The plot() command is looking for at minimum a vector of x values (first argument) and a vector of y values (second argument).

Related

R- how to plot cumulative lines

my problem is the following:
I have to plot a curve which shows the number of breakdowns (y) by the service life (x) but in a cumulative way - and that's the point where I struggle!!
The solution is given in the second Picture, my code in the first (I think only the type of the plot should be different)
my code
solution
Thanks so much for every help!!
I can't replicate your data, so this is more of a comment, then a complete solution.
n <- sum(h$counts) # This should sum up to the number of observations
y <- cumsum(h$counts) / n # Your y values
x <- h$mids # I assume these to be your x-axis value, but this might need an edit.
plot(x = x, y = y, type = "l")
Finally, you can add the vertical and horizontal lines via the abline() function at the respective points.

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)

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.

Plotting values over time in 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.)

R: mirror y-axis from a plot

I have this problem. I got a heatmap, (but i suppose this applies to every plot) but I need to mirror my y-axis.
I got here some example code:
library(gstat)
x <- seq(1,50,length=50)
y <- seq(1,50,length=50)
z <- rnorm(1000)
df <- data.frame(x=x,y=y,z=z)
image(df,col=heat.colors(256))
This will generate the following heatmap
But I need the y-axis mirrored. Starting with 0 on the top and 50 on the bottom. Does anybody has a clue as to what I must do to change this?
See the help page for ?plot.default, which specifies
xlim: the x limits (x1, x2) of the plot. Note that ‘x1 > x2’ is
allowed and leads to a ‘reversed axis’.
library(gstat)
x <- seq(1,50,length=50)
y <- seq(1,50,length=50)
z <- rnorm(1000)
df <- data.frame(x=x,y=y,z=z)
So
image(df,col=heat.colors(256), ylim = rev(range(y)))
Does this work for you (it's a bit of a hack, though)?
df2<-df
df2$y<-50-df2$y #reverse oredr
image(df2,col=heat.colors(256),yaxt="n") #avoid y axis
axis(2, at=c(0,10,20,30,40,50), labels=c(50,40,30,20,10,0)) #draw y axis manually
The revaxis function in the plotrix package "reverses the sense of either or both the ‘x’ and ‘y’ axes". It doesn't solve your problem (Nick's solution is the correct one) but can be useful when you need to plot a scatterplot with reversed axes.
I would use rev like so:
df <- data.frame(x=x,y=rev(y),z=z)
In case you were not aware, notice that df is actually a function. You might want to be careful when overwriting. If you rm(df), things will go back to normal.
Don't forget to relabel the y axis as Nick suggests.
For the vertical axis increasing in the downward direction, I provided two ways (two different answers) for the following question:
R - image of a pixel matrix?

Resources