Strony.wne.uw.edu.pl



R - programming language and software environment for statistical computation and visualization of resultsClass 4. Charts, plots, graphs# Operators for generating sequence of values# Let’s remind operators seq() and rep()# sequence with step 1:1:12# seq() generates a sequence, parameter 'by' specifies the step of the sequence# sequence of numbers from 1 to 100 with step 2seq(1,100,by=2)# rep()repeats the number in the given sequence a certain number of times # and then repeats this action until the desired length is reached# numbers from 1 to 4, each repeated twice, until the desired length is reached# in total 10 elementsrep(1:4, each = 2, len = 10)# Operators sample() and rnorm()# sample() creates a random permutation of the vector given as a parametersample(1:6) # 10-times draw from a set of numbers 1:6 (e.g. 10-times dice roll simulation)sample(1:6,10,replace=T)# rnorm()generates a set of 30 numbers with an average of 50 and a standard deviation of 5# with a normal distributionrnorm(30,50,5)# Charts # In the R environment, you can use a large number of ready charts. Numerous parameters in graphic# functions allow you to modify the appearance and to refine the visual presentation of data. # first let’s install and load the ggplot2 package with the appropriate commandsinstall.packages("ggplot2")library(ggplot2)# Simple graph plot(x,y)# as arguments of function plot (x, y) enter two vectors - the first gives x coordinates and second - y.# if in plot() you will enter only one vector, the second by default will consist of successive # natural numbers (x coordinates)# Example 1. Create vectors x and y. Then draw a simple graph using the function plot().x = c(7.5, 9, 7, 7, 10.5, 8.5, 10, 12.5, 9.5, 10.5)y = c(65, 70, 66, 68, 66, 64, 71, 67, 72, 75)plot(x,y)# By default plot() is a dot plot. By adjusting the "type" parameter you can get other types:# "l" - lines (lowercase letter L)# "b" - both points and lines# "c" - lines part without points# "o" - both points and lines (overplotted)# add lines connecting successive points by adjusting the parameter ?type” plot(x,y,type="l")# obtained graph is very unclear, so we will sort the data according to the variable x.# To do this, create a data frame, and then sort the data ascending with function order():data = data.frame(x,y)data = data[order(data[, 1]), ]# create a dot plot - this one looks the same as the first plotplot(data)# create a plot with lines - this plot is more readableplot(data, type = "l")# create a plot with both points and linesplot(data, type = "b")# Editing the basic elements of the chart# main - main title of the plot# xlab – X axis label# ylab – Y axis label# col - graph color (you can set many colors by setting col as a vector or a color palette)# col.main - color of a main title# col.lab - color of labels of both x and y axes# font – font type (1 - normal, 2 - bold, 3 - italic, 4 – bold italic)# lty – line type (1 - line, 2 - dashed, 3 - dotted, 4 – dash-dot, 5 – long dash, 6 – double dash)# lwd - line thickness (by default 1).# Bar chart barplot()# Example 1. Bar chart for weekly measurements of maximal temperaturemax.temp = c(22, 27, 26, 24, 23, 26, 28)barplot(max.temp)# add to the chart: main title, axis titles, colors and bar descriptions, horizontal display barplot(max.temp,main = "Weekly maximal temperature",xlab = "Celsius degrees",ylab = "Day",names.arg = c("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"),col = "darkred",horiz = TRUE)# Example 2. Bar chart of the age of a students’ groupage = c(21,18,19,21,22,19,22,20,23,22)barplot(age)# age vector summarized by counttable(age)# bar chart of age vector summarized by countbarplot(table(age))# add to the chart: main title, axis titles, colors and bar borders barplot(table(age),main="Age of 10 students - count",xlab="Age",ylab="Count",border="red",col="blue",density=10)# Example 3. Bar chart of weekly sales of various types of carscars = c(1, 3, 6, 4, 9)trucks = c(2, 5, 4, 5, 12)suvs = c(4, 4, 6, 6, 16)# create data frame autos_dataautos_data = data.frame(cars, trucks, suvs)# create a bar chart of the variable Suvs with specified labels for axes and orange bar bordersbarplot(autos_data$suvs, main="Suvs sales", xlab="Days", ylab="Total", names.arg= c("Mon","Tue","Wed","Thu","Fri"), border="orange")# create a bar chart of the variable Cars with labels for axes, blue borders and diagonal lines in bars barplot(autos_data$cars, main="Cars sales", xlab="Days", ylab="Total", names.arg=c("Mon","Tue","Wed","Thu","Fri"), border="blue", density=c(10,20,30,40,50))# create a bar chart of all the variables in autos_data with adjacent bars using rainbow colorsbarplot(as.matrix(autos_data), main="Autos", ylab= "Total", beside=TRUE, col=rainbow(5))# Pie chart pie()# They are considered as less efficient than bar charts, because pie charts require only one# series of values (they do not allow to compare different data series divided by subgroups)# Example 1. A pie chart of the age of students’ groupage = c(21,18,19,21,22,19,22,20,23,22)pie(age)# pie chart of age vector summarized by countpie(table(age))# add to the chart: colors in greyscalepie(table(age), col=grey(0:4/4))# Example 2. Pie chart of weekly sales of various types of carscars <- c(1, 3, 6, 4, 9)pie(cars)# add to the chart: main title, colors and labelspie(cars, main="Cars sales", col=rainbow(length(cars)), labels=c("Mon","Tue","Wed","Thu","Fri"))# calculate the percentage for each day, rounded to one decimal placecar_labels <- round(cars/sum(cars) * 100, 1)# concatenate a % sign after each valuecar_labels <- paste(car_labels, "%", sep="")# add to the chart: main title, colors and custom labelspie(cars, main="Cars sales", col=rainbow(length(cars)), labels=car_labels, cex=0.8)# add to the chart: legend at the right side legend(1.5, 0.5, c("Mon","Tue","Wed","Thu","Fri"), cex=0.8, fill=rainbow(length(cars)))# Example 3. A pie chart of the distribution of political parties in the parliamentparties = data.frame(Politicalparty = c("PO", "PSL", "PiS", "TR", "SLD", "SP", "independent"), Members=c(190, 53, 190, 56, 86, 37, 28))pie(parties[, 2], labels = parties[, 1], col = rainbow(length(parties[, 1])), main = "Distribution of political parties")# Plotting a function in a given range curve()# Example 1. Chart with a function log10(x) , in a range from 0.1 to 10:curve(log10(x), 0.1, 10)# chart with two (or more) functions in one graph window allows the parameter addcurve(sin(x), 0, 10, add=TRUE)# By default, the function graph line is black. Change the line color to red and bluecurve(log10(x), 0.1, 10, col="red")curve (sin(x), 0, 10, col="blue", add=T)# Example 2. Chart with chi^2 distribution for different degrees of freedomx = seq(from=0, to =10, by=0.1)curve(dchisq(x,1), xlim=c(0,10), ylim=c(0,0.6))#1 degree of freedomcurve(dchisq(x,4), add=T)# 4 degrees of freedom# add to the chart: main title, colors and thick linescurve(dchisq(x,1), xlim=c(0,10), ylim=c(0,0.6), col="red", lwd=3, main="Chi^2 distributions", xlab="", ylab="")curve(dchisq(x,4), add=T, col="green", lwd=3)# add to the chart: legend at the top right side and colorscurve(dchisq(x,1), xlim=c(0,10), ylim=c(0,0.6), col="red", lwd=3, main="Chi^2 distributions", xlab="", ylab="", legend("topright",c("1 degree of freedom","4 degrees of freedom"),fill=c("red","green")))curve(dchisq(x,4), add=T, col="green", lwd=3)# Histogram hist()# Example 1. Histogram and normal distribution# sample: 200 numbersproba = rnorm(200)# create a histogramhist(proba,20,probability=T, col="yellow")# add to the chart: a normal distribution curvecurve(dnorm(x), lwd=3, col="red", add=T)# Other graphs# boxplot() - box chart# stars - radar chart# mosaicplot - mosaic chart# pairs() - set of scatterplots# Tasks# Task 1. Define the cars vector with 5 values: 3, 5, 8, 6, 9.# Plot cars with blue points overlayed by a line, and a title “Autos” with red color and bold italic font.# Task 2. Create a vector W containing numbers from 4 to 13.# Create a bar chart of the vector W with the following parameters:# ? enter the title of the chart "Chart",# ? enter "arguments" as the title of the x axis,# ? enter "values" as the title of the y axis,# ? set the color of the bars to red.# Task 3. Create a vector wx with elements from 1 to 4 repeated eight times.# For wx generate a pie chart and a pie chart of wx vector summarized by count. # Use the rainbow() parameter to colorize the pie segments.# Save the chart in .PNG format# Task 4. Create a graph of a function y = cos(x) in a range (?2π, 2π) with the blue line.# Then, in the same chart, add a graph of the function y = sin (x) drawn with a red line.# Add the title of the chart and the titles of the X and Y axes.Hint: xlim - range of x, therefore (?2π, 2π) you can write down as xlim=c(-2*pi,2*pi).# Task 5. Create vector M as a set of 400 numbers with a normal distribution. # For vector M create a histogram with the following parameters:#? enter the title of the chart ?Histogram”,#? enter "Data x" as the title of the x axis,#? enter "Data y" as the title of the y axis,#? set the color to lightblue.# Add to chart: a normal distribution curve of thickness 4 and red color.# Save the chart in .PNG format # Task 6. Create vector N as a set of 40 numbers with an average of 25 and a standard deviation of 5# with a normal distribution. # For vector N create a histogram with the following parameters:#? enter the title of the chart ?Histogram”,#? enter "Data x" as the title of the x axis,#? enter "Data y" as the title of the y axis,#? set the color to green.# Tasks - solution# Task 1. Define the cars vector with 5 values: 3, 5, 8, 6, 9.# Plot cars with blue points overlayed by a line, and a title “Autos” with red color and bold italic font.cars=c(3, 5, 8, 6, 9)plot(cars, type="o", col="blue", main="Autos", col.main="red", font.main=4)# Task 2. Create a vector W containing numbers from 4 to 13.# Create a bar chart of the vector W with the following parameters:# ? enter the title of the chart "Chart",# ? enter "arguments" as the title of the x axis,# ? enter "values" as the title of the y axis,# ? set the color of the bars to red.W=4:13barplot(W, main="Chart", xlab="arguments", ylab="values", col="red")# Task 3. Create a vector wx with elements from 1 to 4 repeated eight times.# For wx generate a pie chart and a pie chart of wx vector summarized by count. # Use the rainbow() parameter to colorize the pie segments.# Save the chart in .PNG formatwx=rep(1:4,8)pie(wx)# pie chart of wx vector summarized by countpie(table(wx)) # rainbow() parameter colorizes the pie segmentspie(table(wx), col=rainbow(4)) # Task 4. Create a graph of a function y = cos(x) in a range (?2π, 2π) with the blue line.# Then, in the same chart, add a graph of the function y = sin (x) drawn with a red line.# Add the title of the chart and the titles of the X and Y axes.Hint: xlim - range of x, therefore (?2π, 2π) you can write down as xlim=c(-2*pi,2*pi).curve (cos(x), xlim=c(-2*pi,2*pi), col = "blue", main = "Functions cos(x) and sin(x)", xlab = "arguments", ylab = "values") curve(sin(x), col="red", add=T)# Task 5. Create vector M as a set of 400 numbers with a normal distribution. # For vector M create a histogram with the following parameters:#? enter the title of the chart ?Histogram”,#? enter "Data x" as the title of the x axis,#? enter "Data y" as the title of the y axis,#? set the color to lightblue.# Add to chart: a normal distribution curve of thickness 4 and red color.# Save the chart in .PNG format M=rnorm(400)hist(M, probability=T, main = "Histogram", xlab="Data x", ylab="Data y", col="lightblue")curve(dnorm(x), lwd=4, col="red", add=T)# Task 6. Create vector N as a set of 40 numbers with an average of 25 and a standard deviation of 5# with a normal distribution. # For vector N create a histogram with the following parameters:#? enter the title of the chart ?Histogram”,#? enter "Data x" as the title of the x axis,#? enter "Data y" as the title of the y axis,#? set the color to green.N=rnorm(40,25,5)hist(N, probability=T, main = "Histogram", xlab="Data x", ylab="Data y", col="green") ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download