library(ggplot2) library(dplyr) library(reshape2)
Explore some of the ggplot2 functions for use in the project
ggplot(iris, aes(x=Petal.Length, y=Petal.Width, color=Species)) + geom_point()
Use aesthetic to make points more transparent on a coutry * contry grid display trade volume as alpha level.
df <- data.frame(x = rnorm(5000), y = rnorm(5000)) ggplot(df, aes(x,y)) + geom_point(alpha = 1/10)
b <- ggplot(economics, aes(x = date, y = unemploy)) j <- b + geom_line() # j # uncomment to display basic plot yrng <- range(economics$unemploy) j <- j + geom_rect(aes(NULL, NULL, xmin = start, xmax = end, fill = party), ymin = yrng[1], ymax = yrng[2], data = presidential) # j library(scales) # to access the alpha function j + scale_fill_manual(values = alpha(c("blue", "red"), .3))
For some countries, the completeness table contained only true values.
WHIch were rendered as red lines, for other countries
true values were rendered as blue lines.
Changed this behaviour by using
manual color palettes for colours scale_colour_manual()
and line palette for lines scale_linetype_manual()
.
# plot completeness rawdata <- tradeflows::sawnwoodexample r <- "France" productcodeinreport <- 4407 completeness <- rawdata %>% filter(productcode == 4407) %>% select(reporter, classification, year, quantity, weight, tradevalue) %>% melt(id=c("reporter", "classification", "year")) %>% mutate(value = !is.na(value)) %>% unique %>% arrange(classification, reporter, year) p <- ggplot(data=filter(completeness, reporter == r), aes(x=year, y=variable, xend=year+1, yend=variable, color=value, linetype=value)) + scale_colour_manual(values = c("TRUE" = "blue", "FALSE" = "red")) + scale_linetype_manual(values = c("TRUE" = 2, "FALSE" = 1)) + #"solid", "dashed" geom_segment(size=3) + facet_grid(classification~.) + ggtitle(paste("Data completeness of", productcodeinreport, "in", r)) plot(p)
ggplot(data.frame(x = seq(1,6)), aes(x = x,y =x,shape=as.factor(x),color=as.factor(x))) + geom_point(size=10)
Seen in Hadley's wiki and in this Stackoverflow question
library(ggplot2) library(grid) # to use unit.c library(gridExtra) grid_arrange_shared_legend <- function(...) { plots <- list(...) g <- ggplotGrob(plots[[1]] + theme(legend.position="bottom"))$grobs legend <- g[[which(sapply(g, function(x) x$name) == "guide-box")]] lheight <- sum(legend$height) grid.arrange( do.call(arrangeGrob, lapply(plots, function(x) x + theme(legend.position="none"))), legend, ncol = 1, heights = unit.c(unit(1, "npc") - lheight, lheight)) } dsamp <- diamonds[sample(nrow(diamonds), 1000), ] p1 <- qplot(carat, price, data=dsamp, colour=clarity) p2 <- qplot(cut, price, data=dsamp, colour=clarity) p3 <- qplot(color, price, data=dsamp, colour=clarity) p4 <- qplot(depth, price, data=dsamp, colour=clarity) grid_arrange_shared_legend(p1, p2, p3, p4)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.