Skip to content

Commit c1518fd

Browse files
Chris R. AlbonChris R. Albon
Chris R. Albon
authored and
Chris R. Albon
committed
New set of snippits.
1 parent 4ef5291 commit c1518fd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1124
-0
lines changed

.Rhistory

+512
Large diffs are not rendered by default.

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.Rproj.user
2+
.Rhistory
3+
.RData

2d-density-plot.r

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# 2D Density Plot
2+
# Original source: r graphics cookbook
3+
4+
# load the gcookbook package for the data
5+
library(gcookbook)
6+
7+
# load the ggplot2 package
8+
library(ggplot2)
9+
10+
# reset the graphing device
11+
dev.off()
12+
13+
# create the ggplot2 data
14+
p <- ggplot(faithful, aes(x = eruptions, y = waiting)) +
15+
# add a layer with the points
16+
geom_point() +
17+
# and a layer for the density heatmap with the alpha and the color determined by density (the .. refers to the fact that density is a variable that was created inside the ggplot() function)
18+
stat_density2d(aes(alpha=..density.., fill=..density..), geom="tile", contour=FALSE); p
File renamed without changes.
File renamed without changes.

annotating-plots.r

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# 2D Density Plot
2+
# Original source: r graphics cookbook
3+
4+
# load the gcookbook package for the data
5+
library(gcookbook)
6+
7+
# load the ggplot2 package
8+
library(ggplot2)
9+
10+
# reset the graphing device
11+
dev.off()
12+
13+
# create the ggplot2 data
14+
p <- ggplot(faithful, aes(x = eruptions, y = waiting))
15+
16+
# Add Text
17+
18+
# create the ggplot2 plot
19+
p + geom_point() +
20+
# add text
21+
annotate("text", x = 3, y = 48, label="Group 1", family="serif", fontface="italic", colour="darkred", size=6) +
22+
# add more text
23+
annotate("text", x = 4.5, y = 66, label="Group 2", family="serif", fontface="italic", colour="darkred", size=6)
24+
25+
# Add Mathematical Expressions
26+
27+
# create the ggplot2 plot
28+
p + geom_point() +
29+
# add the formula, parse=TRUE turns the next into a formula
30+
annotate("text", x = 4.5, y = 66, parse = TRUE, label = "frac(1, sqrt(2 * pi)) * e ^ {-x^2 / 2}")
31+
32+
# Add Lines
33+
34+
# load the grid package to create the flat ends of the line seqment and arrow
35+
library(grid)
36+
37+
# create the ggplot2 plot
38+
p + geom_point() +
39+
# add a horizontal line at y = 66
40+
geom_hline(yintercept = 66) +
41+
# add a vertical line at 3 = 3
42+
geom_vline(xintercept = 3) +
43+
# add an angled line
44+
geom_abline(intercept = 37.4, slope = 9) +
45+
# add a line segment
46+
annotate("segment", x = 1, xend = 2.5, y = 75, yend = 75, arrow=arrow(ends="both", angle=90, length=unit(.2,"cm"))) +
47+
# add an arrow
48+
annotate("segment", x = 4, xend = 5, y = 60, yend = 55, colour="blue", size=2, arrow=arrow())
49+
50+
# Add A Shaded Rectangle
51+
52+
# create the ggplot2 plot
53+
p + geom_point() +
54+
# add a shaped blue rectangle
55+
annotate("rect", xmin=1, xmax=3, ymin=40, ymax=100, alpha=.1, fill="blue")
File renamed without changes.

balloon-plot.r

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Balloon Plot
2+
# Original source: r graphics cookbook
3+
4+
# load the gcookbook package for the data
5+
library(gcookbook)
6+
7+
# load the ggplot2 package
8+
library(ggplot2)
9+
10+
# reset the graphing device
11+
dev.off()
12+
13+
# create a subset of countries for year 2009 and only for a vector of countries names
14+
cdat <- subset(countries, Year==2009 & Name %in% c("Canada", "Ireland", "United Kingdom", "United States",
15+
"New Zealand", "Iceland", "Japan", "Luxembourg", "Netherlands", "Switzerland"))
16+
17+
# create the ggplot data with the size of the balloons determined by GDP
18+
p <- ggplot(cdat, aes(x = healthexp, y = infmortality, size = GDP)) +
19+
# select the select of the points and their colors
20+
geom_point(shape=21, colour="#3333ff", fill="#0066CC") +
21+
# change the scale of the point size to make them bigger
22+
scale_size_area(max_size=15); p

change-barplot-bar-width.r

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Change The Barplot Bar Width# load the gcookbook package for the datalibrary(gcookbook)# load the ggplot2 packagelibrary(ggplot2)# plot a default barplotggplot(pg_mean, aes(x=group, y=weight)) + geom_bar(stat="identity")# plot a barplot with skinny barsggplot(pg_mean, aes(x=group, y=weight)) + geom_bar(stat="identity", width=0.5)# plot a barplot with thick barsggplot(pg_mean, aes(x=group, y=weight)) + geom_bar(stat="identity", width=1)
File renamed without changes.
File renamed without changes.

code_r.Rproj

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Version: 1.0
2+
3+
RestoreWorkspace: Default
4+
SaveWorkspace: Default
5+
AlwaysSaveHistory: Default
6+
7+
EnableCodeIndexing: Yes
8+
UseSpacesForTab: Yes
9+
NumSpacesForTab: 2
10+
Encoding: UTF-8
11+
12+
RnwWeave: Sweave
13+
LaTeX: pdfLaTeX
File renamed without changes.

density-plot.r

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Density Curve Plot
2+
# Original source: r graphics cookbook
3+
4+
# load the gcookbook package for the data
5+
library(gcookbook)
6+
7+
# load the ggplot2 package
8+
library(ggplot2)
9+
10+
# reset the graphing device
11+
dev.off()
12+
13+
# create the ggplot2 data for the faithful$waiting variable
14+
ggplot(faithful, aes(x=waiting)) +
15+
# add a sensity line
16+
geom_line(stat="density") +
17+
# zoom out the plot a little bit
18+
expand_limits(y=0)
19+
20+
# create the ggplot2 data for three lines of different levels of smoothing
21+
ggplot(faithful, aes(x=waiting)) +
22+
# "adjust" determines the level of smoothing, larger the number, the more smoothing
23+
geom_line(stat="density", adjust=.25, colour="red") +
24+
geom_line(stat="density") +
25+
geom_line(stat="density", adjust=2, colour="blue")
File renamed without changes.

dot-plot.r

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Dot Plot (Wilkinson Plot)
2+
# Original source: r graphics cookbook
3+
4+
# load the gcookbook package for the data
5+
library(gcookbook)
6+
7+
# load the ggplot2 package
8+
library(ggplot2)
9+
10+
# reset the graphing device
11+
dev.off()
12+
13+
# create a subset of countries
14+
countries2009 <- subset(countries, Year==2009 & healthexp>2000)
15+
16+
# create the ggplot2 data
17+
p <- ggplot(countries2009, aes(x= infmortality))
18+
19+
# create the dotplot layer
20+
p + geom_dotplot(binwidth=.25) +
21+
# rescale the y axis and remove tic marks
22+
scale_y_continuous(breaks=NULL) +
23+
# remove the axis labels
24+
theme(axis.title.y=element_blank())

frequency-polygon-plot.r

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Frequency Polygon Plot
2+
# Original source: r graphics cookbook
3+
4+
# load the gcookbook package for the data
5+
library(gcookbook)
6+
7+
# load the ggplot2 package
8+
library(ggplot2)
9+
10+
# reset the graphing device
11+
dev.off()
12+
13+
# divide the faithful$waiting data into 10 bins
14+
binsize <- diff(range(faithful$waiting))/10
15+
16+
# create the ggplot2 data for the faithful$waiting variable
17+
ggplot(faithful, aes(x=waiting)) +
18+
# add a sensity line
19+
geom_freqpoly(binwidth=binsize) +
20+
# zoom out the plot a little bit
21+
expand_limits(y=0)
File renamed without changes.
File renamed without changes.
File renamed without changes.

ggplot2/.Rapp.history

Whitespace-only changes.
File renamed without changes.

incomplete/earthquake-plot.r

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# ACLED Exploration
2+
3+
library(foreign)
4+
5+
df <- read.csv("earthquake.csv", header = TRUE, stringsAsFactors = FALSE)
6+
7+
# load the ggplot2 package
8+
library(ggplot2)
9+
10+
# load the ggmap package
11+
library(ggmap)
12+
13+
# get the map from google maps, centered on the median long/lat.
14+
mapImageData <- get_googlemap(
15+
center = c(lon = median(df$Longitude), lat = median(df$Latitude)),
16+
zoom = 4,
17+
maptype = c("terrain")
18+
)
19+
20+
# plot the points on the map in red (the aes has some problems inheriting from prevois models, so that is why we've FALSE'd the inherit)
21+
ggmap(mapImageData, extent = "device") +
22+
geom_point(inherit.aes = FALSE, aes(x = df$Longitude, y = df$Latitude),
23+
data = df,
24+
color = "red",
25+
size = df$Magnitude,
26+
pch = 20)

