Shapes and line types

Problem

You want to use different shapes and line types in your graph.

Solution

plot of chunk unnamed-chunk-2

plot of chunk line_types

Note that with bitmap output, the filled symbols 15-18 may render without proper anti-aliasing; they can appear jagged, pixelated, and not properly centered, though this varies among platforms. Symbols 19 and 21-25 have an outline around the filled area, and will render with smoothed edges on most platforms. For symbols 21-25 to appear solid, you will also need to specify a fill (bg) color that is the same as the outline color (col); otherwise they will be hollow.

Standard graphics

Use the pch option to set the shape, and use lty and lwd to set the line type and width. The line type can be specified by name or by number.

  1. set.seed(331)
  2. # Plot some points with lines
  3. # Set up the plotting area
  4. par(mar=c(3,3,2,2))
  5. plot(NA, xlim=c(1,4), ylim=c(0,1))
  6. # Plot solid circles with solid lines
  7. points(1:4, runif(4), type="b", pch=19)
  8. # Add open squares with dashed line, with heavier line width
  9. points(1:4, runif(4), type="b", pch=0, lty=2, lwd=3)
  10. points(1:4, runif(4), type="b", pch=23, # Diamond shape
  11. lty="dotted", cex=2, # Dotted line, double-size shapes
  12. col="#000099", bg="#FF6666") # blue line, red fill

plot of chunk unnamed-chunk-3

ggplot2

With ggplot2, shapes and line types can be assigned overall (e.g., if you want all points to be squares, or all lines to be dashed), or they can be conditioned on a variable.

  1. # Sample data
  2. df <- read.table(header=T, text='
  3. cond xval yval
  4. A 1 2.0
  5. A 2 2.5
  6. B 1 3.0
  7. B 2 2.0
  8. ')
  9. library(ggplot2)
  10. # Plot with standard lines and points
  11. # group = cond tells it which points to connect with lines
  12. ggplot(df, aes(x=xval, y=yval, group = cond)) +
  13. geom_line() +
  14. geom_point()
  15. # Set overall shapes and line type
  16. ggplot(df, aes(x=xval, y=yval, group = cond)) +
  17. geom_line(linetype="dashed", # Dashed line
  18. size = 1.5) + # Thicker line
  19. geom_point(shape = 0, # Hollow squares
  20. size = 4) # Large points
  21. # Condition shapes and line type on variable cond
  22. ggplot(df, aes(x=xval, y=yval, group = cond)) +
  23. geom_line(aes(linetype=cond), # Line type depends on cond
  24. size = 1.5) + # Thicker line
  25. geom_point(aes(shape=cond), # Shape depends on cond
  26. size = 4) # Large points
  27. # Same as previous, but also change the specific linetypes and
  28. # shapes that are used
  29. ggplot(df, aes(x=xval, y=yval, group = cond)) +
  30. geom_line(aes(linetype=cond), # Line type depends on cond
  31. size = 1.5) + # Thicker line
  32. geom_point(aes(shape=cond), # Shape depends on cond
  33. size = 4) + # Large points
  34. scale_shape_manual(values=c(6,5)) + # Change shapes
  35. scale_linetype_manual(values=c("dotdash", "dotted")) # Change linetypes

plot of chunk unnamed-chunk-4plot of chunk unnamed-chunk-4plot of chunk unnamed-chunk-4plot of chunk unnamed-chunk-4

By default, ggplot2 uses solid shapes. If you want to use hollow shapes, without manually declaring each shape, you can use scale_shape(solid=FALSE). Note, however, that the lines will visible inside the shape. To avoid this, you can use shapes 21-25 and specify a white fill.

  1. # Hollow shapes
  2. ggplot(df, aes(x=xval, y=yval, group = cond)) +
  3. geom_line(aes(linetype=cond), # Line type depends on cond
  4. size = 1.5) + # Thicker line
  5. geom_point(aes(shape=cond), # Shape depends on cond
  6. size = 4) + # Large points
  7. scale_shape(solid=FALSE)
  8. # Shapes with white fill
  9. ggplot(df, aes(x=xval, y=yval, group = cond)) +
  10. geom_line(aes(linetype=cond), # Line type depends on cond
  11. size = 1.5) + # Thicker line
  12. geom_point(aes(shape=cond), # Shape depends on cond
  13. fill = "white", # White fill
  14. size = 4) + # Large points
  15. scale_shape_manual(values=c(21,24)) # Shapes: Filled circle, triangle

plot of chunk unnamed-chunk-5plot of chunk unnamed-chunk-5

Note

This code will generate the chart of line types seen at the top.

  1. par(mar=c(0,0,0,0))
  2. # Set up the plotting area
  3. plot(NA, xlim=c(0,1), ylim=c(6.5, -0.5),
  4. xaxt="n", yaxt="n",
  5. xlab=NA, ylab=NA )
  6. # Draw the lines
  7. for (i in 0:6) {
  8. points(c(0.25,1), c(i,i), lty=i, lwd=2, type="l")
  9. }
  10. # Add labels
  11. text(0, 0, "0. 'blank'" , adj=c(0,.5))
  12. text(0, 1, "1. 'solid'" , adj=c(0,.5))
  13. text(0, 2, "2. 'dashed'" , adj=c(0,.5))
  14. text(0, 3, "3. 'dotted'" , adj=c(0,.5))
  15. text(0, 4, "4. 'dotdash'" , adj=c(0,.5))
  16. text(0, 5, "5. 'longdash'", adj=c(0,.5))
  17. text(0, 6, "6. 'twodash'" , adj=c(0,.5))