Base Plotting in R:

x <-rnorm(100)
hist(x)

y <- rnorm(100)
plot(x,y)

par(mar=c(4,4,1,15))
plot(x,y)

par(mar=c(15,4,4,4))
plot(x,y,pch=21, col="blue", cex =1.5, bg = "green") # filled circles

#plot(x,y,pch=20) # filled circles
#plot(x,y,pch=15) # filled squares
#plot(x,y,pch=10) #diamonds with crosses
#plot(x,y,pch=5) #open diamonds
#plot(x,y,pch=4) # x signs 
#plot(x,y,pch=3) # + signs
#plot(x,y,pch=2) # triangles
#plot(x,y,pch=1) # open circles
par(mar=c(6,6,6,6))
plot(x, xlab ="weight", y, ylab="height", main="Scatterplot demo", pch=24, col="red", cex =0.75, bg = "green")
#title("scatterplot")
text(-2,-2,"label")
legend("topright", legend="legend", pch=24, col="red", cex =0.75, bg = "green")
fit <-lm(y~x)
abline(fit, lwd=1.5, col="red")

z<-rpois(100,2)
par(mfrow=c(2,1))
plot(x,y)
plot(x,z)

par(mar=c(4,4,2,2))
par(mfrow=c(1,2))
plot(x,y)
plot(x,z)

par(mar=c(4,4,2,2))
par(mfrow=c(4,2))
plot(x,y)
plot(x,z)
plot(x,x)
plot(y, x)
plot(y,y)
plot(y,z)
plot(z,x)
plot(z,y)

par(mar=c(4,4,2,2))
par(mfcol=c(4,2))
plot(x,y)
plot(x,z)
plot(x,x)
plot(y, x)
plot(y,y)
plot(y,z)
plot(z,x)
plot(z,y)

par(mfrow =c(1,1))
x<-rnorm(100)
y<-x+rnorm(100)
#2 groups
g <- gl(2,50, labels=c("Males", "Females"))
str(g)
##  Factor w/ 2 levels "Males","Females": 1 1 1 1 1 1 1 1 1 1 ...
plot(x, y)

plot(x, y, type = "n")
points(x[g == "Males"], y[g == "Males"], col= "red")
points(x[g == "Females"], y[g == "Females"], col= "green")
legend("topleft", pch = 1, col = c("red", "green"), legend = c("Males", "Females"))

Graphic Devices in R:

library(datasets)
with(faithful, plot(eruptions, waiting)) ## Make plot appear on screen device
title(main = "Old Faithful Geyser data") ## Annotate with a title

pdf(file = "myplot.pdf") ## Open PDF device; create 'myplot.pdf' in my working directory
## Create plot and send to a file (no plot appears on screen)
with(faithful, plot(eruptions, waiting))
title(main = "Old Faithful Geyser data") ## Annotate plot; still nothing on screen
dev.off() ## Close the PDF file device
## png 
##   2
## Now you can view the file 'myplot.pdf' on your computer
library(datasets)
with(faithful, plot(eruptions, waiting)) ## Create plot on screen device
title(main = "Old Faithful Geyser data") ## Add a main title

dev.copy(png, file = "geyserplot.png") ## Copy my plot to a PNG file
## png 
##   3
dev.off() ## Don't forget to close the PNG device!
## png 
##   2

#by Linda, September 2020