2 R - Zajecia - 4 - Eng
2 R - Zajecia - 4 - Eng
# seq() generates a sequence, parameter 'by' specifies the step of the sequence
# sequence of numbers from 1 to 100 with step 2
seq(1,100,by=2)
# numbers from 1 to 4, each repeated twice, until the desired length is reached
# in total 10 elements
rep(1:4, each = 2, len = 10)
# 10-times draw from a set of numbers 1:6 (e.g. 10-times dice roll simulation)
sample(1:6,10,replace=T)
# 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 commands
install.packages("ggplot2")
library(ggplot2)
1
# 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)
# 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)
# create a dot plot - this one looks the same as the first plot
plot(data)
2
# Bar chart barplot()
# Example 1. Bar chart for weekly measurements of maximal temperature
max.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)
# 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)
# create a bar chart of the variable Suvs with specified labels for axes and orange bar borders
barplot(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))
3
# create a bar chart of all the variables in autos_data with adjacent bars using rainbow colors
barplot(as.matrix(autos_data), main="Autos", ylab= "Total", beside=TRUE,
col=rainbow(5))
# calculate the percentage for each day, rounded to one decimal place
car_labels <- round(cars/sum(cars) * 100, 1)
4
# 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 add
curve(sin(x), 0, 10, add=TRUE)
# By default, the function graph line is black. Change the line color to red and blue
curve(log10(x), 0.1, 10, col="red")
curve (sin(x), 0, 10, col="blue", add=T)
# add to the chart: legend at the top right side and colors
curve(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")))
# Histogram hist()
# Example 1. Histogram and normal distribution
# sample: 200 numbers
proba = rnorm(200)
# create a histogram
hist(proba,20,probability=T, col="yellow")
# Other graphs
# boxplot() - box chart
# stars - radar chart
# mosaicplot - mosaic chart
# pairs() - set of scatterplots
5
# Tasks
# 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 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.
6
Task 1:
Task 2:
W <- 4:13
barplot(W, main = "Chart", xlab = "arguments", ylab = "values", col = "red")
Task 3:
Task 4:
Task 5:
M <- rnorm(400)
hist(M, main = "Histogram", xlab = "Data x", ylab = "Data y", col = "lightblue")
curve(dnorm(x), lwd = 4, col = "red", add = TRUE)
png("histogram.png")
hist(M, main = "Histogram", xlab = "Data x", ylab = "Data y", col = "lightblue")
curve(dnorm(x), lwd = 4, col = "red", add = TRUE)
dev.off()
Task 6: