Ch 1 & 2: Histogram; Bar graph; Pie Chart
Histogram
-
Let's make a histogram using Barry Bone's homerun rocords on p.41.
> x <- c(16, 25, 24, 19, 33, 25, 34, 46, 37, 33, 42, 40, 37, 34, 49, 73, 46, 45, 45) > hist(x)
-
To change the number of breaks in histogram, add the argument "breaks=". You can also change the bar color with the argument "col=".
> hist(x, breaks=4, col="gray")
-
You can create your choice of bin classes with the seq command. Let's set minimum = 0, maximum=80, and each bin has a range of 8.
> bins=seq(0, 80, by=8) > hist(x, breaks=bins, col="blue")
Bar-graph
-
A bar chart with the data of 1.3: The color of your car (p.10).
> x <- c(20, 18, 16, 13, 10, 7, 6) > names(x) <- c("silver", "white", "black", "blue", "brown", "red", "gold") > barplot(x, col="red")
Pie-graph
-
A pie chart with the data of 1.3: The color of your car (p.10).
> x <- c(20, 18, 16, 13, 10, 7, 6) > names(x) <- c("silver", "white", "black", "blue", "brown", "red", "gold") > pie(x)
-
3D pic chart. (1) Install the package plotrix using a command, install.packages( )" and (2) Bring that package into your R with a command, library( ). (3) Draw a graph with a command, pie3D. Note that pie3D's D should be a upper case.
> install.packages("plotrix") > library(plotrix) > pie3D(x, labels=pct)
You can add percentage information next to the labels.
> pct <- round(x/sum(x)*100) > pct <- paste(names(x), pct) > pct <- paste(pct, "%", sep="") > pie(x, labels=pct)
* You can save your graph as a file. Consult with Save your graph as a picture file
[ Going back to Using R: Index ]