Este relatório1 apresenta alguns exemplos de gráficos básicos que podem ser feitos no R. Há muito material disponível na internet! Pesquise! Um bom site para retirar dúvidas é o http://stackoverflow.com/ ou http://pt.stackoverflow.com/

Utilize o help do R para entender um pouco mais sobre os parâmetros de cada gráfico (Ex: ? barplot)

Pakages necessários para rodar os exemplos: install.packages(“ggplot2”); install.packages(“tidyr”); install.packages(“HH”); install.packages(“micromapST”)

A pasta data que contém os arquivos acs2014.csv, countries2012.csv, fathers.txt e living.csv deve ser copiada para seu diretório de trabalho do R, pois serão usados em alguns gráficos como base de dados. Crie uma pasta data em seu diretório de trabalho e copie os arquivos que está disponível em: https://github.com/nbrgraphs/mro/tree/master/data

Bar charts (base)

TFR <- c(2.6,1.9,2.0,3.3,2.5,2.3,2.5)
names(TFR) <- c("Belize","Costa Rica","El Salvador",
                "Guatemala","Honduras","Nicaragua","Panama")
TFR <- sort(TFR)
par(mar = c(5,8,4,2))
barplot (TFR, horiz = TRUE, col = "lightblue", border = "lightblue",
         main = "Central America, Fertility Rate 2012",
         xlab = "average births per woman", xlim = c(0,4),cex.lab = 1.4,
         cex.main = 1.7, cex.names = 1.4, las = 1) 
abline(v = 1:4, col = "grey90")

Histograms (base)

x <- read.csv("data/countries2012.csv")
hist(x$TFR, breaks = seq(from = 1, to = 8), col = "lightblue",
main = "Fertility Rate by Country",
xlab = "average births per woman",
xlim = c(1,8), ylim = c(0,80),
cex.main = 1.7, cex.lab = 1.4, las = 1)

x <- read.csv("data/countries2012.csv")
hist(x $TFR, breaks = seq(from = 1, to = 8, by=.5), col = "lightblue",
main = "Fertility Rate by Country",
xlab = "average births per woman",
xlim = c(1,8), ylim = c(0,50),
cex.main = 1.7, cex.lab = 1.4, las = 1)

Box plots (base)

par(mar = c(5,9,4,2))
data <- read.csv("data/countries2012.csv")
data$CONTINENT <- reorder(data$CONTINENT, data$TFR, median)
boxplot( TFR ~ CONTINENT, data, horizontal = TRUE,
ylim = c(1,8),
col = "lightblue",
main = "Fertility Rate Distributions by Continent",
xlab = "average births per woman", cex.main = 1.7,
cex.lab = 1.4, cex.axis = 1.4, las = 1)
abline (v = 1:8, col = "grey95")
abline (v = 2, col = "red")
text ( x = 2, y = .5, "<- replacement rate", col = "red",
pos = 4)

Line charts (base)

df <- data.frame ( year = seq(2005,2015),
pop = c(296, 298, 301, 304, 307, 308,
311, 313, 315, 318, 320))
plot( df$year, df$pop, type = "l",
main = "U.S. Population, 2005 - 2015", xlab = "", ylab = "millions of people",
        las = 1, cex.axis = 1.2, cex.lab = 1.4, cex.main = 1.7)

plot(UKDriverDeaths,
main = "UK Road Casualities 1969-1984",
ylab = "monthly driver deaths", xlab = "", las = 1,
cex.main = 1.4, cex.lab = 1.2, cex.axis = 1.2)

Month plots (base)

par(mar = c(5,6,2,2))
monthplot( UKDriverDeaths,
main = "UK Road Casualities 1969-1984",
labels = month.abb, las = 1, ylab = "",
cex.axis = 1.2, cex.main = 1.4)
mtext("driver deaths", side = 2, line = 4, cex = 1.3)

Scatterplots (base)

data <- read.csv("data/countries2012.csv")
plot(data$GDP/1000,data$TFR, xpd = TRUE, bty = "l",
main = "Total Fertility Rate vs. GDP, 2012",
xlab = "GDP per capita (in 1000s $US)",
ylab = "average births per woman",
ylim = c(1,8), cex.main = 1.5,
cex.lab = 1.3, cex.axis = 1.1,
col = "cornflowerblue", las = 1)

# Scatterplot with continents labeled by color
data <- read.csv("data/countries2012.csv")
colors6 <- c( "orange","cyan","blue","green",
"black", "red")
plot( data$GDP/1000,data$TFR, bty = "l",
col = colors6[data$CONTINENT],
xlab = "GDP per capita (in 1000s $US)",
ylab = "average births per woman",
main = "Total Fertility Rate vs. GDP, 2012",
las = 1, ylim = c(1,8), cex.main = 1.5, cex.lab = 1.3,
cex.axis = 1.1)
legend( "topright", pch = 21, legend = levels(data$CONTINENT),
col = colors6, bty = "n")

Pie charts (base)

data <- read.table ("data/fathers.txt")
colors4 <- c( "lightblue", "skyblue3", "rosybrown1",
"rosybrown3")
pie(data[,"NOHSDEG"], labels = data$AGE, col = colors4,
cex = 1.5)
mtext(paste( "Fathers without High School Degrees:\n",
"Age at First Child"), side = 3,
cex = 1.7, font = 2)

Divided bar charts (base)

