-
Notifications
You must be signed in to change notification settings - Fork 2
/
plot_popular_names.R
156 lines (120 loc) · 6.23 KB
/
plot_popular_names.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
#From https://github.com/jofusa/ssa-baby-names
library(ggplot2)
source("name_functions.R")
###Single Female Name#####
nm<-"Derya" #Enter Female Name
p<- ggplot(names,aes(x=year,y=rank))+
ylim(max(names$rank),min(names$rank)) + # Flip the Y axis
geom_line(data = names[which(names$female == nm),], aes(group=female, colour = female), alpha = 1, size = 1) +
ylab("Sıralama") + xlab("Yıl") + ggtitle(nm) +
theme(legend.position = "none")
p
####Single Male Name####
nm<-c("Ayaz")
p<- ggplot(names,aes(x=year,y=rank)) +
ylim(max(names$rank),min(names$rank)) +
geom_line(data = names[which(names$male == nm),], aes(group=male),colour = "Blue", alpha = 1) +
ylab("Sıralama") + xlab("Yıl") + ggtitle(nm) +
theme(legend.position = "none")
p
####FEMALE#####
p<-ggplot(names,aes(x=Year,y=Rank)) + ylim(max(names$Rank),min(names$Rank)) + geom_line(data = names[which(names$Female %in% nm),], aes(group=Female, colour = Female), alpha = 1, size = 1)+geom_line(aes(group=Female),colour="#431600",alpha=0.1)+ opts(title = "Female Baby Name Popularity Since 1950")+ opts(axis.text.x=theme_text(angle=-70),hjust=0)
p
nm<-"Trinity"
p<-ggplot(names,aes(x=Year,y=Rank))
p<- p + ylim(max(names$Rank),min(names$Rank))
p<- p + geom_line(data = names[which(names$Female %in% nm),], aes(group=Female, colour = Female), alpha = 1, size = 1)
p<- p + opts(title = "People Liked the Matrix Way Too Much")
matrix.label<-data.frame(Year = 1985 , Rank = 220, Text = "Matrix Released - 1999") # create the custom on graphic text label
p <- p + geom_rect(aes(xmin = 1998 , xmax = 2000 , ymin = 1000 , ymax = 1 ),fill = "Green", alpha = .002)
p <- p + geom_text(data = matrix.label, aes(label = Text))
p
rm(p)
###Calculate Greatest Change over Time
#This would be more elequently done with Hadley Wickam's ddply. The lapply/do.call("rbind") combo is brillinatly useful and for simple things I use
name.min.max<-function(nm){
data.frame(
name = nm,
min = min(names[which(names$female == nm),2]),
max = max(names[which(names$female == nm),2]),
dif = max(names[which(names$female == nm),2]) - min(names[which(names$female == nm),2])
)
}
name.list<-unique(names$female)
out<-lapply(X = as.list(name.list), FUN = name.min.max)
out <- do.call("rbind", out)
female.dif<-out[order(-out$dif),]
#Top Female Names with greatest change overtime # Need to seperate the winners and losser just plots largest difference
nm<-as.character(female.dif[1:10,1])
#Plot
p<-ggplot(names,aes(x=year,y=rank))
p <- p + ylim(max(names$rank),min(names$rank))
p <- p + geom_line(data = names[which(names$female %in% nm),], aes(group=female, colour = female), alpha = 1, size = 1)
p <- p + opts(title = "Top Movers")
p <- p + facet_wrap(~female)
p
`Delt.Absolute` <-
function(x1,x2=NULL,k=0,type=c('arithmetic','log'))
{
x1 <- try.xts(x1, error=FALSE)
type <- match.arg(type[1],c('log','arithmetic', 'absolute'))
if(length(x2)!=length(x1) && !is.null(x2)) stop('x1 and x2 must be of same length');
if(is.null(x2)){
x2 <- x1 #copy for same symbol deltas
if(length(k) < 2) {
k <- max(1,k)
}
}
dim(x2) <- NULL # allow for multiple k matrix math to happen
if(type=='log') {
xx <- lapply(k, function(K.) {
log(unclass(x2)/Lag(x1,K.))
})
} else if (type=='absolute') {
xx <- lapply(k, function(K.) {
unclass(x2) - Lag(x1,K.)
})
} else {
xx <- lapply(k, function(K.) {
unclass(x2)/Lag(x1,K.)-1
})
}
xx <- do.call("cbind", xx)
colnames(xx) <- paste("Delt",k,type,sep=".")
reclass(xx,x1)
}
female.names<-names[,c(1,4,2)]
female.names<-female.names[order(female.names$Female, female.names$Year),]
female.names$delta <- Delt.Absolute(female.names$Rank,k=1, type = "absolute")
#female.names$delta3 <- Delt.Absolute(female.names$Rank)
female.names[20:30,]
female.names[c(TRUE, female.names$Female[-1] != female.names$Female[-length(female.names$Female)]), 4] <- NA
female.names.winners<-female.names[order(female.names[4]),]
female.names.losers<-female.names[order(-female.names[4]),]
female.names.winners<-female.names[order(female.names[4]),]
top.female.names<-unique(female.names.winners[1:12,2])
p<-ggplot(names,aes(x=Year,y=Rank)) + ylim(max(names$Rank),min(names$Rank)) + geom_line(data = names[which(names$Female %in% top.female.names),], aes(group=Female, colour = Female), alpha = 1, size = 1)+ opts(title = "Female Baby Name Popularity Since 1950")+ opts(axis.text.x=theme_text(angle=-70),hjust=0) + facet_wrap(~ Female)
p
#Came across this by using the same fast accedency script as above, but using fastest decline. This Is what I found
#Poor, Poor Hillary
nm<-"Hillary"
p<-ggplot(names[which(names$Female %in% nm),],aes(x=Year,y=Rank))
p<- p + ylim(max(names$Rank),min(names$Rank))
p<- p + geom_line(data = names[which(names$Female %in% nm),], aes(group=Female, colour = Female), alpha = 1, size = 1)
p<- p + opts(title = "Poor, Poor Hillary: Popularity of the Female Name 'Hillary' \n But we already that the Clinton Prseidency was better to certain women", size = 30)
matrix.label<-data.frame(Year = 1996 , Rank = 100, Text = "Clinton \n Presidency") # create the custom on graphic text label, kind of a hack
p <- p + geom_rect(aes(xmin = 1992 , xmax = 2001 , ymin = 1000 , ymax = 1 ),fill = "Blue", alpha = .002)
p <- p + geom_rect(aes(xmin = 2007 , xmax = 2008 , ymin = 1000 , ymax = 1 ),fill = "Green", alpha = .002)
matrix.label2<-data.frame(Year = 2007 , Rank = 100, Text = "Hillary's \n Presidential \n Run")
# 1993 ->2001 the years of the Clinton Presdidency but 1992 was when he won the election so that is our starting point
p <- p + geom_text(data = matrix.label, aes(label = Text, size =20))
p <- p + geom_text(data = matrix.label2, aes(label = Text, size = 20))
print(p)
###Loop through all Male names ### Might give you a Seizure
name.list<-unique(names$Male)
for (i in 1:length(name.list)) {
print(name.list[i])
p<-ggplot(names,aes(x=Year,y=Rank)) + ylim(max(names$Rank),min(names$Rank)) + geom_line(data = names[which(names$Male == name.list[i]),], aes(group=Male),colour = "Blue", alpha = 1)+geom_line(aes(group=Male),colour="#431600",alpha=0.2)+ opts(title = name.list[i])+ opts(axis.text.x=theme_text(angle=-70),hjust=0)
print(p)
Sys.sleep(.2)
}