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()})
Related
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 trying to make an "opposing stacked bar chart" and have found pyramid.plot from the plotrix package seems to do the job. (I appreciate ggplot2 will be the go-to solution for some of you, but I'm hoping to stick with base graphics on this one.)
Unfortunately it seems to do an odd thing with the x axis, when I try to set the limits to non integer values. If I let it define the limits automatically, they are integers and in my case that just leaves too much white space. But defining them as xlim=c(1.5,1.5) produces the odd result below.
If I understand correctly from the documentation, there is no way to pass on additional graphical parameters to e.g. suppress the axis and add it on later, or let alone define the tick points etc. Is there a way to make it more flexible?
Here is a minimal working example used to produce the plot below.
require(plotrix)
set.seed(42)
pyramid.plot(cbind(runif(7,0,1),
rep(0,7),
rep(0,7)),
cbind(rep(0,7),
runif(7,0,1),
runif(7,0,1)),
top.labels=NULL,
gap=0,
labels=rep("",7),
xlim=c(1.5,1.5))
Just in case it is of interest to anyone else, I'm not doing a population pyramid, but rather attempting a stacked bar chart with some of the values negative. The code above includes a 'trick' I use to make it possible to have a different number of sets of bars on each side, namely adding empty columns to the matrix, hopefully someone will find that useful - so sorry the working example is not as minimal as it could have been!
Setting the x axis labels using laxlab and raxlab creates a continuous axis:
pyramid.plot(cbind(runif(7,0,1),
rep(0,7),
rep(0,7)),
cbind(rep(0,7),
runif(7,0,1),
runif(7,0,1)),
top.labels=NULL,
gap=0,
labels=rep("",7),
xlim=c(1.5,1.5),
laxlab = seq(from = 0, to = 1.5, by = 0.5),
raxlab=seq(from = 0, to = 1.5, by = 0.5))
I´m working with R scatterplot3D and I need to use expression() in labels because I have to use some Greek letters;
my question is: is there a way to pull the y.lab name down or write it along the axis (in a diagonal position)? I went to help and packages description but nothing seems to work;
thanks in advance for any help
Maria
library(scatterplot3d)
par(mfrow=c(1,1))
A <- c(3,2,3,3,2)
B <- c(2,4,5,3,4)
D <- c(4,3,4,2,3)
scatterplot3d(A,D,B, xlab=expression(paste(x[a],"-",x[b])),
ylab=expression(x[a]),
zlab=expression(sigma^2))
You can't use any of the classic ways due to the way the scatterplot3d() function constructs the plot. It's basically plotted on top of a classic plot pane, which means the axis labels are bound to the classic positions. The z-label is printed at the real left Y-axis, and the y label is printed at the real right Y-axis.
You can use text() to get around this:
use par("usr") to get the limits of the X and Y coordinates
calculate the position you want the label on (at 90% of the horizontal position and 8% of the vertical position for example.)
use text() to place it (and possibly the parameter srt to turn the label)
This makes it a bit more generic, so you don't have try different values for every new plot you make.
Example :
scatterplot3d(A,D,B, xlab=expression(paste(x[a],"-",x[b])),
ylab="",
zlab=expression(sigma^2))
dims <- par("usr")
x <- dims[1]+ 0.9*diff(dims[1:2])
y <- dims[3]+ 0.08*diff(dims[3:4])
text(x,y,expression(x[a]),srt=45)
Gives
scatterplot3d(A,D,B, xlab=expression(paste(x[a],"-",x[b])),
ylab="",
zlab=expression(sigma^2))
mtext( expression(x[a]), side=4,las=2,padj=18, line=-4)
One does need to use fairly extreme parameter values to get the expression in the right place in that transformed spatial projection.
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.
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.