diff --git a/Napoleon-route.png b/Napoleon-route.png new file mode 100644 index 0000000..6d83d0d Binary files /dev/null and b/Napoleon-route.png differ diff --git a/class-activity-3.Rmd b/class-activity-3.Rmd index d8dd1d6..33411b3 100644 --- a/class-activity-3.Rmd +++ b/class-activity-3.Rmd @@ -1,28 +1,37 @@ --- title: "class activity 3" -author: "Charles Lang" -date: "10/2/2018" -output: html_document +author: "Minruo Wang" +date: "10/2/2019" +always_allow_html: yes +output: + html_document: + keep_md: true + toc: true + toc_float: true --- -#Mapping aesthetic to data to = layer +### Learn +#### Useful links +[ggplot2 website](https://ggplot2.tidyverse.org/reference/) +[R Graphic Cookbook](http://www.cookbook-r.com/Graphs/) + +#### Mapping aesthetic to data to = layer ```{r} -install.packages("ggplot2") +# install.packages("ggplot2") library(ggplot2) ggplot(diamonds, aes(x = price, y = carat)) + geom_point() ``` -#Two layers +#### Two layers ```{r} ggplot(mpg, aes(reorder(class, hwy), hwy)) + - geom_jitter() + + geom_jitter() + # adds a small amount of random variation to the location of each point geom_boxplot() ``` ```{r} - #Plot count ggplot(diamonds, aes(depth)) + geom_histogram(aes(y = ..count..), binwidth=0.2) + @@ -37,31 +46,49 @@ ggplot(diamonds, aes(depth)) + ```{r} ggplot(mpg, aes(displ, hwy, color = class)) + geom_point() - ``` - -Can you create a line graph using the "economics_long" data set that shows change over time in "value01" for different categories of "variable"? - + +### Exercise +##### 1. Can you create a line graph using the "economics_long" data set that shows change over time in "value01" for different categories of "variable"? + ```{r} +# show in one graph +ggplot(economics_long, aes(date, value01, color = variable)) + + geom_line() +``` + +```{r} +# show in facet +ggplot(economics_long, aes(x = date, y = value01, color = variable)) + + geom_line() + + facet_wrap(~ variable) +``` +##### 2. If you would like to recreate the Minard graphic of Napoleon's Troops the code is below and the data is in this repo. +```{r} +# load and merge the data +cities <- read.table("cities.txt", header = TRUE, sep = "") +troops <- read.table("troops.txt", header = TRUE, sep = "") +temps <- read.table("temps.txt", header = TRUE, sep = "") +Napo <- merge(cities, troops, by = c("long", "lat"), all = TRUE) +Napoleon <- merge(Napo, temps, by = "long", all = TRUE) ``` -If you would like to recreate the Minard graphic of Napoleon's Troops the code is below and the data is in this repo. ```{r} - -ggplot(cities, aes(long, lat)) + - geom_path(aes(size = survivors, colour = -direction, - group = interaction(group, direction)), data = -troops) + - geom_text(aes(label = city), hjust = 0, vjust = 1, -size = 4) -# Polish appearance -last_plot() + +# Napoleon's troops route +route <- ggplot(Napoleon, aes(long, lat)) + + geom_path(aes(size = survivors, colour = direction, + group = interaction(group, direction)), + data = troops) + + geom_text(aes(label = city), hjust = 0, vjust = 1, size = 4) + scale_x_continuous("", limits = c(24, 39)) + scale_y_continuous("") + scale_colour_manual(values = c("grey50","red")) + - scale_size(to = c(1, 10)) + scale_size(range = c(1, 10)) +# show the graph +route +# save the graph using ggsave +ggsave(route, file = "Napoleon-route.png") ``` diff --git a/class-activity-3.html b/class-activity-3.html new file mode 100644 index 0000000..c877b22 --- /dev/null +++ b/class-activity-3.html @@ -0,0 +1,340 @@ + + + + +
+ + + + + + + + + +# install.packages("ggplot2")
+library(ggplot2)
+
+ggplot(diamonds, aes(x = price, y = carat)) +
+ geom_point()
+ggplot(mpg, aes(reorder(class, hwy), hwy)) +
+ geom_jitter() + # adds a small amount of random variation to the location of each point
+ geom_boxplot()
+#Plot count
+ggplot(diamonds, aes(depth)) +
+ geom_histogram(aes(y = ..count..), binwidth=0.2) +
+ facet_wrap(~ cut) + xlim(50, 70)
+## Warning: Removed 26 rows containing non-finite values (stat_bin).
+## Warning: Removed 10 rows containing missing values (geom_bar).
+#Plot density
+ggplot(diamonds, aes(depth)) +
+ geom_histogram(aes(y = ..density..), binwidth=0.2) +
+ facet_wrap(~ cut) + xlim(50, 70)
+## Warning: Removed 26 rows containing non-finite values (stat_bin).
+
+## Warning: Removed 10 rows containing missing values (geom_bar).
+ggplot(mpg, aes(displ, hwy, color = class)) +
+ geom_point()
+# show in one graph
+ggplot(economics_long, aes(date, value01, color = variable)) +
+ geom_line()
+# show in facet
+ggplot(economics_long, aes(x = date, y = value01, color = variable)) +
+ geom_line() +
+ facet_wrap(~ variable)
+# load and merge the data
+cities <- read.table("cities.txt", header = TRUE, sep = "")
+troops <- read.table("troops.txt", header = TRUE, sep = "")
+temps <- read.table("temps.txt", header = TRUE, sep = "")
+Napo <- merge(cities, troops, by = c("long", "lat"), all = TRUE)
+Napoleon <- merge(Napo, temps, by = "long", all = TRUE)
+# Napoleon's troops route
+route <- ggplot(Napoleon, aes(long, lat)) +
+ geom_path(aes(size = survivors, colour = direction,
+ group = interaction(group, direction)),
+ data = troops) +
+ geom_text(aes(label = city), hjust = 0, vjust = 1, size = 4) +
+ scale_x_continuous("", limits = c(24, 39)) +
+ scale_y_continuous("") +
+ scale_colour_manual(values = c("grey50","red")) +
+ scale_size(range = c(1, 10))
+# show the graph
+route
+## Warning: Removed 43 rows containing missing values (geom_text).
+# save the graph using ggsave
+ggsave(route, file = "Napoleon-route.png")
+## Saving 7 x 5 in image
+## Warning: Removed 43 rows containing missing values (geom_text).
+