I have a table with 2 columns: time and distance. Both they > 0 (in meters and minutes accordingly). When I do:
plot(dist, time, main="Distance vs Time", xlab="Distance (meters)", ylab="Time (min)")
I get following plot:
Not very readable. I will use log scale instead:
plot(log(dist), log(time), main="Distance vs Time",
xlab="Distance (meters), log scale", ylab="Time (min), log scale")
And I get following plot:
My question is: why plot shows negative values as well? I do not have any parameters less than 0.
You might prefer
plot(dist, time, log="xy", ...)
The reason you are getting negative values in the plot is that you have explicitly taken the logarithm of your data. Values less than 1 will be transformed to negative values - that's just the way the math works ... using log="xy" instead will plot the points in the same locations, but will change the scales so that they show the original values.
set.seed(101)
x <- rlnorm(10)
y <- rlnorm(10)
par(mfrow=c(2,2),las=1,bty="l")
Plot on original scale:
plot(x,y)
Plot logged data, labeled by log values (which will be negative when the original values are <1):
plot(log(x),log(y))
Plot logged data, labeled by original values:
plot(x,y,log="xy")
Recreate the same plot (almost) from scratch by specifying the axis label ticks at the log positions but using the original values as labels:
plot(log(x),log(y),axes=FALSE)
brkpos <- c(0.2,0.5,1.0,2,3)
axis(side=1,at=log(brkpos),label=brkpos)
axis(side=2,at=log(brkpos),label=brkpos)
box()
(I should have used axis labels "x" and "y" in this last subplot rather than "log(x)" and "log(y)" ...)
Related
I want to make a plot in R where the spacing between ticks on the y-axis all have the same distance and the tick labels are a custom list of values, for example:
set.seed(1)
n <- 10
x <- 1:n
y <- rnorm(n)
plot(x, y, axes = FALSE, ylim=c(-2,2))
axis(1)
axis(2, seq(-2,2,1), c(-100,-10,0,5,1000))
gets me a plot where the distance between the y-axis ticks are equal but clearly the true distance between values is not equal, i.e., -100 to - 10 is not the same distance as 5 to 1000, numerically.
Now this works, but the problem with this solution is that the data is not correctly mapped to the right position in the plot. As in, I would like for the data to be plotted correctly based on the original scale. So either I need a way to simply change the y-axis to be plotted on a different scale, or for the data to be transformed to a new scale that matches my axis(2, seq(-2,2,1), c(-100,-10,0,5,1000)) command.
I guess what I am saying is I want the equivalent of plot(x, y, log = "y") but I don't actually want the log scale, I just want the tick marks to be even spaced based on values I want shown, i.e., -100,-10,0,5,1000
Your example is a bit hard to implement because you are setting the plot boundaries from -2 to 2 and then wanting axis labels that go from -100 to 1000. It should work if you use at and set the boundaries of the initial plot to match the axis parameters. I've modified your example to spread the data across the plot more evenly:
set.seed(1)
n <- 10
x <- 1:n
y <- 100*rnorm(n)
yticks = c(-100,-10,0,5,200)
plot(x, y, axes = FALSE, ylim=c(-100,200))
axis(1)
axis(2,at = yticks,labels=yticks)
I would like to plot the predicted probabilities of Y (binary outcome) over the range of observed x values (x=age). I use the following code to produce the plot:
(1) I calculate the predicted probabilities of Y over a specified range of x-values (xlevels = x.list) for my independent variable (age), and save it in an object.
prob <- effect(c("age"),M, xlevels = x.list)
(2) Then I plot that object, customising certain plot appearances (such as axis labels, color of confidence intervals, etcetera).
plot(prob,
xlab="x",
ylab="Pred. prob.",
confint=list(col="red", alpha=0.3),
lines=list(col="red")
rug=FALSE, main="")
This produces a plot that almost looks like the one I would like to have. However, when trying to customise the main title, the y and x axis limits, as well as the ticks on the axis, the plot gets produced, but unfortunately also messed up (the y axis does not range from 0 to 1, and the actual line with confidence intervals gets pushed out of the plots' margins).
plot(prob,
xlab="x",
ylab="Pred. prob.",
confint=list(col="red", alpha=0.3),
lines=list(col="red")
rug=TRUE,
axes=list(y=lim={c(0, 1, 0.1)})))
In particular, I would like to change the y-axis so that it ...
(a) ranges from 0-1
(b) with where ticks in increments of 0.1.
(c) I further would like to rid of the box around the plot, i.e. only have the x and y axis drawn.
I have been trying to read up on ?plot.eff, but unfortunately cannot get the legacy arguments to work. Any input on how the code should be modified to get it to work would be much appreciated.
axes=list(y=lim={c(0, 1, 0.1)})) looks very strange, have you tried just ylim=c(0,1)
also do main=""
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))
I'm building a plot in R and I have used the plot() function, with log="y" parameter.
Does that mean that ONLY the y-axis labels will be converted in log scale OR that also the y-coordinates of my data will be converted in log-scale?
Thank you
When using log = "y" it plots the log-transformed y-values with the labels on the original scale -- the opposite of what you seem to suggest.
Compare these three plots:
x <- rnorm(50)
y <- 2*exp(x) + rexp(50)
plot(x, y) # y-scale, y-scale-labels
plot(x, y, log = "y") # log-y-scale, y-scale-labels
plot(x, log(y)) # log-y-scale, log-y-scale labels
Notice that the last two plots only differs in the y-axis labels. Both are still correct as the axis titles are also different.
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.