banner



How To Repair X Axis In Plot R

Axes and Text

Many high level plotting functions (plot, hist, boxplot, etc.) allow you to include axis and text options (as well as other graphical parameters). For instance

# Specify axis options within plot()
plot(x, y, main="title", sub="subtitle",
xlab="X-axis label", ylab="y-axix label",
xlim=c(xmin, xmax), ylim=c(ymin, ymax))

For finer command or for modularization, you can use the functions described below.

Titles

Use the championship( ) function to add together labels to a plot.

championship(primary="primary title", sub="sub-title",
xlab="x-axis label", ylab="y-axis label")

Many other graphical parameters (such every bit text size, font, rotation, and colour) can too be specified in the title( ) function.

# Add a blood-red title and a blueish subtitle. Make x and y
# labels 25% smaller than the default and green.
title(principal="My Title", col.main="ruddy",
sub="My Sub-title", col.sub="blue",
xlab="My 10 label", ylab="My Y label",
col.lab="green", cex.lab=0.75)

Text Annotations

Text tin be added to graphs using the text( ) and mtext( ) functions. text( ) places text within the graph while mtext( ) places text in one of the four margins.

(To exercise adding text to plots in R, effort this interactive exercise.)

text(location, "text to identify", pos, ...)
mtext("text to identify", side, line=due north, ...)

Mutual options are described below.

option clarification
location location tin be an x,y coordinate. Alternatively, the text tin can be placed interactively via mouse by specifying location as locator(1).
pos position relative to location. i=below, 2=left, 3=above, 4=correct. If you specify pos, yous can specify starting time= in percent of character width.
side which margin to identify text. 1=bottom, 2=left, 3=meridian, 4=correct. you can specify line= to indicate the line in the margin starting with 0 and moving out. you tin can besides specify adj=0 for left/bottom alignment or adj=1 for top/right alignment.

Other mutual options are cex, col, and font (for size, color, and font style respectively).

Labeling points

You can apply the text( ) function (see above) for labeling point equally well as for adding other text annotations. Specify location as a set up of x, y coordinates and specify the text to identify every bit a vector of labels. The x, y, and label vectors should all be the same length.

# Example of labeling points
attach(mtcars)
plot(wt, mpg, primary="Milage vs. Car Weight",
xlab="Weight", ylab="Mileage", pch=18, col="blueish")
text(wt, mpg, row.names(mtcars), cex=0.vi, pos=four, col="red")

labeling points click to view

Math Annotations

You can add mathematically formulas to a graph using TEX-like rules. See help(plotmath) for details and examples.

Axes

Y'all can create custom axes using the axis( ) role.

axis(side, at=, labels=, pos=, lty=, col=, las=, tck=, ...)

where

selection description
side an integer indicating the side of the graph to depict the axis (ane=bottom, 2=left, 3=summit, 4=right)
at a numeric vector indicating where tic marks should exist fatigued
labels a character vector of labels to exist placed at the tickmarks
(if NULL, the at values will exist used)
pos the coordinate at which the axis line is to exist drawn.
(i.due east., the value on the other axis where it crosses)
lty line blazon
col the line and tick mark color
las labels are parallel (=0) or perpendicular(=ii) to axis
tck length of tick mark every bit fraction of plotting region (negative number is outside graph, positive number is inside, 0 suppresses ticks, 1 creates gridlines) default is -0.01
(...) other graphical parameters

If you lot are going to create a custom centrality, yous should suppress the centrality automatically generated by your high level plotting role. The pick axes=Faux suppresses both 10 and y axes. xaxt="northward" and yaxt="n" suppress the 10 and y axis respectively. Here is a (somewhat overblown) example.

# A Silly Centrality Instance

# specify the data
10 <- c(1:x); y <- x; z <- 10/ten

# create extra margin room on the correct for an axis
par(mar=c(5, 4, 4, 8) + 0.i)

# plot x vs. y
plot(x, y,type="b", pch=21, col="ruby",
yaxt="n", lty=3, xlab="", ylab="")

# add x vs. 1/x
lines(ten, z, blazon="b", pch=22, col="blue", lty=two)

# draw an axis on the left
axis(ii, at=x,labels=x, col.centrality="red", las=2)

# describe an axis on the correct, with smaller text and ticks
axis(4, at=z,labels=circular(z,digits=2),
col.centrality="blue", las=2, cex.axis=0.7, tck=-.01)

# add a title for the right centrality
mtext("y=one/x", side=four, line=3, cex.lab=1,las=2, col="bluish")

# add a main title and bottom and left axis labels
title("An Case of Creative Axes", xlab="Ten values",
ylab="Y=X")

axis example click to view

Minor Tick Marks

The minor.tick( ) part in the Hmisc packet adds pocket-size tick marks.

# Add minor tick marks
library(Hmisc)
minor.tick(nx=n, ny=n, tick.ratio=n)

nx is the number of minor tick marks to identify between 10-axis major tick marks.
ny does the same for the y-axis. tick.ratio is the size of the pocket-sized tick mark relative to the major tick marking. The length of the major tick marker is retrieved from par("tck").

Reference Lines

Add reference lines to a graph using the abline( ) function.

abline(h=yvalues, v=xvalues)

Other graphical parameters (such as line type, color, and width) can likewise exist specified in the abline( ) function.

# add together solid horizontal lines at y=1,v,7
abline(h=c(1,5,7))
# add dashed bluish verical lines at 10 = one,3,5,7,9
abline(5=seq(1,10,2),lty=2,col="blue")

Note: You tin also use the grid( ) function to add reference lines.

Fable

Add a legend with the legend() role.

fable(location, title, legend, ...)

Common options are described below.

pick description
location In that location are several means to indicate the location of the fable. You can give an x,y coordinate for the upper left hand corner of the legend. You can employ locator(one), in which case yous use the mouse to indicate the location of the legend. You tin can also utilise the keywords "lesser", "bottomleft", "left", "topleft", "top", "topright", "right", "bottomright", or "center". If you use a keyword, you may want to use inset= to specify an amount to motility the legend into the graph (equally fraction of plot region).
title A character string for the legend title (optional)
legend A character vector with the labels
... Other options. If the legend labels colored lines, specify col= and a vector of colors. If the fable labels point symbols, specify pch= and a vector of signal symbols. If the legend labels line width or line style, employ lwd= or lty= and a vector of widths or styles. To create colored boxes for the legend (mutual in bar, box, or pie charts), utilise fill= and a vector of colors.

Other common legend options include bty for box type, bg for background colour, cex for size, and text.col for text color. Setting horiz=True sets the legend horizontally rather than vertically.

# Legend Example
adhere(mtcars)
boxplot(mpg~cyl, main="Milage by Car Weight",
yaxt="n", xlab="Milage", horizontal=TRUE,
col=terrain.colors(iii))
fable("topright", inset=.05, title="Number of Cylinders",
c("4","6","8"), make full=terrain.colors(3), horiz=Truthful)

Legend example click to view

For more on legends, see assist(legend). The examples in the help are particularly informative.

To Do

Try the costless first affiliate of this online data visualization course in R.

How To Repair X Axis In Plot R,

Source: https://www.statmethods.net/advgraphs/axes.html

Posted by: riverajusholl.blogspot.com

0 Response to "How To Repair X Axis In Plot R"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel