0% found this document useful (0 votes)
50 views7 pages

2 R - Zajecia - 4 - Eng

The document discusses various chart types in R including bar plots, pie charts, line plots, histograms and more. It provides examples of how to create these visualizations using functions like plot(), barplot(), pie(), curve(), and hist() and customize them by adding titles, labels, colors. The document also includes tasks for the reader to create their own charts based on sample data vectors.

Uploaded by

Rasooly MN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views7 pages

2 R - Zajecia - 4 - Eng

The document discusses various chart types in R including bar plots, pie charts, line plots, histograms and more. It provides examples of how to create these visualizations using functions like plot(), barplot(), pie(), curve(), and hist() and customize them by adding titles, labels, colors. The document also includes tasks for the reader to create their own charts based on sample data vectors.

Uploaded by

Rasooly MN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

R - programming language and software environment for statistical

computation and visualization of results

Class 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 2
seq(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 elements
rep(1:4, each = 2, len = 10)

# Operators sample() and rnorm()


# sample() creates a random permutation of the vector given as a parameter
sample(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 distribution
rnorm(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 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)

# 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 plot
plot(data)

# create a plot with lines - this plot is more readable


plot(data, type = "l")

# create a plot with both points and lines


plot(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).

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)

# Example 2. Bar chart of the age of a students’ group


age = c(21,18,19,21,22,19,22,20,23,22)
barplot(age)

# age vector summarized by count


table(age)

# bar chart of age vector summarized by count


barplot(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 cars


cars = c(1, 3, 6, 4, 9)
trucks = c(2, 5, 4, 5, 12)
suvs = c(4, 4, 6, 6, 16)

# create data frame autos_data


autos_data = data.frame(cars, trucks, suvs)

# 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))

# 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’ group


age = c(21,18,19,21,22,19,22,20,23,22)
pie(age)

# pie chart of age vector summarized by count


pie(table(age))

# add to the chart: colors in greyscale


pie(table(age), col=grey(0:4/4))

# Example 2. Pie chart of weekly sales of various types of cars


cars <- c(1, 3, 6, 4, 9)
pie(cars)

# add to the chart: main title, colors and labels


pie(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 place
car_labels <- round(cars/sum(cars) * 100, 1)

# concatenate a % sign after each value


car_labels <- paste(car_labels, "%", sep="")

# add to the chart: main title, colors and custom labels


pie(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 parliament


parties = 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")

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)

# Example 2. Chart with chi^2 distribution for different degrees of freedom


x = seq(from=0, to =10, by=0.1)
curve(dchisq(x,1), xlim=c(0,10), ylim=c(0,0.6)) #1 degree of freedom
curve(dchisq(x,4), add=T) # 4 degrees of freedom

# add to the chart: main title, colors and thick lines


curve(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 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")))

curve(dchisq(x,4), add=T, col="green", lwd=3)

# Histogram hist()
# Example 1. Histogram and normal distribution
# sample: 200 numbers
proba = rnorm(200)

# create a histogram
hist(proba,20,probability=T, col="yellow")

# add to the chart: a normal distribution curve


curve(dnorm(x), lwd=3, col="red", add=T)

# Other graphs
# boxplot() - box chart
# stars - radar chart
# mosaicplot - mosaic chart
# pairs() - set of scatterplots

5
# 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.

6
Task 1:

cars <- c(3, 5, 8, 6, 9)


plot(cars, type = "b", pch = 16, col = "blue", main = "Autos", font.main = 4, col.main = "red")

Task 2:

W <- 4:13
barplot(W, main = "Chart", xlab = "arguments", ylab = "values", col = "red")

Task 3:

wx <- rep(1:4, each = 8)


pie(wx, col = rainbow(length(unique(wx))))
png("pie_chart.png")
pie(table(wx), col = rainbow(length(unique(wx))))
dev.off()

Task 4:

x <- seq(-2*pi, 2*pi, length.out = 100)


y1 <- cos(x)
y2 <- sin(x)
plot(x, y1, type = "l", col = "blue", xlab = "x", ylab = "y", main = "Trigonometric Functions")
lines(x, y2, col = "red")

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:

N <- rnorm(40, mean = 25, sd = 5)


hist(N, main = "Histogram", xlab = "Data x", ylab = "Data y", col = "green")

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy