draw_lines | R Documentation |
Function to add lines and error bars to an existing plot.
draw_lines(
x,
y = NULL,
lb = NULL,
ub = NULL,
columns = NULL,
pch = 19,
cex = 1.25,
lwd = 2,
lty = 1,
arrow = FALSE,
length = 0.05,
col = "black",
col.p = "black",
col.eb = col_to_hex("grey", 0.5),
bg = "white",
border = NA,
aes = NULL
)
x |
Either a numeric vector or a data frame. |
y |
An optional numeric vector matching in length to |
lb |
An optional numeric vector matching in length to |
ub |
An optional numeric vector matching in length to |
columns |
A character vector of either 2 or 4 elements, the column names for the x and y-axis values and (optionally) the column names for the lower and upper bounds of the error bars. |
pch |
The type of point to draw
(see |
cex |
The size of the points to draw
(see |
lwd |
The width of the lines
(see |
lty |
The type of line to draw
(see |
arrow |
Logical; if |
length |
The width of the caps on the error bars. |
col |
The color of the lines
(see |
col.p |
The color of the points
(see |
col.eb |
The color of the error bars. |
bg |
The background color of the points
(see |
border |
The color for the border of a single filled error bar. |
aes |
An optional named character vector specifying
column names (if |
Adds lines and error bars to an existing plot.
# Draw a line
plot_blank()
draw_lines( c( 0, .5, 1 ), c( 0, .5, 1 ) )
# Pass points in via data frame
draw_lines(
data.frame( X = c( .1, .2, .3 ), Y = c( .8, .8, .8 ) ),
lty = 2, lwd = 3, col.p = 'blue', col = 'blue'
)
# Compute mean, SE, and 95% CI limits for data set
dtf <- aggregate(
mtcars$mpg, list( mtcars$cyl, mtcars$am ),
function(x) c( mean(x), sem(x) )
)
dtf$X <- rep( 1:3, 2 )
dtf$M <- dtf$x[,1];
dtf$LB <- dtf$x[,1] - dtf$x[,2] * 1.96
dtf$UB <- dtf$x[,1] + dtf$x[,2] * 1.96
# Create blank plot
xl <- c( .5, 3.5 ); yl <- c( 5, 35 )
plot_blank( xl, yl )
draw_hv( h = yl, l = xl )
draw_hv( v = xl, l = yl )
# Draw lines for automatic transmission
draw_lines(
dtf[1:3,],
columns = c( 'X', 'M', 'LB', 'UB' )
)
# Draw lines for manual transmission
draw_lines(
dtf[1:3 + 3,],
columns = c( 'X', 'M', 'LB', 'UB' ),
col = 'blue', col.p = 'blue', col.eb = col_to_hex( 'blue', .3 )
)
# Add axes and labels
draw_axes( 1:3, dtf$Group.1[1:3] )
mtext( 'Cylinders', side = 1, line = 2, cex = 1.25 )
draw_axes( c( 10, 20, 30 ), side = 2 )
mtext( 'MPG', side = 2, line = 2, cex = 1.25 )
# Add legend
legend(
2.5, 30,
c( 'Automatic', 'Manual' ),
fill = c( 'black', 'blue' ),
bty = 'n'
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.