Grafici in R Strumenti quantitativi per la gestione
Transcript
Grafici in R Strumenti quantitativi per la gestione
2/23/2015 Grafici in R Grafici in R Strumenti quantitativi per la gestione Emanuele Taufer Caricare il dataset da una libreria Plot Boxplot Istogramma Scatterplot matrice Disegnare linee aggiuntive Caricare il dataset da una libreria Carichiamo il file Auto dalla libreria ISLR (da installare) library(ISLR) data(Auto) head(Auto) mpg cylinders displacement horsepower weight acceleration year origin 1 18 8 307 130 3504 12.0 70 1 2 15 8 350 165 3693 11.5 70 1 3 18 8 318 150 3436 11.0 70 1 4 16 8 304 150 3433 12.0 70 1 5 17 8 302 140 3449 10.5 70 1 6 15 8 429 198 4341 10.0 70 1 name 1 chevrolet chevelle malibu 2 buick skylark 320 3 plymouth satellite 4 amc rebel sst 5 ford torino 6 ford galaxie 500 names e str ci danno informazioni sulle variabili contenute nel file names(Auto) ## [1] "mpg" "cylinders" "displacement" "horsepower" ## [5] "weight" "acceleration" "year" "origin" ## [9] "name" str(Auto) file:///C:/Users/emanuele.taufer/Dropbox/3%20SQG/Labs/Lab_1__grafici.html 1/17 2/23/2015 Grafici in R ## 'data.frame': 392 obs. of 9 variables: ## $ mpg : num 18 15 18 16 17 15 14 14 14 15 ... ## $ cylinders : num 8 8 8 8 8 8 8 8 8 8 ... ## $ displacement: num 307 350 318 304 302 429 454 440 455 390 ... ## $ horsepower : num 130 165 150 150 140 198 220 215 225 190 ... ## $ weight : num 3504 3693 3436 3433 3449 ... ## $ acceleration: num 12 11.5 11 12 10.5 10 9 8.5 10 8.5 ... ## $ year : num 70 70 70 70 70 70 70 70 70 70 ... ## $ origin : num 1 1 1 1 1 1 1 1 1 1 ... ## $ name : Factor w/ 304 levels "amc ambassador brougham",..: 49 36 231 14 161 141 54 223 241 2 ... Plot Possiamo usare la funzione plot() per produrre uno scatterplot di variabili quantitative. Tuttavia, digitando semplicemente i nomi delle variabili si produrrà un messaggio d’errore, perché R non sa dove cercare i dati. L’istruzione corretta è plot(Auto$cylinders, Auto$mpg) file:///C:/Users/emanuele.taufer/Dropbox/3%20SQG/Labs/Lab_1__grafici.html 2/17 2/23/2015 Grafici in R In alternativa è possibile istruire R per cercare i dati nel file Auto con la funzione attach() attach(Auto) plot(cylinders, mpg) file:///C:/Users/emanuele.taufer/Dropbox/3%20SQG/Labs/Lab_1__grafici.html 3/17 2/23/2015 Grafici in R Boxplot La variabile cylinders è memorizzata come un vettore numerico, e quindi R la tratta come quantitativa. Tuttavia, poiché vi sono solo un piccolo numero di possibili valori per cylinders , può essere preferibile trattare la variabile cylinders come qualitativa. La funzione as.factor() converte le variabili quantitative in qualitative cylinders = as.factor (cylinders) Se la variabile tracciata sull’asse x è categorica, allora la funzione plot() produrrà automaticamente un boxplot. Alcuni esempi, con diverse opzioni, di seguito. L’opzione varwidth=TRUE produce boxplot con largehzza proporzionale alla radice della dimensione del gruppo. plot(cylinders, mpg) file:///C:/Users/emanuele.taufer/Dropbox/3%20SQG/Labs/Lab_1__grafici.html 4/17 2/23/2015 Grafici in R plot(cylinders, mpg, col="red") file:///C:/Users/emanuele.taufer/Dropbox/3%20SQG/Labs/Lab_1__grafici.html 5/17 2/23/2015 Grafici in R plot(cylinders, mpg, col="red", varwidth=T) file:///C:/Users/emanuele.taufer/Dropbox/3%20SQG/Labs/Lab_1__grafici.html 6/17 2/23/2015 Grafici in R plot(cylinders, mpg, col="red", varwidth=T,horizontal=T) file:///C:/Users/emanuele.taufer/Dropbox/3%20SQG/Labs/Lab_1__grafici.html 7/17 2/23/2015 Grafici in R plot(cylinders, mpg, col="red", varwidth=T, xlab="cylinders", ylab="MPG") file:///C:/Users/emanuele.taufer/Dropbox/3%20SQG/Labs/Lab_1__grafici.html 8/17 2/23/2015 Grafici in R Istogramma la funzione hist() produce istogrammi hist(mpg) file:///C:/Users/emanuele.taufer/Dropbox/3%20SQG/Labs/Lab_1__grafici.html 9/17 2/23/2015 Grafici in R hist(mpg,col=2) file:///C:/Users/emanuele.taufer/Dropbox/3%20SQG/Labs/Lab_1__grafici.html 10/17 2/23/2015 Grafici in R hist(mpg,col=2,breaks=15) file:///C:/Users/emanuele.taufer/Dropbox/3%20SQG/Labs/Lab_1__grafici.html 11/17 2/23/2015 Grafici in R Scatterplot matrice La funzione pairs() produce scatter plot di tutte le variabili di un dataset pairs(Auto) file:///C:/Users/emanuele.taufer/Dropbox/3%20SQG/Labs/Lab_1__grafici.html 12/17 2/23/2015 Grafici in R pairs(~ mpg + displacement + horsepower + weight + acceleration, Auto) file:///C:/Users/emanuele.taufer/Dropbox/3%20SQG/Labs/Lab_1__grafici.html 13/17 2/23/2015 Grafici in R Disegnare linee aggiuntive Linee aggiuntive possono essere inserite nel grafico con la funzione abline(a,b) dove a indica l’intercetta, b la pendenza plot(horsepower,mpg) abline(40,‐0.15) file:///C:/Users/emanuele.taufer/Dropbox/3%20SQG/Labs/Lab_1__grafici.html 14/17 2/23/2015 Grafici in R plot(horsepower,mpg) abline(v=150) abline(h=20) file:///C:/Users/emanuele.taufer/Dropbox/3%20SQG/Labs/Lab_1__grafici.html 15/17 2/23/2015 Grafici in R La retta ottenuta dalla funzione lm può essere tracciata richiamando l’oggetto output di lm reg<‐ lm(mpg~horsepower) plot(horsepower,mpg) abline(reg) file:///C:/Users/emanuele.taufer/Dropbox/3%20SQG/Labs/Lab_1__grafici.html 16/17 2/23/2015 file:///C:/Users/emanuele.taufer/Dropbox/3%20SQG/Labs/Lab_1__grafici.html Grafici in R 17/17