# É er®ando que se aprende - 2 Uma introdução a linguagem R para análise de dados - 2. Parte 2: http://bit.ly/aprendeerrando2 Parte 3: http://bit.ly/aprendeerrando3 Parte 4: http://bit.ly/aprendeerrando4 Parte 1: http://bit.ly/aprendeerrando ## Instalando pacotes ```r install.packages("ggplot") install.packages("ggplot2") install.packages("gcookbook") ``` ## Carregando pacotes instalados ```r library("ggplot") library("ggplot2") library("gcookbook") ``` ## Gráficos ### Scatterplot ```r plot(mtcars$wt, mtcars$mpg) qplot(mtcars$wt, mtcars$mpg) ``` ![](https://i.imgur.com/FyDDWke.png) ```r #Tipos de pontos diferentes ggplot(heightweight, aes(x=ageYear, y=heightIn)) + geom_point(shape=3) #Mapeando uma variavel continua por cor ou tamanho heightweight[,c("sex", "ageYear", "heightIn", "weightLb")] ggplot(heightweight, aes(x=ageYear, y=heightIn, size=weightLb)) + geom_point() ggplot(heightweight, aes(x=ageYear, y=heightIn, colour=weightLb)) + geom_point() ``` ### Gráfico de linhas ```r 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 = c("line", "point")) #Gráfico de área ggplot(uspopage, aes(x=Year, y=Thousands, fill=AgeGroup)) + geom_area() ggplot(uspopage, aes(x=Year, y=Thousands, fill=AgeGroup)) + geom_area(colour="black", size=2, alpha=.4)+ scale_fill_brewer(palette="Blues", breaks=rev(levels(uspopage$AgeGroup))) ``` ### Gráfico de barras ```r barplot(BOD$demand, names.arg = BOD$Time, xlab = "Eixo X", ylab = "Eixo Y") ggplot(pg_mean, aes(x = group, y = weight)) + geom_bar(stat = "identity") str(BOD) ggplot(BOD, aes(x = Time, y = demand)) + geom_bar(stat = "identity") #Convertendo Time para variável categórica ggplot(BOD, aes(x = factor(Time), y = demand)) + geom_bar(stat = "identity") ggplot(pg_mean, aes(x = group, y = weight)) + geom_bar(stat = "identity", fill = "lightblue", colour = "black") #Colorindo grafico de barras upc <- subset(uspopchange, rank(Change)>40) upc ggplot(upc, aes(x=Abb, y=Change, fill=Region)) + geom_bar(stat="identity") ggplot(upc, aes(x=reorder(Abb, Change), y=Change, fill=Region)) + geom_bar(stat="identity", colour="black") + scale_fill_manual(values=c("#669933", "#FFCC66")) + xlab("State") #Colorindo barras csub <- subset(climate, Source=="Berkeley" & Year >= 1900) csub$pos <-csub$Anomaly10y >= 0 csub ggplot(csub, aes(x=Year, y=Anomaly10y, fill=pos)) + geom_bar(stat="identity", position="identity") #Colorindo barras positivas e negativas ggplot(csub, aes(x=Year, y=Anomaly10y, fill=pos)) + geom_bar(stat="identity", position="identity", colour="black", size=0.25) + scale_fill_manual(values=c("#CCEEFF", "#FFDDDD"), guide=FALSE) ``` ### Histograma ```r hist(mtcars$mpg) ``` ### BoxPlot ```r plot(ToothGrowth$supp, ToothGrowth$len) qplot(ToothGrowth$supp, ToothGrowth$len, geom = "boxplot") boxplot(len~supp+dose, data = ToothGrowth) ``` ### Funções Curvas ```r curve(x^3 - 5*x, from= -4, to = 4) teste <- function(xvar) { 1/(1 + exp(-xvar + 10)) } curve(teste(x), from = 0, to = 20) curve(1-teste(x), add = TRUE, col = "red") ``` ### Editando legendas ```r p <- ggplot(PlantGrowth, aes(x=group, y=weight, fill=group)) + geom_boxplot() + scale_fill_brewer(palette="Pastel2") p = theme(legend.position="top") p = theme(legend.position=c(1,0)) p <- ggplot(PlantGrowth, aes(x=group, y=weight, fill=group)) + geom_boxplot() p p+labs(fill="Condicao") p <- ggplot(PlantGrowth, aes(x=group, y=weight, fill=group)) + geom_boxplot() p + scale_fill_discrete(labels=c("Controle", "Tratamento1", "Tratamento2"))