Robin Choudhury
2018-07-04
For this Section we will be using RStudio
R can work like a calculator…
1 / 200 * 30
[1] 0.15
pi *3
[1] 9.424778
Base R knows pi
as the constant 3.14...
.
R can also handle text…
print("Hello, World!")
[1] "Hello, World!"
And can mix text and calculations…
paste("The value of pi is", pi)
[1] "The value of pi is 3.14159265358979"
You can also create new objects with <-
a <- 1 / 200 * 30; a
[1] 0.15
Then use those objects in subsequent calculations
a*3
[1] 0.45
Object names must start with a letter, and needs to contain letters, numbers, _, and .
You can make assignments with either <- or =, but most people prefer <-. Using <- originated when APL computers had a single <- key on them (see http://blog.revolutionanalytics.com/2008/12/use-equals-or-arrow-for-assignment.html for more info).
R is case sensitive, so A will not tell you the value of a
ggplot2 is part of a a suite of packages in R known colloquially as the 'Tidyverse'. ggplot2 was developed to build graphs by mapping aesthetics, allowing users to flexibly create beautiful publication-quality figures.
library(tidyverse)
library(datasets)
I will be using the iris dataset:
data(iris)
As mentioned earlier, ggplot2 works by layering on different aesthetics from a dataset. ggplot2 first needs to know what dataset you plan to use, and what will be plotted.
ggplot(data=iris, aes(x = Sepal.Length,y = Sepal.Width))
…pretty boring. Why?
Now we want to use a scatterplot method to mark out these two variables.
ggplot(data=iris, aes(x = Sepal.Length,y = Sepal.Width))+
geom_point()
ggplot(data=iris, aes(x = Sepal.Length,y = Sepal.Width))+
geom_point() + geom_smooth()
ggplot(data=iris, aes(x = Sepal.Length,y = Sepal.Width))+
geom_point(aes(color=Species)) + geom_smooth()
ggplot(data=iris, aes(x = Sepal.Length,y = Sepal.Width))+
geom_point(aes(color=Species, size=Petal.Length)) + geom_smooth()
ggplot(data=iris, aes(x = Sepal.Length,y = Sepal.Width,color=Species))+
geom_point(aes(size=Petal.Length)) + geom_smooth()
ggplot(data=iris, aes(x = Sepal.Length,y = Sepal.Width,color=Species))+
geom_point(aes(size=Petal.Length)) + geom_smooth(method="lm")
ggplot(data=iris, aes(x = Sepal.Length,y = Sepal.Width,color=Species))+
geom_point(aes(size=Petal.Length)) + geom_smooth(aes( linetype=Species), method="lm")
ggplot(data=iris, aes(x = Sepal.Length,y = Sepal.Width,color=Species))+
geom_point(aes(size=Petal.Length)) + geom_smooth(aes( linetype=Species), method="lm") +
geom_rug()
ggplot(data=iris, aes(x = Sepal.Length,y = Sepal.Width,color=Species))+
geom_point(aes(size=Petal.Length)) +
geom_density2d()
ggplot(data=iris, aes(x = Sepal.Length,y = Sepal.Width))+
geom_hex()
ggplot(data=iris, aes(Sepal.Length,fill=Species))+
geom_histogram()
ggplot(data=iris, aes(Sepal.Length,fill=Species))+
geom_histogram(position = "dodge")
ggplot(data=iris, aes(Sepal.Length,fill=Species))+
geom_histogram(color="black")
ggplot(data=iris, aes(Sepal.Length,fill=Species))+
geom_histogram(aes(color="black"))
ggplot(data=iris, aes(Sepal.Length,fill=Species))+
geom_histogram(aes(color="black"), alpha=0.5)
ggplot(data=iris, aes(Sepal.Length,fill=Species))+
geom_histogram(color="black")+
theme_bw()
ggplot(data=iris, aes(Sepal.Length,fill=Species))+
geom_histogram(color="black", alpha=0.5)+
theme_bw() +
theme(legend.position="bottom")
ggplot(data=iris, aes(Sepal.Length,fill=Species))+
geom_histogram(color="black", alpha=0.5)+
theme_bw() +
theme(legend.position="bottom",
axis.title=element_text(size=20, face="bold"))
ggplot(data=iris, aes(Sepal.Length,fill=Species))+
geom_histogram(color="black", alpha=0.5)+
theme_bw() + ylab("Count")+xlab("Sepal Length")+
theme(legend.position="bottom",
axis.title=element_text(size=20, face="bold"))
ggplot(data=iris, aes(Sepal.Length,fill=Species))+
stat_bin()
ggplot()+
geom_point(data=iris, aes(Sepal.Length, Sepal.Width))+
geom_smooth(data=iris, aes(Sepal.Length, Sepal.Width ))
ggplot(data=iris, aes(Species, Sepal.Width))+
geom_jitter(alpha=0.2)+ geom_boxplot()
ORDER MATTERS!
ggplot(data=iris, aes(Sepal.Length, Sepal.Width))+
geom_jitter(alpha=0.2)+ geom_smooth()+
facet_grid(Species~.)
nz <- map_data("nz")
ggplot(nz, aes(long, lat, group = group)) +
geom_polygon(fill = "white", colour = "black")+
coord_cartesian()
data("world.cities")
nz.city <- world.cities %>% filter(country.etc=="New Zealand")
ggplot(data=nz, aes(long, lat)) +
geom_polygon(aes(group = group),fill = "white", colour = "black") +
geom_point(data=nz.city, aes(long, lat, size = pop)) +
coord_cartesian()