data.frame “Grammatik för grafik”
data: en data.frame med all dataaes: aesthteic mappings (färger, former)geom: geometriska objektfacets: skapar subplottar baserat på villkorscales: skalor för aesthteic mappingscoordinate system
Bild från denna källa 
print()
ggplot()
+”, 
+ geom_line()”In brief, the grammar tells us that a statistical graphic is a mapping from data to aesthetic attributes (colour, shape, size) of geometric objects (points, lines, bars). The plot may also contain statistical transformations of the data and is drawn on a specific coordinate system.”
Från “ggplot2 book” av Hadley Wickham
aes | 
Beskrivning | 
|---|---|
x | 
x-axel | 
y | 
y-axel | 
size | 
storlek | 
color | 
färg | 
shape | 
form | 
geom | 
Beskrivning | 
|---|---|
geom_point | 
Scatterplot | 
geom_line | 
Line graph | 
geom_bar | 
Barplot | 
geom_boxplot | 
Boxplot | 
geom_histogram | 
Histogram | 
geom | 
aes | 
|---|---|
geom_points | 
point shape, point size | 
geom_line | 
line type, line size | 
geom_bar | 
y min, y max, fill color, outline color | 
ggplot(data=Nile) + aes(x=years, y=level) + geom_point()
ggplot(data=Nile) + aes(x=years, y=level) + geom_line()
p <- ggplot(data=Nile) + aes(x=years, y=level, color=period) +  geom_point(aes(shape=period))
p
p <- ggplot(data=Nile) + aes(x=years, y=level, color=period) + geom_line(aes(type=period)) + geom_point()
p
p <- ggplot(data=Nile) + aes(x=years, y=level) + facet_grid(period~.) + geom_line()
p
p <- ggplot(data=Nile) + aes(x=years, y=level) + facet_grid(~period) + geom_line()
p
p + theme_bw()
p + theme_classic()
p + theme_grey()
qplot(): Liknar plot() från basgrafiken.ggplot()chisq.test(), fisher.test()t.test()cor() och cov() och testas medcor.test()data("chickwts")
horsebean <- chickwts$weight[chickwts$feed == "horsebean"]
sunflower <- chickwts$weight[chickwts$feed == "sunflower"]
mean(horsebean)
[1] 160.2
mean(sunflower)
[1] 328.9167
t.test(horsebean,alternative="two.sided", mu = 150, conf.level = 0.95)
    One Sample t-test
data:  horsebean
t = 0.83507, df = 9, p-value = 0.4253
alternative hypothesis: true mean is not equal to 150
95 percent confidence interval:
 132.5687 187.8313
sample estimates:
mean of x 
    160.2 
t.test(horsebean, sunflower,alternative="two.sided", mu = 0, conf.level = 0.95)
    Welch Two Sample t-test
data:  horsebean and sunflower
t = -9.0449, df = 19.964, p-value = 1.69e-08
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -207.6313 -129.8021
sample estimates:
mean of x mean of y 
 160.2000  328.9167 
y ~ x1 + x2
Används på flera håll i R (ex. facet-grid)library(MASS)
library(car)
data(Prestige)
mod1 <- lm(prestige ~ income + women + education, data=Prestige)
mod2 <- lm(prestige ~ income + women + education - 1, data=Prestige)
mod3 <- lm(prestige ~ income:women + education, data=Prestige)
summary()anova()Exempel:
summary(mod1)
anova(mod1)
anova(mod3, mod1, test="Chisq")
plot(mod1)
durbinWatsonTest(mod1)
qqPlot(mod1)  
resid()predict()