官术网_书友最值得收藏!

Choosing line styles and width

Similar to plotting point symbols, R provides simple ways to adjust the style of lines in graphs.

Getting ready

All you need to try out in this recipe is to run R and type the recipe in the command prompt. You can also choose to save the recipe as a script so that you can use it again later on. We will use the cityrain.csv data file that we read in the last recipe again.

How to do it...

Line styles can be set using the lty and lwd arguments (for the line type and width, respectively) in the plot(), lines(), and par() commands. Let's take our rainfall example and apply different line styles, keeping the color the same:

plot(rain$Tokyo,
ylim=c(0,250),
main="Monthly Rainfall in major cities",
xlab="Month of Year",
ylab="Rainfall (mm)",
type="l",
lty=1,
lwd=2)

lines(rain$NewYork,lty=2,lwd=2)
lines(rain$London,lty=3,lwd=2)
lines(rain$Berlin,lty=4,lwd=2)

legend("top",
legend=c("Tokyo","New York","London","Berlin"),
ncol=4,
cex=0.8,
bty="n",
lty=1:4,
lwd=2)

How it works...

Both the line type and width can be set with numerical values, as shown in the preceding example. The line type number values correspond to types of lines:

  • 0: blank
  • 1: solid (the default)
  • 2: dashed
  • 3: dotted
  • 4: dotdash
  • 5: longdash
  • 6: twodash

We can also use the character strings instead of numbers, for example, lty="dashed" instead of lty=2.

The line width argument, lwd, takes positive numerical values. The default value is 1. In the example, we used a value of 2, thus making the lines thicker than the default.

See also

We will explore more examples of line styles in subsequent chapters, especially in Chapter 5, Creating Line Graphs and Time Series Charts, in which we will see some advanced line graph recipes.

主站蜘蛛池模板: 平定县| 罗平县| 北川| 望都县| 昌邑市| 嘉定区| 抚宁县| 武山县| 重庆市| 开封市| 图们市| 新平| 遵义县| 集贤县| 灵川县| 都兰县| 镇江市| 石棉县| 吉林市| 湖北省| 马尔康县| 文水县| 古蔺县| 桑植县| 丹巴县| 尉犁县| 牟定县| 宁夏| 太康县| 东方市| 马尔康县| 江陵县| 无锡市| 嵊泗县| 兴城市| 宁蒗| 麻江县| 安阳市| 博乐市| 金沙县| 进贤县|