-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChart Collection.R
175 lines (130 loc) · 4.34 KB
/
Chart Collection.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# Plots
# Graficos no R - BarPlot, Pie Chart, Line Chart, Scatter Plot, Histograma e Treemap
# http://www.r-graph-gallery.com/
# Definindo a pasta de trabalho
# Substitua o caminho abaixo pela pasta no seu computador
setwd("D:/Dropbox/DSA/PowerBI-DataScience/Cap11/06-Plots")
getwd()
graphics.off()
par("mar")
par(mar=c(1,1,1,1))
# Dados
my_vector = c(3,12,5,18,45)
names(my_vector) = c("A","B","C","D","E")
my_vector
# Barplot
barplot(my_vector)
barplot(my_vector, col = c(1,2,3,4,5) )
png("barplot.png" , width = 480, height = 480 )
barplot(my_vector, col = rgb(0.5,0.1,0.6,0.6), xlab = "Categorias", ylab = "Valores", main = "Barplot em R" , ylim = c(0,60) )
dev.off()
# Ggplot2
library(ggplot2)
head(mtcars)
# Barplot
ggplot(mtcars, aes(x=as.factor(cyl) )) +
geom_bar()
ggplot(mtcars, aes(x=as.factor(cyl), fill=as.factor(cyl) )) +
geom_bar( ) +
scale_fill_manual(values = c("red", "green", "blue") )
# Criando dados fake
data = data.frame(group = c("A ","B ","C ","D ") , value=c(33,62,56,67) )
# Barplot
ggplot(data, aes(x = group, y = value ,fill = group )) +
geom_bar(width = 0.85, stat="identity")
# Pie Chart
slices <- c(10, 12,4, 16, 8)
lbls <- c("US", "UK", "Australia", "Germany", "France")
pie(slices, labels = lbls, main = "Beer per Country")
# Pie Chart com percentuais
slices <- c(10, 12, 4, 16, 8)
lbls <- c("US", "UK", "Australia", "Germany", "France")
pct <- round(slices/sum(slices)*100)
lbls <- paste(lbls, pct)
lbls <- paste(lbls,"%",sep="")
pie(slices,labels = lbls, col=rainbow(length(lbls)),
main="Beer per Country")
# Pie Chart 3D
install.packages("plotrix")
library(plotrix)
slices <- c(10, 12, 4, 16, 8)
lbls <- c("US", "UK", "Australia", "Germany", "France")
pie3D(slices,labels=lbls,explode=0.1,
main="Beer per Country")
# Line
# Dados
cars <- c(1, 3, 6, 4, 9)
trucks <- c(2, 5, 4, 5, 12)
# Plot
plot(cars, type="o", col="blue", ylim=c(0,12))
lines(trucks, type="o", pch=22, lty=2, col="red")
title(main="Autos", col.main="red", font.main=4)
# Boxplot
library(ggplot2)
head(mpg)
# Plot
ggplot(mpg, aes(x=reorder(class, hwy), y=hwy, fill=class)) +
geom_boxplot() +
xlab("class") +
theme(legend.position="none")
# Scatter Plot
library(ggplot2)
data = data.frame(cond = rep(c("condition_1", "condition_2"), each=10),
my_x = 1:100 + rnorm(100,sd=9), my_y = 1:100 + rnorm(100,sd=16) )
ggplot(data, aes(x=my_x, y=my_y)) +
geom_point(shape=1)
# Adiciona linha de regressao
ggplot(data, aes(x=my_x, y=my_y)) +
geom_point(shape=1) +
geom_smooth(method = lm , color="red", se=FALSE)
# Adiciona smooth
ggplot(data, aes(x=my_x, y=my_y)) +
geom_point(shape=1) +
geom_smooth(method=lm , color="red", se=TRUE)
# Treemap
install.packages("treemap")
library(treemap)
# Dados
group=c(rep("group-1",4),rep("group-2",2),rep("group-3",3))
subgroup=paste("subgroup" , c(1,2,3,4,1,2,1,2,3), sep="-")
value=c(13,5,22,12,11,7,3,1,23)
data=data.frame(group,subgroup,value)
# Labels
treemap(data, index=c("group","subgroup"),
vSize="value", type="index",
fontsize.labels=c(15,12),
fontcolor.labels=c("white","orange"),
fontface.labels=c(2,1),
bg.labels=c("transparent"),
align.labels=list(
c("center", "center"),
c("right", "bottom")),
overlap.labels=0.5,
inflate.labels=F,
)
# Customizando
treemap(data, index=c("group","subgroup"), vSize="value", type="index",
border.col=c("black","white"),
border.lwds=c(7,2)
)
# Histograma
x <- mtcars$mpg
h <- hist(x, breaks = 10, col="red", xlab = "Miles Per Gallon",
main = "Histograma com Curva de Distribuicao")
xfit <- seq(min(x),max(x),length=40)
yfit <- dnorm(xfit,mean=mean(x),sd=sd(x))
yfit <- yfit*diff(h$mids[1:2])*length(x)
lines(xfit, yfit, col="blue", lwd=2)
# Usando o ggplot2
library(ggplot2)
# dataset
data = data.frame(value = rnorm(10000))
# Custom Binning. I can just give the size of the bin
ggplot(data, aes(x=value)) +
geom_histogram(binwidth = 0.05)
# Uniform color
ggplot(data, aes(x=value)) +
geom_histogram(binwidth = 0.2, color="white", fill=rgb(0.2,0.7,0.1,0.4) )
# Proportional color
ggplot(data, aes(x=value)) +
geom_histogram(binwidth = 0.2, aes(fill = ..count..) )