Is it possible to draw an uneven axis in R? I know that I can specify labels at specific spots, but I mean, I want a particular section of my graph to be spread out. For instance, imagine an X axis such as:
-10 -5 0 1 2 3 4 5 10
where there is equal spacing between each of the above values.
You would need to make a factor of your levels, say "fac", and then plot. Later using axis() with labels=as.character(fac).
dat <- data.frame(x=factor(c(-10 ,-5, 0, 1, 2, 3, 4, 5, 10)), y=1:9)
with(dat, plot(x, y)) # a step-like plot
with(dat, plot(as.numeric(x), y, type="p", xaxt="n")) # points instead of steps
axis(1, at=1:9, labels=as.character(dat$x)) # the "irregular" axis
Thinking further about #Ben Bolker's question, it should also be possible to define a an auxiliary x-value to determine the plotting "x=" and the axis "at=" coordinate on the horizontal and then using unique(as.character(.)) applied to the "real x" as the "labels=" argument to axis as shown above but not needing a factor construction. An even more complex scheme is possible with this approach where continuous values across specific ranges of the auxiliary variable could be used for plotting but truncated values constructed for the labels at the boundaries of those ranges. I think further design justification and specification would be needed before building an implemented example.
Related
I'm analyzing numeric data with values between 1 to 7. I want to plot boxplots and show the significance across categories. My problem is that adding the labels also extends the values in the y axis. This might imply that the possible data range is up to more than 7 - which is not the best. I tried using ylim() but using it cuts off the signif labels. Is there a way to make the axis values to be 1-7, without cutting the information the should apear beyond this range?
my current plot:
when using ylim()
the desired outcome is something like that:
As mentioned in the comments, the solution is setting breaks:
gboxplot(...)+ scale_y_continuous(breaks = seq(0, 7, by = 1))
delme <- exp(rnorm(1000,1.5,0.3))
boxplot(delme,log="y")
boxplot(log10(delme))
Why are the whiskers different in this 2 plots?
Thanks
Agus
I would say that in your first plot you just changed the y axis to log, so the values you plot still range between 1 and 10. In this plot the y axis is a log scale. The whiskers on this axis look different because the space between each "tick" (ie axis break) is not constant (there is more space between 2 and 4 than between 10 and 8)
In the second plot, you take the log of the values then plot them, so they range from .2 to 1, and are plotted with a linear y axis.
Look at the summary for both of the normal and log transformed dataframes
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.
There are two continuous variables (A and B) in my data. I want to make a scatter plot with a regression line in R. I think this is easy. But how can I make different scales (interval) for A on X axis and for B on y axis? For instance, A on X axis: 2, 4, 6, 8, 10, the interval is 2. How can I change it to: 1,2,3,4,5,6,7,8,9,10 where the interval is 1. It is similar for y axis.
Thanks a lot!
There are good instructions at Robert Kabacoff's site. In a nutshell, you could use code such as:
z = c(1:10)
plot (A,B) #add whatever specifications you need
axis (1, at=z, labels=z) #1 indicates the lower horizontal axis; 1=bottom, 2=left, 3=top, 4=right
I was wondering if it was possible to graph three lines in R using functions. For instance, how could I get the functions:
3x+1
4x+2
x+1
to show up on the same graph in r?
First decide the bounds, say 0 to 100, and make an empty plot including those points:
plot(c(0,100), c(0,100))
possibly of course with optional parameters such as axes=, xlab=, ylab=, and so on, to control various details of the axes and titling/labeling; then, add each line with abline(a, b) where b is the slope and a is the intercept, so, in your examples:
abline(1, 3)
abline(2, 4)
abline(1, 1)
Of course there are many more details you can control such as color (col= optional parameter), line type (lty=) and width (lwd=), etc, but this is the gist of it.
You can also use the curve function. For example:
curve(3*x+1, from=-5, to=5)
curve(4*x+2, add=T)
curve(x+1, add=T)
Here the add parameter causes the plots to be put on the same graph
Here's another way using matplot:
> x <- 0:10
> matplot(cbind(x, x, x), cbind(3*x+1, 4*x+2, x+1),
type='l', xlab='x', ylab='y')
matplot(X, Y, ...) takes two matrix arguments. Each column of X is plotted against each column of Y.
In our case, X is a 11 x 3 matrix with each column a sequence of 0 to 10 (our x-values for each line). Y is a 11 x 3 matrix with each column computed off the x vector (per your line equations).
xlab and ylab just label the x and y axes. The type='l' specifies that lines are to be drawn (see other options by typing ?matplot or ?plot at the R prompt).
One nice thing about matplot is that the defaults can be nice for plotting multiple lines -- it chooses different colors and styles per line. These can also be modified: see ?matplot (and lty for more detail).