100% found this document useful (1 vote)
34 views

07 Scatterplot Barplot Piechart

This document provides an overview of various types of plots that can be created in R, including scatterplots, bar plots, pie charts, boxplots, and instructions on exporting plots. It lists basic plotting functions and concepts in R like the use of xlab, xlim, main, and pch parameters. It also provides tips on saving graphic files in different formats like PDF and PNG.

Uploaded by

the killerboy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
34 views

07 Scatterplot Barplot Piechart

This document provides an overview of various types of plots that can be created in R, including scatterplots, bar plots, pie charts, boxplots, and instructions on exporting plots. It lists basic plotting functions and concepts in R like the use of xlab, xlim, main, and pch parameters. It also provides tips on saving graphic files in different formats like PDF and PNG.

Uploaded by

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

Scatterplots, Barplots, Piechart, Factors

To remember:
Basic operators ToDo:
Variable assignment  Boxplots
Basic data types
 Scatterplots
Vectors
Matrices  Bar-plots
Row and column names  Pie-chart
Indexing a matrix
 Lists
 Data frames
 Time series
 Arrays
 Read and writ data
 Plots
 Exporting plots
#Set the working directory- getwd()/ setwd("Y:/")
getwd()
setwd("C:/Users/nobel/Desktop/research methodology
MSC")
getwd()

####Study_1
study_1 <- read_excel("Tree_height.xlsx", sheet =
"study_1")
str(study_1)
head(study_1)
ToDo 3
# exporting plots as graphic files
#Customize your plot (as you like: color, box type) and save it as a pdf file with width
= 7 and height = 5
pdf(file = "plot2.pdf", width = 7, height = 5) # specify size of the graphics region (in
inches)
plot(temp ~ time, data=beaver1)
dev.off()
# copy an existing plot from Rstudio to a pdf-file
plot(temp ~ time, data=beaver1, col="green")
dev.print(device=pdf, file="plot3.pdf", width=5, height=7, paper="a4")

# If you need a raster graphics file instead of PDF:


# Export PNG
# Export the plot as a PNG file with width=800, height=600 and units = "px“
png(file="plot.png", width=800, height=600, units = "px") # specify plot size in pixels
plot(temp ~ time, data=beaver1)
dev.off()

# or copy an existing plot


plot(temp ~ time, data=beaver1, col="red")
dev.print(device=png, file="plot2.png", width=800, height=600, units = "px")
Which are the tree basic groups of plotting in R?
High-level plotting functions
Low-level plotting functions
Interactive graphics functions
What is doing:
xlab?
xlim?
main?
pch?
par()

How do you save graphic files?


## Default S3 method:
boxplot(x, ..., range = 1.5, width = NULL,
varwidth = FALSE,
notch = FALSE, outline = TRUE,
names, plot = TRUE,
border = par("fg"), col = NULL, log
= "",
pars = list(boxwex = 0.8, staplewex =
0.5, outwex = 0.5),
horizontal = FALSE, add = FALSE,
at = NULL)

#How to put values on boxplot and


control its width?
boxplot(Tree, horizontal = TRUE, axes =
FALSE, staplewex = 0.9)

http://earlh.com/blog/2011/09/18/labeled-boxplot-in-r/
Pie-chart

A pie-chart is a representation of values as slices of a circle


with different colors.
In R the pie chart is created using the pie() function which
takes positive numbers as a vector input. The additional
parameters are used to control labels, color, title etc.
pie(x, labels, radius, main, col, clockwise)

x is a vector containing the numeric values used in the pie chart.


labels is used to give description to the slices.
radius indicates the radius of the circle of the pie chart.(value between
−1 and +1).
main indicates the title of the chart.
col indicates the color palette.
clockwise is a logical value indicating if the slices are drawn
clockwise or anti clockwise.

https://www.tutorialspoint.com/r/r_pie_charts.htm
Scatterplots

A graph of plotted points that show the relationship


between two sets of data.
-scatter draws points without lines
The simple scatterplot is created using the plot() function.

ToDo:
Read the Tree.csv file in R
Plot the Elevation and the Diameter of the Tree data set,
change the points symbol to star and the color blue
………………………………..
Plot the Height and the Diameter of the Tree data set,
change the points symbol and the color to what you want
and give a title
………………………….
Make a multi-panel plots (use function par() and mfrow)
……………………………….
ToDo1
Read the Tree.csv file in R and make a boxplot
with the tree height and tree species, give the
axes the proper names:………………………
Change the color in blue:
……………………..
Can you make the boxplot horizontal?
…………………..
To Do
# Create data for the Tree pie-chart
# Assign the percentage of every species to a vector:
…………………………
#Assign the species name to a vector with a name that
you want:
……………………………….

#Plot the pie-chart, with the title: "pie-chart of tree


species“ and rainbow color pallet:
…………………………………….

Save the file


……………….
Strength -
-dots are widely dispersed, the
relationship is consider weak.
-dots are densed around a line then
the relationship is strong.

Linearity - data pattern is either


linear/straight or nonlinear/curved.

Slope - direction of change in


variable Y with respect to increase
in value of variable X. If Y
increases with increase in X, slope
is positive otherwise slope is
negative.
https://www.tutorialspoint.com/statistics/scatterplots.htm
# Create data for the graph.
x <- c(21, 62, 10, 53)
labels <- c("London", "New York", "Singapore",
"Mumbai")

# Give the chart file a name.


png(file = “……………")

# Plot the chart.


………………..
## Plot the chart 3D (install package plotrix)
……………………………….

# Save the file.


dev.off()
Kakum National Park, Ghana, Source: https://operationgroundswell.com
Bar chart
A bar chart represents data in rectangular bars with length of the bar
proportional to the value of the variable. R uses the
function barplot() to create bar charts.
barplot(H, xlab, ylab, main, names.arg, col)

# Create the data for the chart.


A<-c(4,18,45,29,4)
lbls<-c("Alnus", "Larix","Picea", "Pinus", "Sorbus")

# Give the chart file a name and save as png.


…………………..
# Plot the bar chart with an appropriate title, blue colour, red border
and no space between columns
……………………………..
# Save the file.
dev………………
##############
## To do ##
##############
#
# Use the beaver2 data set to create the following plot:
#
# * plot temperature against time with double-sized blue
circles with green backgrounds
# * remove the box around the plotting area
# * change the axis labels to be 1.3 times the normal size
# * change the text of the axis labels to include what was
measured plus the units of measurement
# * give the plot the heading "My Own Plot"
# * export the plot to a PDF file
African elephant in Ghana, Source: http://www.travelmyne.de/afrika/ghana

http://www.clayford.net/statistics/a-note-on-boxplots-in-r/

http://www.tutorialspoint.com/statistics/
https://www.tutorialspoint.com/r/r_bar_charts.htm

You might also like