260 likes | 268 Views
A reference guide with examples for plotting in R. Introduction to R and Rstudio. Basic R abilities, packages, paths, data. Learn about the power and flexibility of R. Make beautiful plots effortlessly.
E N D
Easy plotting with R Yoo! R so beautiful Talia Kustin reference
Examples reference
Agenda • Introduction to R and Rstudio • Basic R abilities • Packages, paths, data • Plotting in R: • Introduction • First plot together • Second plot – on your own!
What is R? • Open source programming language • Great for statistical computing, graphics and data analysis
Why use R? • R is free • R is popular • R is powerful • R is flexible • R is supported
How does R work? • Functions: • Print • Plot graph • Open file • Variables: • Data and tables • Text • Numbers my_name<- “Talia Kustin” Function- print(my_name) Result - “Talia Kustin”
Keyboard shortcuts • Control + enter run current line / selection in console • Alt + - Insert assignment operator (<-) • Control + 1 Move to script editor • Control + 2 Move to console More shortcuts here
Script viewer • Write scripts and save them as files • Run lines in the console (Control + enter)
Data viewer • View data • Filter data
Console • Write command and run them • Run commands from the script viewer (Control + enter) • Saves history of commands run (up and down with the arrow keys) • Working path
Upper panel • Environment • Data – all the tables that are used in this session • Values – strings, numbers and more • History – all commands typed
Lower panel • Files – list of all files in the working directory • Plots – all the plots that we drew • Packages – all packages installed • (see specific slide) • Help – see next slide
Where can I get help? • R studio help • Ask the professionals
Useful packages in R • ggplot2 – plotting • ggsignif– statistical test for plots • plyr–summaries data and more • reshape2 - restructure and aggregate data
Packages in R • There are lot’s of packages in R • Install • Load
Installing packages in R • ggplot2 • ggsignif • plyr • reshape
Paths • Every directory and file has a path • Desktop - c:/Users/(username)/Desktop • File in desktop - c:/Users/(username)/Desktop/file.docx • When we work with files and directories we have to specify it to the program
Set directory • Specify a working directory: • setwd(“c:/Users/(username)/Desktop”) • Get the working directory: • getwd()
Data files • CSV = comma-separated values • R can work with excel files as well =
Data import • experiment<- read.csv(“c:/Users/(username)/Desktop/experiment.csv”) Or • setwd(“c:/Users/(username)/Desktop”) • experiment<- read.csv(”experiment.csv”) Or • file <- file.choose() #chose file from file explorer • experiment<- read.csv(file)
Data in R • Get information on data: • class(experiment ) • str(experiment ) • colnames(experiment ) • dim(experiment ) • experiment reference
Wide vs long formats • Usually we use wide format • For plotting in R we need long format melt dcast
experiment: Melt command(wide long) experiment_long <- melt(experiment, id.vars=c("subject", "sex"), variable.name="condition", value.name="measurement" ) הפקודה משתנים קטגוריאליים ה-data.frame הישן ה-data.frame החדש שם המשתנה החדש שם הערך
Wide vs long formats experiment_long experiment
experiment_long: Data in R • Get information on data #2 • max(experiment_long$measurement) – 13.8 • min(experiment_long$measurement) – 1.3 • mean(experiment_long$measurement) - 9.116667 • unique(experiment_long$condition) – control cond1 cond2 • length(experiment_long$sex[which(experiment_long$sex=="Female")]) - 6