I am using the twoord.plot function in the plotrix package and need to rotate the X Axis tick labels 45 degrees. Anyone know how to do so?
You need to suppress the usual labeling and put your desired labeling in with text(..., srt=45). Since by default text only goes in the plot region, the y argument may need to be negative, and you will need to extend the plotting region by "lowering" the third argument to usr. This is all described in the R-FAQ.
Related
I would like to draw X and Y axis in the beggining of the system of equations, but i don't know how. When i'm trying to set axis it always putting them under the plotted function.
pi=3.141592
x= seq(-pi,pi,0.1)
plot(x,sin(x), axes=FALSE)
axis (1,-3:3)
I don't wanna put simple lines, because i wanna have values in those axis, and would like to change those values.
Alright!
I get it, to draw Y axis in 0 position i need to add
axis(4, labels=TRUE, pos=0)
after plotting, thanks buddy!
No problem!
Here is a plot generated by julia's Plots library, using the xaxis=:log attribute:
The plot has evenly spaced tick marks, with labels like 10^0.25. This might be useful sometimes, but I find it a bit confusing for this plot, because most people don't know the value of 10^0.25 without looking it up.
I would prefer the x axis to have logarithmically spaced ticks, representing uniform intervals of the quantity on the x axis. Here's a quick example to show what I mean, generated using semilogx in Python's matplotlib lirary:
In this plot the x axis has ticks at x=1, x=2, x=3 etc., which I find more useful for the figures I'm generating. Can this be achieved in julia using Plots?
As it has been told in the discourse topic, the default tick behavior (for most backends) is different than what you expected.
There is a way you can achieve the tick behaviour you want. You can manually set tick positions and tick labels through xticks (yticks or zticks) argument.
For example the following snippet will put ticks at equally spaced points as you wanted in your post.
x = 1:0.1:10;
y = rand(length(x));
plot(x, y, xscale=:log10, xticks=(1:10, 1:10))
The first element of the tuple is for the location of the ticks and the second is for the labels. Although I used a range object for labels, you can use array of Strings or LaTeXStrings etc.
You might also want to take a look at minorticks (xminorticks for your case) and minorgrid attributes in the Julia Plots documentation.
I'm having some issues trying to change the values of both axis.
All I wanna do it's just changing the values, those exact values that you can see in the picture (20, 0, -20,-40) to this characters: "20°N", "EQ","20°S and 40°S (The same goes with the x axis).
lat<-seq(-51.25,31.25,by=2.5)
lon<-seq(238.75,331.25,by=2.5)
data<-nc_open("aprecFeb2012.nc")
dataZG<-ncvar_get(data,"aprod")
dataz<-dataZG[96:133,16:49]
filled.contour(lon, lat, dataz,zlim =c(-500:500),
plot.axes={axis(1);axis(2);map('world2', add=TRUE);grid()})
Note: You can download the netcdf file that I'm using in this example here
Picture
P.D.: Please use the same function (filled.contour)
I'm just wanna says this really quick:
You guys have been really helpful to me this past two years(the time since I've been using R), so helpful that this is my first question ever here in stackoverflow. Thank you so much
kind regards,
Freddy
The annotation for the x and y axis can be changed through the plot.axes parameter of filled.contour. See ?axis help file, which tells us at and labels can be defined for each axis side where 1=bottom (x axis), 2=left (y axis), 3=above, 4=right.
at indicates points where tick-marks are to be drawn.
labels are either a logical value specifying whether (numerical) annotations are to be used OR a character vector of labels to be placed in the tick-points.
It is also helpful when asking questions in StackOverflow to indicate which packages have to be loaded in order to run your code. I included the libraries I had to install to answer this question :)
library(ncdf4)
library(graphics)
library(maps)
Set labels to desired character vector. The length of the labels vector should match the number of tick marks designated in at. I will only do the y axis where you have defined the character vectors, and leave the x axis annotation to you.
filled.contour(lon, lat, dataz, zlim =c(-500:500),
plot.axes={axis(1); axis(2, at=seq(-40,20,20), labels=c("20°S", "40°S","EQ", "20°N")); map('world2', add=TRUE);grid()})
Here is an example where labels take on a logical value, and we set different tick marks:
filled.contour(lon, lat, dataz, zlim =c(-500:500),
plot.axes={axis(1); axis(2, at=seq(-40,20,10), labels=TRUE); map('world2', add=TRUE);grid()})
filled.contour(lon, lat, dataz, zlim =c(-500:500),
plot.axes={axis(1); axis(2, at=seq(-40,20,10), labels=FALSE); map('world2', add=TRUE);grid()})
Is it possible to add more than one x-axis to a plot in R? And to put an annotation next to each scale?
Edit > here's the result of Nick Sabbe idea. For the annotation (a little text at the left of each axis), is it possible ?
You can use the line argument of axis() to place an axis higher or lower, this way you can make multiple axes. With mtext() you can then add a label to the side. Do note that the plot itself is only on one scale so you need to rescale the points and labels of the other scale accordingly:
# Plot and first axis:
plot(1:10,1:10,bty="n",col="red",pch=16,axes=FALSE,xlab="",ylab="")
axis(2,0:11,las=1)
axis(1,0:11,line=1,col="red",col.ticks="red",col.axis="red")
mtext("Label 1",1,line=1,at=0.2,col="red")
# Secondary points and axis:
points(rnorm(10,50,20)/10, rnorm(10,5,2),pch=16, col="blue" )
axis(1,0:11,labels=0:11*10,line=3,col="blue",col.ticks="blue",col.axis="blue")
mtext("Label 2",1,line=3,at=0.2,col="blue")
You can use ?axis for that. Parameter at is in the scale of the original axis of the plot, and you can pass labels to show other values.
You have to scale the axess labels yourself, though.
A very simple/silly example:
plot(1:10,1:10)
axis(side=4, at=c(3,7), labels=c(30,70))
Finally, note that most people consider adding multiple axes to a plot bad form...
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.