The codes are taken from stackoverflow.
### http://stackoverflow.com/questions/10014187/displaying-text-below-the-plot-generated-by-ggplot2
library(ggplot2)
library(gridExtra)
## Loading required package: grid
# Base plot
df = data.frame(x=seq(1:10), y = seq(1:10))
p = ggplot(data = df, aes(x = x, y = y)) + theme_bw() + geom_point() + ylim(0,10) +
theme(plot.margin = unit(c(1,1,3,1), "cm"))
p
# Create the textGrobs
Text1 = textGrob(paste("Largest x-value is", round(max(df$x), 2), sep = " "))
Text2 = textGrob(paste("Mean = ", mean(df$x), sep = ""))
p1 = p + annotation_custom(grob = Text1, xmin = 4, xmax = 4, ymin = -3, ymax = -3) +
annotation_custom(grob = Text2, xmin = 8, xmax = 8, ymin = -3, ymax = -3)
p1
# Code to override clipping
gt <- ggplot_gtable(ggplot_build(p1))
gt$layout$clip[gt$layout$name=="panel"] <- "off"
grid.draw(gt)
### http://stackoverflow.com/questions/29593938/rotate-legend-in-ggplot2
p <- qplot(mpg, wt, data=mtcars, colour=cyl)
p + theme_bw()+ scale_colour_continuous(guide = guide_legend(direction = "horizontal", title.position = "top",
label.position="bottom", label.hjust = 0.5, label.vjust = 0.5,
label.theme = element_text(angle = 90))) +
theme(legend.position = c(0.5, 0.9))
### http://stackoverflow.com/questions/29587538/mapping-nearest-neighbours-of-a-long-lat-data-set-using-ggmap-geom-point-and-a
library(ggmap)
## Warning: package 'ggmap' was built under R version 3.1.3
library(geosphere)
## Warning: package 'geosphere' was built under R version 3.1.3
## Loading required package: sp
library(dplyr)
##
## Attaching package: 'dplyr'
##
## The following object is masked from 'package:stats':
##
## filter
##
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(ggplot2)
colbj <- structure(list(Name = structure(c(1L, 1L, 2L, 2L, 3L), .Label = c("B",
"D", "L"), class = "factor"), Store = c(1L, 2L, 1L, 2L, 1L),
sqft = c(1200L, 750L, 550L, 600L, 1000L), long = c(116.4579,
116.3811, 116.4417, 116.4022, 116.4333), lat = c(39.93921,
39.93312, 39.88882, 39.90222, 39.911), nn = c(5L, 4L, 5L,
5L, NA)), .Names = c("Name", "Store", "sqft", "long", "lat",
"nn"), class = "data.frame", row.names = c("1", "2", "3", "4",
"5"))
### Arrange the data: set up departure and arrival long/lat
mutate_each(colbj, funs(.[nn]), vars = long:lat) %>%
rename(arr_long = vars1, arr_lat = vars2) %>%
filter(complete.cases(nn)) -> mydf
### Get line information
rts <- gcIntermediate(mydf[,c("long", "lat")],
mydf[,c("arr_long", "arr_lat")],
50,
breakAtDateLine = FALSE,
addStartEnd = TRUE,
sp = TRUE)
### Convert the routes to a data frame for ggplot use
rts <- as(rts, "SpatialLinesDataFrame")
rts.df <- fortify(rts)
### Get a map (borrowing the OP's code)
bjgmap <- get_map(location = c(lon = 116.407395,lat = 39.904211),
zoom = 13, scale = "auto",
maptype = "roadmap",
messaging = FALSE, urlonly = FALSE,
filename = "ggmaptemp", crop = TRUE,
color = "bw",
source = "google", api_key)
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=39.904211,116.407395&zoom=13&size=640x640&scale=2&maptype=roadmap&language=en-EN&sensor=false
# Draw the map
ggmap(bjgmap) +
geom_point(data = colbj,aes(x = long, y = lat, fill = factor(Name)),
size = 10,pch = 21, col = "white") +
geom_path(data = rts.df, aes(x = long, y = lat, group = group),
col = "black")
### http://stackoverflow.com/questions/29578317/r-ggplot2-bar-chart-on-multiple-columns
dat <- read.table(header=TRUE, text="
season sep oct nov dec jan feb
2000 2 7 47 152 259 140
2001 1 5 88 236 251 145
2002 2 14 72 263 331 147
2003 5 6 71 207 290 242")
if (!require("pacman")) install.packages("pacman")
## Loading required package: pacman
pacman::p_load(dplyr, tidyr, ggplot2)
dat %>%
gather(month, value, -season) %>%
ggplot(aes(y = value, x = month)) + theme_bw()+
geom_bar(stat = "identity") +
facet_wrap(~season)