par(mar = c(5, 8, 4, 2))
fathers <- read.table("data/fathers.txt")
columns <- c("COLLDEG", "SOMECOLL", "HSDEG","NOHSDEG")
data <- as.matrix(subset(fathers, select = columns))
colors4 <- c( "lightblue", "skyblue3", "rosybrown1",
"rosybrown3")
edugroups <- c( "College degree", "Some college",
"H.S. degree", "No H.S. degree")
barplot ( data, names.arg = edugroups, horiz = TRUE,
col = colors4, border = colors4, xlab = "percent",
ylim = c(0,5.5), cex.axis = 1.3, cex.names = 1.2,
cex.lab = 1.3, las = 1)
legend ( "top", legend = fathers$AGE, fill = colors4,
border = colors4, bty = "n", cex = 1.1, horiz = TRUE)
title ("Father’s Age at First Child, by Education", cex.main = 1.6)

Grouped bar charts (base)

fathers <- read.table("data/fathers.txt")
columns <- c("NOHSDEG", "HSDEG", "SOMECOLL", "COLLDEG")
data <- as.matrix(subset(fathers, select = columns))
colors4 <- c( "lightblue", "skyblue3", "rosybrown1",
"rosybrown3")
edugroups <- c( "No H.S. degree", "H.S. degree",
"Some college", "College degree")
barplot( data, names.arg = edugroups, beside = TRUE,
col = colors4, border = colors4, ylab = "percent",
ylim = c(0,58), cex.axis = 1.3, cex.names = 1.1,
cex.lab = 1.3, las = 1)
legend( "top", fill = colors4, border = colors4, bty = "n",
legend = fathers$AGE, cex = 1.2, horiz = TRUE)
title ( "Father’s Age at First Child, by Education",
cex.main = 1.6)

Nos exemplos a seguir empregamos o pacote ggplot que coloca em prática a gramática dos gráficos. O usuário deve ter alguma experiência com o R para empregar este pacote. Veja Wickham, Hadley. 2016. ggplot2: Elegant Graphics for Data Analysis. 2nd ed. Springer.

Faceted bar charts (ggplot2)

library(ggplot2)
library(tidyr)
fathers <- read.table("data/fathers.txt")
data <- gather( fathers, key = EDUCATION, value = PERCENT,-AGE)
levels(data$EDUCATION) <- c( "No H.S. degree", "H.S. degree",
                             "Some college", "College degree")
g <- ggplot(data, aes(x = AGE, y = PERCENT))
g + geom_bar(stat = "identity", fill = "lightblue") +
  coord_flip(expand = FALSE) +
  facet_grid(.~EDUCATION) + theme_bw(16) +
  theme(axis.line = element_blank(),
        axis.ticks.length = unit(0, "cm"),
        panel.grid.major.y = element_blank(),
        panel.grid.minor = element_blank(),
        strip.background = element_rect(fill="grey90"),
        strip.text.x =
          element_text(margin = margin(t = 5, b = 5)),
        plot.title = element_text(face = "bold")) +
  ggtitle("Father’s Age at First Child, by Education") +
  xlab(NULL) + ylab("percent")

Escalas tipo likert (HH)

library(ggplot2)
data <- read.csv("data/living.csv")
colors2 <- c("lightblue","skyblue3")
data$LIVING <- factor( data$LIVING, levels=c( "Other group quarters",
                                              "Nursing home",
                                              "Alone in household",
                                              "With others in household"))
twolinelabels <- c( "Other group\nquarters",
                    "Nursing home",
                    "Alone in\nhousehold",
                    "With others\nin household")
data$AGE <- factor(data$AGE, levels = c( "70-79 yrs",
                                         "80-89 yrs",
                                         "90-99 yrs",
                                         "100+ yrs"))
g <- ggplot(data, aes( x = LIVING, y = PERCENT,fill = SEX))

g + geom_bar(stat = "identity", position = "dodge") +
  scale_x_discrete(labels = twolinelabels) +
  scale_fill_manual( values = colors2,
                     limits = c("Male","Female")) +
  scale_y_continuous(breaks = c(0,30,60)) +
  coord_flip() + facet_grid(. ~ AGE) +
  theme_bw(16) +
  theme(axis.ticks.length = unit(0, "cm"),
        legend.title = element_blank(),
        legend.position = c(.9,-.2),
        legend.key = element_blank(),
        legend.key.size = unit(.8, "lines"),
        panel.grid.major.y = element_blank(),
        panel.grid.minor = element_blank(),
        strip.background = element_rect(fill = "grey90"),
        strip.text.x = element_text(margin = margin(t = 5, b = 5)),
        plot.title = element_text( hjust = 0, size = 16,face = "bold"), 
        plot.margin = margin(8,8,24,8)) +
  xlab(NULL) + ylab("percent") +
  ggtitle ("Living Arrangements of Older Age-Sex Groups")

o Gráfico a seguir é muito útil para ilustrar dados de escalas atitudinais, como por exemplo a escala de likert. Também é util para representar dados categóricos. É necessário o pacote HH

Um pouco do que o R é capaz de criar! “Não pergunte se o R é capaz de fazer, pergunte como se faz!”

library(micromapST)
data <- read.csv("data/acs2014.csv", row.names = 1)
data <- data[order(data$Income),]
panelDesc <- data.frame(
type = c("map", "id", "dot", "dot"),
lab1 = c("", "", "Median Age", "Median Income"),
lab3 = c("", "", "", "in 2014 inflation-adjusted $"),
col1 = c(NA, NA, "Age", "Income"))
micromapST(data,panelDesc, rowNames = "full", sortVar = "Age",
ascend = FALSE,
title = paste( "2010-2014 American Community",
"Survey: State Age and Income"),
details = list(Title.cex = 1.4))


  1. documento baseado no material disponível em https://github.com/nbrgraphs/mro