[R] Plotting one or more series on the same graphs
Marc Schwartz (via MN)
mschwartz at mn.rr.com
Mon Nov 21 20:22:40 CET 2005
On Mon, 2005-11-21 at 11:12 +0100, gianpaolo.romeo at nectarine.it wrote:
> Hi, I'm from Italy (sorry for my english...). I've two questions about
> the plot function.
> I've to create a simple graph for the data set "n_species":
>
>
> species=sqlQuery(dati, "select count(distinct species), season from
> captures_complete_r group by species, season")
> n_species=tapply(species$count, species$season, sum)
> n_species=data.frame(n_species)
> n_species
> n_species
> autumn 10
> spring 7
> summer 7
> winter 7
>
>
> The problem is that, if I use "plot(n_species)", I can't put the seasons
> on the x-axis, and so the values on that axes appear as numbers.
The problem here is that by default, plot() is using the result of:
axTicks(1)
as the x labels and tick mark locations, which yields:
> axTicks(1)
[1] 1.0 1.5 2.0 2.5 3.0 3.5 4.0
with four data points. Actually, R is using a C coded equivalent of
axTicks() internally, but the result is the same. See ?axTicks for more
information.
> I've tried to remove the axis and then I've add with the script:
>
> plot(n_species, type="l", main="Species trend", xlab="Season",
> ylab="Number of species",ylim=c(3,10), col=1, axes=FALSE)
> legend("topright", c("Species captured"), col = c(1), lty=c(1))
> axis(1, 1:4, c("autumn", "spring", "summer", "winter") )
> axis(2, 1:11)
This is better and gives you finer control over the axis labels and tick
mark locations. However, using axis(2, 1:11) will cause problems as the
'y' ranges of your values change, as you see below.
> and it seems to work, but when I have decimal value on the y-axis, the
> script
>
>
> plot(k$simp_seas, ylim=c(min(k), max(k)), type="l",main="Index trend",
> xlab="Season", ylab="Index value", axes=FALSE)
> points(k$shan_seas, col="red", type="l")
> leg.txt=c("Simpson's index", "Shannon's index")
> legend("topleft", leg.txt, col=c(1,2), lty=c(1,1))
> axis(1, 1:4, c("autumn", "spring", "summer", "winter") )
> axis(2, 1:11)
>
> doesn't put decimal value, but jut a "1" in the middle of the vertical
> axis.
> How can make a correct graphs?
>
> Thanks, Gianpaolo.
Since we don't have your actual data, I can provide some guidance, but
not a specific solution.
In your examples above, you need to exclude the x axis from being drawn,
but not the y axis. Using 'axes = FALSE' will preclude both axes from
being drawn.
Thus:
Instead of using 'axes = FALSE', use 'xaxt = "n"', which will preclude
the x axis only from being drawn. You will then end up with the default
y axis tick marks and labels.
See ?par for more information.
If you want more control over the y axis values as well, you can
continue to use 'axes = FALSE', but will need to consider how you want
the tick marks and labels to look and then use those values as the
arguments to axis(2, ...) as appropriate, rather than hard coding the
values and expecting them to work in all cases.
In addition, you might want to take a look at ?matplot, which will
enable you to plot multiple series in a single plot.
HTH,
Marc Schwartz
More information about the R-help
mailing list