incomplete/venn-scatterplot.r

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Venn-Scatterplot
2+
# Original source: http://stackoverflow.com/questions/2397097/how-can-a-data-ellipse-be-superimposed-on-a-ggplot2-scatterplot
3+
4+
# create a dataframe of simulated data
5+
x <- c(2,3,4, 10,12,10, 20,21,23)
6+
y <- c(50,54,49, 30,25,26, 5,6,5)
7+
group <- c("a", "a", "a", "b", "b", "b", "c", "c", "c")
8+
df <- data.frame(x,y, group)
9+
rm(x, y, group)
10+
11+
# load ggplot2
12+
library(ggplot2)
13+
library(devtools)
14+
source_url("https://raw.github.com/JoFrhwld/FAAV/master/r/stat-ellipse.R")
15+
16+
#calculating ellipses
17+
library(ellipse)
18+
df_ell <- data.frame()
19+
for(g in levels(df$group)){
20+
df_ell <- rbind(df_ell, cbind(as.data.frame(with(df[df$group==g,], ellipse(cor(x, y),
21+
scale=c(sd(x),sd(y)),
22+
centre=c(mean(x),mean(y))))),group=g))
23+
}
24+
#drawing
25+
library(ggplot2)
26+
ggplot(data=df, aes(x=x, y=y,colour=group)) +
27+
geom_point(size=1.5, alpha=.6) +
28+
geom_path(data=df_ell, aes(x=x, y=y,colour=group), size=1, linetype=2)

incomplete/verification-function-1.r

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# ACLED Exploration
2+
3+
library(foreign)
4+
5+
df <- read.csv("Kenya.csv", header = TRUE, stringsAsFactors = FALSE)
6+
7+
# load the ggplot2 package
8+
library(ggplot2)
9+
10+
# load the ggmap package
11+
library(ggmap)
12+
13+
# get the map from google maps, centered on the median long/lat.
14+
mapImageData <- get_googlemap(
15+
center = c(lon = median(df$LONGITUDE), lat = median(df$LATITUDE)),
16+
zoom = 7,
17+
maptype = c("terrain")
18+
)
19+
20+
# plot the points on the map in red (the aes has some problems inheriting from prevois models, so that is why we've FALSE'd the inherit)
21+
p <- ggmap(mapImageData, extent = "device") +
22+
geom_point(inherit.aes = FALSE, aes(x = df$LONGITUDE, y = df$LATITUDE, colour = df$EVENT_TYPE),
23+
data = df,
24+
size = 4,
25+
pch = 20)

istanbul.png

751 KB
Loading

istanbul.png.rda

290 Bytes
Binary file not shown.

line-plot-with-multiple-lines.r

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Line Plot With Multiple Lines
2+
# Original source: r graphics cookbook
3+
4+
# load the gcookbook package for the data
5+
library(gcookbook)
6+
7+
# load the ggplot2 package
8+
library(ggplot2)
9+
10+
# load plyr package
11+
library(plyr)
12+
13+
# reset the graphing device
14+
dev.off()
15+
16+
# summarize the ToothGrowth data
17+
tg <- ddply(ToothGrowth, c("supp", "dose"), summarise, length=mean(len))
18+
19+
# create a ggplot with lines colored by the tg$supp variable
20+
ggplot(tg, aes(x=dose, y=length, colour=supp)) +
21+
geom_line()
22+
23+
# create a ggplot with line-types determined by the tg$supp variable
24+
ggplot(tg, aes(x=dose, y=length, linetype=supp)) +
25+
geom_line()

line-plot-with-points.r

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Plot With Both Lines And Poiints
2+
3+
# load the gcookbook package for the data
4+
library(gcookbook)
5+
6+
# load the ggplot2 package
7+
library(ggplot2)
8+
9+
# reset the graphing device
10+
dev.off()
11+
12+
# create a plot with both lines and dots
13+
ggplot(BOD, aes(x=Time, y=demand)) +
14+
geom_line() +
15+
geom_point()
16+

ggplot2/line-plot.r line-plot.r

File renamed without changes.

map_gezi_europe.png

95.7 KB
Loading

map_gezi_international.png

100 KB
Loading

map_gezi_istanbul.png

448 KB
Loading

map_gezi_turkey.png

75.5 KB
Loading
File renamed without changes.
File renamed without changes.

rugplot.r

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Rug Plot
2+
# Original source: r graphics cookbook
3+
4+
# load the gcookbook package for the data
5+
library(gcookbook)
6+
7+
# load the ggplot2 package
8+
library(ggplot2)
9+
10+
# reset the graphing device
11+
dev.off()
12+
13+
# create the scatterplot
14+
sps <- ggplot(heightweight, aes(x = ageYear, y = heightIn, colour = sex)) +
15+
geom_point() +
16+
scale_colour_brewer(palette = "Set1")
17+
18+
# add the riug plot
19+
sps + geom_rug(position="jitter", size=.2)

scatterplot-with-annotation.r

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Rug Plot
2+
# Original source: r graphics cookbook
3+
4+
# load the gcookbook package for the data
5+
library(gcookbook)
6+
7+
# load the ggplot2 package
8+
library(ggplot2)
9+
10+
# reset the graphing device
11+
dev.off()
12+
13+
# create the scatterplot
14+
sps <- ggplot(heightweight, aes(x = ageYear, y = heightIn, colour = sex)) +
15+
geom_point() +
16+
scale_colour_brewer(palette = "Set1")
17+
18+
# create a text annotation layer at location x=16.5 and y=52
19+
sps + annotate("text", label="r-score: .02", parse = TRUE, x=16.5, y=52)

0 commit comments

Comments
 (0)