This shows how to make a simple scatterplot as well as customize a few aspects of the plot in R
I will be using a sample data frame built into R to demonstrate, but to import one yourself from excel, you can use ‘read.table’ as follows
If you save it from excel as a ‘tab-separated text file’, here’s an example how to load it into R:
mydata <- read.table("/Users/timothyjohnstone/Dropbox/GiraldezLab/tinyORFs/aifNanoTest.txt",
header=T,
sep="\t")
head(mydata)
## orfID geneID
## 1 ENSDART00000000070_51815_45 ENSDARG00000000069
## 2 ENSDART00000000564_23663_102 ENSDARG00000000540
## 3 ENSDART00000001318_32544_286 ENSDARG00000033450
## 4 ENSDART00000001444_37788_235 ENSDARG00000001313
## 5 ENSDART00000002241_5403_108 ENSDARG00000019588
## 6 ENSDART00000002244_29659_416 ENSDARG00000016855
## max_orfFrame_GLOBAL_wholeTx orfFrame_GLOBAL orfRate_GLOBAL F1_GLOBAL
## 1 4626.433 138.30645 124 103
## 2 1429.323 74.00000 37 37
## 3 3571.580 83.56522 92 70
## 4 6365.453 116.14876 121 94
## 5 3507.711 88.12766 47 46
## 6 5496.359 676.46347 479 428
## F2_GLOBAL F3_GLOBAL covF1Raw_GLOBAL
## 1 13 8 4
## 2 0 0 4
## 3 22 0 2
## 4 27 0 2
## 5 1 0 4
## 6 24 27 3
In this script, I’ll use the built-in dataset called mtcars which contains data about various types of cars and their running characteristics. Let’s take a look at that table:
head(mtcars)
## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
## Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
## Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
The basic plotting command in R defaults to a scatterplot if given two sets of numeric data. Here’s a simple example where we plot horsepower vs. piston displacement
plot(mtcars$disp, mtcars$hp)
It’s a simple plot, let’s add axis labels, a title, and make the points look nicer
plot(mtcars$disp, mtcars$hp,
main="Horsepower vs. displacement in various cars",
xlab="HP", ylab="Displacement (mm)",
pch=19, # changes the plot symbol to a nicer dot. there are plenty of options available
cex=0.7, # changes the size of the dots to be smaller (70% of default)
col="lightblue" #changes the color of the dots
)
Perhaps we want to plot two different subsets of the data, to compare 4-cylinder engines vs. 6-cylinder vs. 8 cylinder. We can use the ‘points’ command to add another set of points to a previous plot, and the ‘legend’ command to add a legend. Note that we have to actively change the axis limits here, since the axes would otherwise fit automatically to the first set of points and the second set is outside of them. Let’s also add an overall linear trendline for the data.
plot(mtcars[mtcars$cyl==4,]$disp, mtcars[mtcars$cyl==4,]$hp,
main="Horsepower vs. displacement in various cars",
xlab="HP", ylab="Displacement (mm)",
xlim=c(50,500), ylim=c(50,350), # Change the axis limits of the plot
pch=19, # changes the plot symbol to a nicer dot. there are plenty of options available
cex=0.8, # changes the size of the dots to be smaller (70% of default)
col="lightblue" #changes the color of the dots
)
points(mtcars[mtcars$cyl==6,]$disp, mtcars[mtcars$cyl==6,]$hp,
pch=14, # changes the plot symbol to a triangle box.
cex=0.8, # changes the size of the dots to be smaller (70% of default)
col="coral" #changes the color of the dots
)
points(mtcars[mtcars$cyl==8,]$disp, mtcars[mtcars$cyl==8,]$hp,
pch=12, # changes the plot symbol to a cross box.
cex=0.8, # changes the size of the dots to be smaller (70% of default)
col="purple" #changes the color of the dots
)
abline(lm(mtcars$hp ~ mtcars$disp),
col="gray47", #make it dark grey
lty="dashed", #make it dashed
lwd=0.5 #make it 50% of the default width
)
legend('topleft',legend = c("V4","V6","V8"), pch=c(19,14,12), col=c("lightblue","coral","purple"))
Many more graphical parameters controlling axes, line widths, etc. can be found by typing: ?par in the R console