0413 in class exercise 2, 5
## Warning: package 'rmdformats' was built under R version 3.6.3
In class exercise 2
Doll (1955) showed per capita consumption of cigarettes in 11 countries in 1930, and the death rates from lung cancer for men in 1950. Use R base graphics to generate the plot shown below. Source: Freedman, et al. (1997). Statistics. pp. 148-150.
Column 1: Country names Column 2: Cigarettes consumption (per million) Column 3: Death rate (per capita)
loading data and check data structure, display first 6 rows of data
dta<-read.table("C:/Users/user/Desktop/cigarettes.txt", header=T)
str(dta)'data.frame': 11 obs. of 3 variables:
$ Country : Factor w/ 11 levels "Australia","Canada",..: 1 2 3 4 10 5 6 7 8 9 ...
$ consumption: int 480 500 380 1100 1100 230 490 250 300 510 ...
$ death : int 180 150 170 350 460 60 240 90 110 250 ...
head(dta) Country consumption death
1 Australia 480 180
2 Canada 500 150
3 Denmark 380 170
4 Finland 1100 350
5 UK 1100 460
6 Iceland 230 60
plot
plot(dta$death~dta$consumption, type="n",
main="Lung Cancer and Cigaratte Consumption",
ylab="Death rate (per million)",
xlab="Consumption (per capital per year)",
xlim=c(200, 1200))
abline(v=seq(200, 1200, by=200), col="lightgray", lty=2)
abline(h=seq(100, 400, by=100), col="lightgray", lty=2)
abline(lm(death~consumption, data=dta), lty=2)
text(death ~consumption, labels=Country,data=dta, cex=0.8, font=1.8)In class exercise 5
Draw a pie chart to represent 50 shades of gray. Hint: Use ‘?gray’ to examine the gray level specification documented for the gray{grDevices}. Use ‘?pie’ to study the function for making pie charts documented for pie{graphics}
par(mar=c(1,1,1,1))
col_gray<-rep(1:50)
labels<-as.matrix(gray(0:50/50))
pie(col_gray,
col=gray(0:50/50),
labels = labels, radius=0.7, cex=0.15, clockwise = TRUE) # labels will overlap, hence I use sample to show partial labels
legend(
x = -1.8,
y = 1,
inset = .05,
title = "50 shade of gray",
legend = labels,
fill = gray(0:50/50),
horiz = FALSE,
cex = 0.6,
text.width = 0.2, ncol=3
)