Skip to content
Snippets Groups Projects
Commit 50179ee3 authored by Elisabetta Ronchieri's avatar Elisabetta Ronchieri
Browse files

Delete ggplot2.Rmd

parent 65cb64b8
No related branches found
No related tags found
No related merge requests found
---
title: "Esempi ggplot"
author: "ronchieri"
date: "22 febbraio 2017"
output:
html_document: default
pdf_document: default
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, comment ="")
library(ggplot2)
```
## Libro di riferimento [R Graphics Cookbook](http://ase.tufts.edu/bugs/guide/assets/R%20Graphics%20Cookbook.pdf)
## Ulteriore materiale [Introduction to ggplot2](http://opr.princeton.edu/workshops/Downloads/2015Jan_ggplot2Koffman.pdf) di Dawn Hoffman
####Scatter plot
Soluzione 1
```{r}
plot(mtcars$wt, mtcars$mpg)
qplot(mtcars$wt, mtcars$mpg)
```
Soluzione 2
```{r}
qplot(wt, mpg, data=mtcars)
ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point()
```
####Grafico a linee
Soluzione 1
```{r}
plot(pressure$temperature, pressure$pressure, type="l")
plot(pressure$temperature, pressure$pressure, type="l")
points(pressure$temperature, pressure$pressure)
lines(pressure$temperature, pressure$pressure/2, col="red")
points(pressure$temperature, pressure$pressure/2, col="red")
qplot(pressure$temperature, pressure$pressure, geom="line")
```
Soluzione 2
```{r}
qplot(temperature, pressure, data=pressure, geom="line")
ggplot(pressure, aes(x=temperature, y=pressure)) + geom_line()
```
Soluzione 3
```{r}
# Linee e punti insieme
qplot(temperature, pressure, data=pressure, geom=c("line", "point"))
ggplot(pressure, aes(x=temperature, y=pressure)) + geom_line() + geom_point()
```
#### Grafico a barra
Soluzione 1
```{r}
barplot(BOD$demand, names.arg=BOD$Time)
```
Soluzione 2
```{r}
table(mtcars$cyl)
barplot(table(mtcars$cyl))
qplot(factor(mtcars$cyl))
```
Soluzione 3
```{r}
ggplot(BOD, aes(x=Time, y=demand)) + geom_bar(stat="identity")
ggplot(mtcars, aes(x=factor(cyl))) + geom_bar()
```
#### Creare istogramma
Soluzione 1
```{r}
hist(mtcars$mpg)
# Specificare il numero dei bin con breaks
hist(mtcars$mpg, breaks=10)
qplot(mtcars$mpg)
qplot(mpg, data=mtcars, binwidth=4)
# Equivalente a
ggplot(mtcars, aes(x=mpg)) + geom_histogram(binwidth=4)
```
#### crare un box plot
Soluzione 1
```{r}
plot(ToothGrowth$supp, ToothGrowth$len)
boxplot(len ~ supp, data = ToothGrowth)
# si confronta supp+dose con len
boxplot(len ~ supp + dose, data = ToothGrowth)
qplot(ToothGrowth$supp, ToothGrowth$len, geom="boxplot")
qplot(supp, len, data=ToothGrowth, geom="boxplot")
# Equivalente a
ggplot(ToothGrowth, aes(x=supp, y=len)) + geom_boxplot()
qplot(interaction(ToothGrowth$supp, ToothGrowth$dose), ToothGrowth$len,
geom="boxplot")
qplot(interaction(supp, dose), len, data=ToothGrowth, geom="boxplot")
# Equivalente a
ggplot(ToothGrowth, aes(x=interaction(supp, dose), y=len)) + geom_boxplot()
```
#### plottare una funzione
Soluzione 1
```{r}
curve(x^3 - 5*x, from=-4, to=4)
# Definire una funzione da usare nella curva
myfun <- function(xvar) {
1/(1 + exp(-xvar + 10))
}
curve(myfun(x), from=0, to=20)
# Aggiungere una linea
curve(1-myfun(x), add = TRUE, col = "red")
# This sets the x range from 0 to 20
#qplot(c(0,20), fun=myfun, stat="function")
# Equivalente a
ggplot(data.frame(x=c(0, 20)), aes(x=x)) + stat_function(fun=myfun, geom="line")
```
#### Fare un dot plot o cleveland
Soluzione 1
```{r}
library(gcookbook)
# For the data set
tophit <- tophitters2001[1:25, ]
# Take the top 25 from the tophitters data set
ggplot(tophit, aes(x=avg, y=name)) + geom_point()
tophit[, c("name", "lg", "avg")]
ggplot(tophit, aes(x=avg, y=reorder(name, avg))) +
geom_point(size=3) +
# Use a larger dot
theme_bw() +
theme(panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
panel.grid.major.y = element_line(colour="grey60",
linetype="dashed"))
```
Soluzione 2
```{r}
ggplot(tophit, aes(x=reorder(name, avg), y=avg)) +
geom_point(size=3) +
# Use a larger dot
theme_bw() +
theme(axis.text.x = element_text(angle=60, hjust=1),
panel.grid.major.y = element_blank(),
panel.grid.minor.y = element_blank(),
panel.grid.major.x = element_line(colour="grey60", linetype="dashed"))
```
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment