# quick way to specify legend title and labels:
ggplot(data = m111survey, mapping = aes(x = sex)) +
geom_bar(na.rm = T, mapping = aes(fill = weight_feel), position = "dodge") +
scale_fill_discrete(name = "Feeling about\nweight",
labels = c("underweight","about right","overweight"))
## Run this to generate a graph of the element inheritance tree
build_element_graph <- function(tree) {
require(igraph)
require(plyr)
inheritdf <- function(name, item) {
if (length(item$inherit) == 0)
data.frame()
else
data.frame(child = name, parent = item$inherit)
}
edges <- plyr::rbind.fill(mapply(inheritdf, names(tree), tree))
# Explicitly add vertices (since not all are in edge list)
vertices <- data.frame(name = names(tree))
graph.data.frame(edges, vertices = vertices)
}
g <- build_element_graph(ggplot2:::.element_tree)
V(g)$label <- V(g)$name
set.seed(324)
par(mar=c(0,0,0,0)) # Remove unnecessary margins
plot(g, layout=layout.fruchterman.reingold, vertex.size=4, vertex.label.dist=.25)
# experiment with b&w (this doesn't work)
mytheme <- theme(line = element_line(colour = "black"))
ggplot(data = m111survey, mapping = aes(x = fastest, y= GPA)) +
geom_point(mapping = aes(color = sex), na.rm = TRUE) +
geom_smooth(mapping = aes(color = sex), se = F, na.rm = T) +
mytheme
p <- ggplot(data = m111survey, mapping = aes(x = fastest, y= GPA)) +
geom_point(mapping = aes(color = sex), na.rm = TRUE)
p + geom_smooth(mapping = aes(colour = sex), na.rm = T) +
scale_color_grey()
# Hmm, maybe ...
mytheme <- theme_bw() + scale_color_grey(na.value = "black") +
scale_fill_grey(na.value = "black")
ggplot(data = m111survey, mapping = aes(x = sex)) +
geom_bar(na.rm = T, mapping = aes(fill = weight_feel), position = "dodge") +
scale_fill_discrete(name = "Feeling about\nweight",
labels = c("underweight","about right","overweight")) +
mytheme
# nope. So try:
ggplot(data = m111survey, mapping = aes(x = sex)) +
geom_bar(na.rm = T, mapping = aes(fill = weight_feel), position = "dodge") +
scale_fill_discrete(name = "Feeling about\nweight",
labels = c("underweight","about right","overweight")) +
scale_color_grey(na.value = "black") +
scale_fill_grey(na.value = "black") +
theme_bw()
# No, need to keep options from first fill.
ggplot(data = m111survey, mapping = aes(x = sex)) +
geom_bar(na.rm = T, mapping = aes(fill = weight_feel), position = "dodge") +
scale_fill_grey(name = "Feeling about\nweight",
labels = c("underweight","about right","overweight"))
ggplot(data = m111survey, mapping = aes(x = fastest, y= GPA)) +
geom_point(mapping = aes(color = sex), na.rm = TRUE) +
geom_smooth(mapping = aes(color = sex), se = F, na.rm = T) +
scale_colour_grey(name = "Sex") + theme_bw()
## TODO: investigate how to address option to keep unused levels in factor
# variable.
m2 <- subset(m111survey, weight_feel != "3_overweight")
ggplot(data = m2, mapping = aes(x = sex)) +
geom_bar(na.rm = T, mapping = aes(fill = weight_feel), position = "dodge") +
scale_fill_discrete(drop = F)
library(tigerData)
abbN <- function(n, chrVec) {
substr(chrVec, 1, n)
}
levels(shrooms$population) <- abbN(3, levels(shrooms$population))
levels(shrooms$spore.print.color) <- abbN(3, levels(shrooms$spore.print.color))
levels(shrooms$odor) <- abbN(2, levels(shrooms$odor))
ggplot(data = shrooms, mapping = aes(x = odor)) +
geom_bar(na.rm = T, mapping = aes(fill = class), position = "dodge") +
scale_fill_discrete(drop = FALSE) +
facet_grid(population ~ spore.print.color)
library(tree)
tr.mod <- tree(class ~ ., data = shrooms)
summary(tr.mod)
xtabs(~odor+class+spore.print.color+population, data = shrooms)
tr.mod
xtabs(~odor+spore.print.color, data = shrooms)
b1 <- with(shrooms, odor %in% c("ALMOND","ANISE","NONE"))
b2 <- with(shrooms, b1 & spore.print.color %in% c("BLACK","BROWN","PURPLE","WHITE"))
b3 <- with(shrooms, b2 & population %in% c("ABUNDANT","NUMEROUS","SCATTERED","SEVERAL","SOLITARY"))
s2 <- subset(shrooms, b3)
s3 <- subset(s2, population == "SEVERAL")
s4 <- droplevels(s3)
xtabs(~odor+class+spore.print.color+population, data = s4)
preds <- predict(tr.mod, newdata = shrooms[,-1], type = "class")
wrong <- preds != shrooms$class
sm <- shrooms[wrong, ]
sm <- droplevels(sm)
str(sm)
table(shrooms$stalk.root)
length(is.na(shrooms$stalk.root))
ssr <- subset(shrooms, !is.na(stalk.root))
tr.mod2 <- tree(class ~ ., data = ssr)
tr.mod2
preds.sr <- predict(tr.mod2, newdata= ssr, type = "class")
sum(preds.sr != ssr$class)
ssr1 <- subset(shrooms, is.na(stalk.root))
ssr1 <- ssr1[,-12]
tr.mod2 <- tree(class ~ ., data = ssr1)
tr.mod2
preds.sr <- predict(tr.mod2, newdata= ssr, type = "class")
sum(preds.sr != ssr$class)
temp <- as.character(shrooms$stalk.root)
temp[is.na(temp)] <- "unrecorded"
temp <- factor(temp)
s5 <- shrooms
s5$stalk.root <- temp
tr.mod3 <- tree(class ~ .,data = s5)
tr.mod3
summary(tr.mod3)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.