Showing posts with label R. Show all posts
Showing posts with label R. Show all posts

November 13, 2022

dplyr package

R dplyr Package

library(dplyr)

glimpse(df)
glimpse(mutate(mammals, adult_body_mass_kg = adult_body_mass_g / 1000))

mutate() adds new variables that are functions of existing variables.
select() picks variables based on their names.
filter() picks cases based on their values.
summarise() reduces multiple values down to a single summary.
arrange() changes the ordering of the rows.

filter function in dplyr package in R:
filter(schtype == "public")
temp <- filter(temp, instrument == "Guitar")
email50_big <- email50 %>% filter(number == "big")
votes %>% filter(vote <=3)
filter(newdata_long, day == "weekday_is_monday", channel == "data_channel_is_lifestyle")
filter(days, weekday == "weekday_is_saturday" | weekday == "weekday_is_sunday" )
filter(starwars, hair_color == "none" & eye_color == "black")

dplyr package mutate function in R:
newdata_long <- mutate(newdata_long, rate_positivity = rate_positive_words/rate_negative_words, gain = n_tokens_title - n_tokens_content)
email50 <- email50 %>%
      mutate(num_char_cat = ifelse(num_char < med_num_char, "below median", "at or above median"))
votes %>% mutate(year=session+1945)
votes_processed <- votes %>%
  filter(vote <= 3) %>%
  mutate(year = session + 1945,
         country = countrycode(ccode, "cown", "country.name"))
msleep %>%
    mutate(rem_proportion = sleep_rem / sleep_total, bodywt_grams = bodywt * 1000) %>%
    head

join functions in dplyr package in R:
left_join(names, plays, by="name")
left_join(names2, plays2, by=c("name","surname"))
right_join(names, plays, by="name")
inner_join(names, plays, by="name")
full_join(names, plays, by="name")

dplyr package select function in R:
select(temp, first, last, band)
select(mammals, adult_head_body_len_mm, litter_size)
select(mammals, -adult_head_body_len_mm)
select(mammals, contains("body"))
select(mammals, starts_with("adult"))
select(mammals, ends_with("g"))
select(mammals, 1:3)
car_price <- car_price %>% dplyr::select(car_ID, car_Company, everything())
df %>% select(-b, -c, everything())
select_if(car_price[,-1], is.numeric)
select_all(mtcars, funs(toupper(.)))

select_at(.tbl, .vars, .funs = list(), ...)

select(newdata_long, day, channel, shares)
head(select(msleep, -name))
head(select(msleep, starts_with("sl")))
artists %>%
  full_join(bands, by = c("first", "last"))
bands %>%
  left_join(artists, by = c("first", "last")) %>%
  filter(instrument == "Guitar") %>%
  select(first, last, band)
starwars %>%
  select(name, ends_with("color"))
distinct(newdata_long, channel)
day_groups<- group_by(data, day)

summarise function in dplyr package:
summarise(data,  share.avg = mean(shares))
summarise(newdata_long, shares_avg = mean(shares, na.rm = T))
goal2 <-
  artists %>%
    full_join(bands,by=c("first","last")) %>%
      inner_join(songs,by=c("first","last"))
votes_processed %>%
  group_by(year) %>%
  summarise(total=n(), percent_yes=mean(vote==1))
summarise(mammals, mean_mass = mean(adult_body_mass_g, na.rm = TRUE))

starwars %>%
  group_by(species) %>%
  summarise(
    n = n(),
    mass = mean(mass, na.rm = TRUE)
  ) %>%
  filter(n > 1)

mframe %>%
  select(funding_type, raised_amount) %>%
  filter(funding_type %in% c("vente","angel","prv_equi")) %>%
  group_by(funding_type) %>%
  summarise(
    avg = mean(raised_amount, na.rm = TRUE)
  )


by_country %>% arrange(percent_yes)
newdata_arranged <- arrange(newdata_long, day, channel)
by_country %>% arrange(desc(percent_yes))
newdata_arranged <- arrange(newdata_long, desc(shares, n_tokens_title))
sample_n(airquality, size = 10)
sample_frac(airquality, size = 0.1)
count(airquality, Month)

means <- atmos %>%
  filter(year == year) %>%
  group_by(long, lat) %>%
  summarize(temp = mean(temp, na.rm = TRUE),
         pressure = mean(pressure, na.rm = TRUE),
         ozone = mean(ozone, na.rm = TRUE),
         cloudlow = mean(cloudlow, na.rm = TRUE),
         cloudmid = mean(cloudmid, na.rm = TRUE),
         cloudhigh = mean(cloudhigh, na.rm = TRUE)) %>%
  ungroup()


tbl (pronounced as tibble)
library(tibble)
hfl <- tbl_df(hflights)
tbl_df(data)
as.tibble(mtcars)

Related aRticles:   Data.Table in R    Using Databases in R

March 20, 2022

R Matrices

Matrices in R

Two dimensional R data structure, with all elements in rows and columns having same datatype.

matrix(data = NA, nrow = 1, ncol = 1, byrow = FALSE, dimnames = NULL)
num.matrix <- matrix(1:12, 4) # numeric matrix
matrix(1:6, nrow = 3, ncol=2)
Perf_Matrix <- matrix(1:6, nrow=2)
matrix(1.01:9.99, byrow = TRUE, nrow = 3) # floating point matrix
matrix(letters[1:9], byrow = FALSE, nrow = 3) # character matrix
matrix(1:6, nrow=2)<6 # logical matrix
log_mat <- matrix(rep(c(TRUE, FALSE), 3), nrow=2)
column<-matrix(rep(1:100,100), nrow=100, byrow=T)
matrix(1:6, 2, , FALSE)
dice_matrix <- matrix(dice_vector, nrow = 3, byrow = T)
M <- matrix(seq(1,16), 4, 4)
> x <- matrix(rnorm(9), nrow = 3, dimnames = list(c("X","Y","Z"), c("A","B","C")))
mdat <- matrix(c(1, 2, 3, 11, 12, 13), nrow = 2, ncol = 3, byrow = TRUE, dimnames = list(c("row1", "row2"), c("C.1", "C.2", "C.3")))
matrix(nrow=3, ncol=3) # NA matrix
matrix(scan('matrix9.dat'), nrow=3, byrow=TRUE)

cbind(…, deparse.level = 1) # column binding
> cbind(c(1,2,3), c(4,5,6))
cbind(2:4, c(9,3,5))
cbind(0, rbind(1, 1:3))
cbind(I = 0, X = rbind(a = 1, b = 1:3))
all_wars_matrix <- cbind(star_wars_matrix,worldwide_vector)

rbind(…, deparse.level = 1, make.row.names = TRUE, stringsAsFactors = default.stringsAsFactors()) # row binding
> rbind(c(1,2,3), c(4,5,6))
mat <- rbind(1:3, c(6,9,2))
rbind(1:4, c = 2, "a++" = 10, dd, deparse.level = 2)

> attributes(a)
> class(a)
[1] "matrix"
mode(matrix9)
length(mat3)
length(mat[2, ])
dim(dice_matrix)
dim(mat)[3]
dimnames(x)[[1]] <- letters[1:8]
dimnames(y)[[2]] < paste("observation", 1:4, sep="")

> colnames(x)
> colnames(x) <- c("C1", "C2", "C3")
colnames(last_5) <- paste0("year_", 1:5)
> rownames(x)
> rownames(x) <- c("R1", "R2", "R3")
rowSums(x, na.rm = FALSE, dims = 1)
rowMeans(x, na.rm = FALSE, dims = 1)
colSums(x, na.rm = FALSE, dims = 1)
colMeans(x, na.rm = FALSE, dims = 1)
rowsum(x, group, reorder = TRUE, na.rm = FALSE, ...)

> A[1:4]
> x[ , ]    # leaving row, column field blank will select entire matrix
> x[c(1,2), c(2,3)]    # select rows 1 & 2 and columns 2 & 3
> x[c(3,2), ]    # leaving column field blank will select all columns
> mat[c(3, 5, 7)]
my_matrix[1:3, 2:4]
x[cbind(c(1,3,2), c(3,3,2))] 
x[c(1,3,2), c(3,3,2)]
mat[8] # 8th element in matrix, count column wise
## We can use negative integers to specify rows or columns to be excluded.
> x[-1, ]    # select all rows except first
> x[1,,drop=FALSE]  # result is a 1X3 matrix rather than a vector
> x[c(TRUE,FALSE,TRUE), c(TRUE,TRUE,FALSE)]
> x[, "A"]
> x[TRUE, c("A","C")]

> x[x%%2 == 0]    # select even elements
> x[x>4]    # select elements greater than 5
mat > 10

> x[2, 2] <- 10    # modify a single element
> x[x<5] <- 0    # modify elements less than 5
> rbind(x, c(1, 2, 3))    # add row
> cbind(x, c(1, 2, 3))    # add column
> x <- x[1:2, ]    # remove last row
> dim(x) <- c(3, 2)    # change to 3X2 matrix
write.matrix(x, file = "", sep = " ", blocksize)
sum(mat)
sum(3*col_mat^3/(1+row_mat*col_mat))

mat1 %*% mat2 # matrix multiplication
crossprod(x, y = NULL) # matrix cross-product
z <- crossprod(1:4)
tcrossprod(x, y = NULL)
tcrossprod(as.matrix(x))
> t(x)    # transpose a matrix

as.matrix # attempts to turn its argument into a matrix.
as.matrix(warpbreaks[1:10, ])
x  <- as.matrix(mtcars)
is.matrix # tests if its argument is a (strict) matrix.
is.matrix(as.matrix(1:10))

Related Articles:   Data Table in R        R lists


September 5, 2021

R Lists

Lists in R programming language

A List is a one dimensional R data structure, similar to a vector.
In list, each element is itself a data structure.
Each element of a list could be a vector, factor, matrix, dataframe or even a list itself.
lists of vectors (or) recursive vectors -- lists containing other lists.

my_list <- list(my_vector, my_matrix, my_df)
mylist <- list(team1, Stadiums, Perf_Matrix)
R_list <- list(Teaminfo = team1, St = Stadiums, Perf = Perf_Matrix )
x <- list(2.4,TRUE,1:3)
x <- list("a" = 6.5, "b" = FALSE, "c" = 1:4)
a <- list(a=1:6, b="string", c=pi, d=list(-1, -5))
empty.list <- list()
vector(mode="list", length=6)
list(1:5, letters[1:5], rnorm(5))

access a single element from list, using double brackets [[.
subsetting lists using single brackets [ results in lists.

mylist[[2]]            # Accessing second elements
mylist[["St"]]        # Accessing second elements, using column name
mylist[2]               # Accessing second element
mylist[[2]][3]        # Accessing third element in second vector/element
mylist[[1]][2,]       # Accessing second row of first data frame
list33[[4]][[2]]
> listR[c(1:2)]       # index using integer vector
> x[c(T,F,F)]         # index using logical vector
> x[-2]                 # using negative integer to exclude second component
> x[c("age","speaks")]  # index using character vector
> x["age"]            # single [ returns a list
> x[["age"]]          # double [[ returns the content
> x$name             # same as x[["name"]]
> x$a                   # partial matching, same as x$ag or x$age
> x[["a"]]             # cannot do partial match with [[
> x$speaks[1]
shining_list$reviews
shining_list[["reviews"]]
mylist[[1]][["name"]]

> length(list2)
> str(list4)
str(R_list[[4]])

> x[["name"]] <- "Satya"
> L[["married"]] <- FALSE
> L[["age"]] <- NULL
> x$married <- NULL
list3[[2]][1] = "aaaa"
song$similar$reason <- "nothingggg"
my.data[[i]] <- matrix(rnorm(16),nrow=4)
sh_list <- c(existing_column, new_element = "newwen!")

names(my_list) <- c("name1", "name2")

pairlist(…)
as.pairlist(x)
is.pairlist(list.8)

is.list(df)
is.list(lll)
as.list(x, all.names = FALSE, sorted = FALSE, …)
as.list(y ~ x1 + x2 + x3 + x4)

which.min(list(A = 7, pi = pi))

unlist(x, recursive = TRUE, use.names = TRUE)
unlist(l2)
unlist(options())
unlist(options(), use.names = FALSE)
unlist(l.ex, recursive = FALSE)
ul <- unlist(initial.param)
unlist(strsplit("a.b.c", "[.]"))
unlist(strsplit("a.b.c", ".", fixed = TRUE))

relist(flesh, skeleton = attr(flesh, "skeleton"))
relist(ul)
as.relistable(x)
is.relistable(x)

initial.param <- as.relistable(ipar)

attach(list_R)
detach(list_R)

Related Articles:   Lists in Python        Vectors in R


July 20, 2021

Functions in R

User-Defined Functions in R

Functions are objects, in R
func_name <- function (argument) {
   statement
}

power_R <- function(x, y) {
   # function to print x raised to the power y
   result <- x^y
   print(paste(x,"raised to the power", y, "is", result))
}

power_R <- function(x, y = 2) {
   # function to print x raised to the power y
   result <- x^y
   print(paste(x,"raised to the power", y, "is", result))
}

return(expression)

If there are no explicit returns from a function, the value of the last evaluated expression is returned

check <- function(x) {
   if (x > 0) {
       result <- "Positive"
   } else if (x < 0) {
       result <- "Negative"
   } else {
       result <- "Zero"
   }
   return(result)
}

multi_return <- function() {
   my_list <- list("color" = "red", "size" = 20, "shape" = "round")
   return(my_list)
}

To make assignments to global variables, superassignment operator, <<-, is used

outer_func <- function(){
   inner_func <- function(){
       a <<- 30
       print(a)
   }
   inner_func()
   print(a)
}

recursive.factorial <- function(x) {
   if (x == 0)    return (1)
   else           return (x * recursive.factorial(x-1))
}

print.student <- function(obj) {
 cat(obj$name, "\n")
 cat(obj$age, "years old\n")
 cat("GPA:", obj$GPA, "\n")
}

formals(func_name)
body(func_name)
environment(func_name)

fn <- function(x) {x+x+x}; fn(6)
(function(x) {x+1}) (2) # function definition/call in single line
my.standard<-function(x){(x-mean(x))/sd(x)}

There are three kinds of functions in R.
Most of the functions that you come across are called closures.
A few important functions, like length() are known as builtin functions, which use a special evaluation mechanism to make them go faster.
Language constructs, like if and while are also functions! They are known as special functions.

args(predict)
args(train.default)

Related Articles: R Date Time operations


June 4, 2021

R Conditional Statements if else

Conditional Statements, if else, ifelse function, switch, in R

if block syntax:
if (test_expression) {
   statement
}

if-else block syntax:
if (test_expression) {

   statement1
} else {
   statement2
}

if-elseif-else block syntax:
if (test_expression1) {

   statement1
} else if (test_expression2) {
   statement2
} else if (test_expression3) {
   statement3
} else
   statement4

It's important that the else if keywords comes on the same line as the closing bracket of the previous part of the control construct.


y <- if(x > 2) 4 else 8


if(x > 0) print("Positive number") else print("Negative number")

if (total > 999)

{
  print("You are out of cash!")
} else if (total <800) {
  print("You can buy some Goodies!")
} else {
  print("Your shopping is done, but No Goodies!")
}

if(person$marital == "married")

{
  if(person$housing == "yes" | person$salary > 50000)
  {
    print("Issue credit card")
  } else
  {
    print("Sorry, you are not eligible for credit card")
  }
} else if(person$marital == "single")
{
  if(person$education == "tertiary" & person$salary > 30000)
  {
    print("Issue credit card")
  } else
  {
      print("Sorry, you are not eligible for credit card")
  }
}

if (test1 & test2 & test3){

    cat("Your answers are correct! Good job...\n")
}

if (!"cars_cyl" %in% l| !"cars_4" %in% l | !"cars_3" %in% l) {

  cat('You have not created all three variables. Please have a look!\n')
}

ifelse function in R:
ifelse(test_expression, x, y)
ifelse(vector_r %% 2 == 0, "even", "odd")
ifelse(num_char < med_num_char, "below median", "at or above median")
odi$century <- ifelse(odi$Runs > 99, 1, 0)
grades$penalized <- ifelse(grades$date >"2017-11-03", 1, 0)

switch function in R:
switch (statement, list)
switch(EXPR, ...)
> switch(2, "red", "green", "blue")
> x <- switch(4, "red", "green", "blue")
> switch("color", "color" = "red", "shape" = "square", "length" = 5)
cat(ch,":", switch(EXPR = ch, a = 1, b = 2:3), "\n")
switch(ff[1], A = "I am A", B="Bb..", C=" is C")
switch(2, invisible(pi), pi)

Related Articles: Distributions in R   R Format functions

March 23, 2021

R shiny package

shiny package in R

install.packages("shiny")

ui <- fluidPage("Hello", "There")
ui <- fluidPage(h1("Shiny"), "by", strong("Satya"))

shinyApp(ui, server)
titlePanel("Discrete Random Variable - Frequency Distribution")
fluidRow(column(12, verbatimTextOutput("value")))
shinyUI(fluidPage(title = "Expected Value", h3("Instructions"), hr() ))
plotOutput("redPlot")
plotOutput(outputId="my_plot")

actionButton("action", label="Play 10 times", class="btn btn-danger text-center"
p("Click the button to play the game 10 times.")
h1()
h2()
br()
em()
strong()
column(6, h5("Expected Value"), br(), plotOutput("exp_plot") ) )

mainPanel("this is main panel")
mainPanel( plotOutput("plot"), tableOutput("table") )
sidebarPanel("this is side bar")
sidebarPanel(
      h4("Plot parameters"),
      textInput("title", "Plot title", "Car speed vs distance to stop"),
      numericInput("num", "Number of cars to show", 30, 1, nrow(cars)),
      sliderInput("size", "Point size", 1, 5, 2, 0.5)
    )
sidebarLayout(sidebarPanel(), mainPanel())

actionButton()
checkboxInput()
checkboxGroupInput()
dateInput()
dateRangeInput()
fileInput()
numericInput("num", "Number of rows", value=9, min=1)
numericInput(inputId="customers", label="no. of customers", value=4, min=0)
passwordInput()

radioButtons()
selectInput()
sliderInput()
textInput("name", "What is your name?")
textInput(inputId="name", label="Enter your name", value="Satya")
textOutput(outputId = "greeting")
textAreadInput()
colourpicker::colourInput()
renderPlot()
renderText()
renderTable()
xx <- reactive({ input$num + 2 })
sum <- reactive({ input$num1 + input$num2 })
observe({ print(input$num)  print( xx() ) })

Related Articles: ggplot2 package in R       R Matrices


February 15, 2021

R - ggplot2 package

ggplot2 package in R language

library(ggplot2)

ggplot(data = NULL, mapping = aes(), ..., environment = parent.frame()) # gg - grammar of graphics
ggplot(email50, aes(x = number_yn)) + geom_bar()
ggplot(data = hsb2, aes(x=science, y=math)) + geom_point()
ggplot(data = hsb2, aes(x=science, y=math, color=prog)) + geom_point()
mtc_plot <- ggplot(mtcars, aes(x = factor(cyl), y = mpg)) +  geom_point()
ggplot(mtcars, aes(x = wt, y = mpg, shape = disp)) + geom_point()
ggplot(mammals, aes(x=body, y=brain)) + geom_point(alpha=0.6) + stat_smooth(method="lm", col="red", se=FALSE)
ggplot(mammals, aes(x=body, y=brain)) + geom_point(alpha=0.6) + coord_fixed() + scale_x_log10() + scale_y_log10() + stat_smooth(method="lm", col="#C421243", se=FALSE, size=1)
ggplot(df, aes(gp, y)) + geom_point() + geom_point(data = ds, aes(y = mean), colour = 'red', size = 3)
ggplot() + geom_point(data = df, aes(gp, y)) + geom_point(data = ds, aes(gp, mean), colour = 'red', size = 3) + geom_errorbar( data = ds, aes(gp, mean, ymin = mean - sd, ymax = mean + sd), colour = 'red', width = 0.4 )
ggplot(mtcars, aes(wt, mpg)) + geom_point(colour = "red", size = 3)
dia_plot <- ggplot(diamonds, aes(x=carat, y=price, col=clarity)) + geom_smooth() + geom_point(alpha=0.4)
ggplot(mtcars, aes(wt, mpg)) +  geom_point(shape = 21, colour = "black", fill = "white", size = 5, stroke = 5)
ggplot(mtcars2, aes(wt, mpg)) + geom_point(na.rm = TRUE)
ggplot(mtcars, aes(x = wt, y = mpg, col = factor(cyl))) + geom_point()
dia_plot <- ggplot(diamonds, aes(carat, price)) + geom_boxplot()
ggplot(diamonds, aes(carat, price)) + geom_boxplot(aes(group = cut_width(carat, 0.25)), outlier.alpha = 0.1)
ggplot(rpart::kyphosis, aes(Age, as.numeric(Kyphosis) - 1)) + geom_jitter(height = 0.05) +  binomial_smooth()
ggplot(mtcars, aes(x = wt,y = mpg, size = disp, col= hp)) +  geom_text(aes(label = cyl))
with(dfr[(dfr$var3 < 155) & (dfr$var4 > 27),], plot(var1, var2))
with(d, plot(x, n, pch=16, axes=F, xlab=NA, ylab=NA, cex=1.2))
ggplot(mtcars, aes(x=cyl, y=wt)) + geom_point(position = "jitter")
ggplot(mtcars, aes(x = wt, y = mpg, col = factor(cyl), label = cyl)) + geom_label()

aes(x, y, ...)
aes(mpg, wt)
aes(x = mpg ^ 2, y = wt / cyl)
aes(color = x)
aes(fg = x)

layer(geom = NULL, stat = NULL, data = NULL, mapping = NULL, position = NULL, params = list(), inherit.aes = TRUE, check.aes = TRUE, check.param = TRUE, subset = NULL, show.legend = NA)
ggplot(mpg, aes(displ, hwy)) +
  layer(geom = "point", stat = "identity", position = "identity", data = head, params = list(na.rm = FALSE) )

borders(database = "world", regions = ".", fill = NA, colour = "grey50", xlim = NULL, ylim = NULL, ...)
ggplot(ia, aes(long, lat)) +
  geom_polygon(aes(group = group), fill = NA, colour = "grey60") +
  geom_text(aes(label = subregion), data = seats, size = 2, angle = 45)
ggplot(capitals, aes(long, lat)) +
  borders("state") +
  geom_point(aes(size = pop)) +
  scale_size_area() +
  coord_quickmap()
ggplot(diamonds, aes(price, colour = cut)) + geom_freqpoly()
ggplot(mtcars, aes(x=mpg, col=factor(cyl))) + geom_freqpoly(binwidth=1)
ggplot() + 
  geom_polygon(data = usa, aes(x=long, y = lat, group = group), fill = NA, color = "red") + 
  coord_fixed(1.3)

qplot(data=diamonds, x=carat, y=price, color=cut) + scale_color_brewer(palette='Accent') # quick plot
qplot(data=reddit, x=age.range)
qplot(x=dob, data=pf)
qplot(carat, price, data=diamonds, color=cut, log="xy", facets=~clarity, main="Diamonds")
qplot(data$sex, geom="histogram", binwidth = 0.25, main = "Histogram for G2", xlab = "sex", fill=I("blue"))
qplot(x = friend_count, data = pf, binwidth = 25) +
  scale_x_continuous(limits = c(0, 1000), breaks = seq(0, 1000, 50))
qplot(Temp, data=airquality, binwidth=5)
qplot(age, wage, colour=class, data=training) + geom_smooth(method="lm", formula=y~x)
qplot(cwage, age, data=training, fill=cwage, geom=c("box plot","jitter"))
qplot(num_balls) + xlab("Balls") + ylab("Count") + ggtitle("Probability Distribution")
qplot(data=data, x=failures, y=G1, geom=c("point","smooth")) + labs(list(title="Failures Vs G1", x="Failures", y="G1")) 

geom_point(mapping = NULL, data = NULL, stat = "identity", position = "identity", ..., na.rm = FALSE, show.legend = NA, inherit.aes = TRUE)
p + geom_point(aes(shape = factor(cyl)))
p + geom_point(aes(colour = cyl)) + scale_colour_gradient(low = "blue")
p + geom_point(colour = "black", size = 4.5) + geom_point(colour = "pink", size = 4) +  geom_point(aes(shape = factor(cyl)))

geom_jitter(mapping = NULL, data = NULL, stat = "identity", position = "jitter", ..., width = NULL, height = NULL, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE)
ggplot(mtcars, aes(x=cyl, y=wt)) + geom_jitter()
p + geom_jitter()
p + geom_jitter(aes(colour = class))
ggplot(mpg, aes(cyl, hwy)) + geom_jitter(width = 0.25)
ggplot(mpg, aes(cty, hwy)) + geom_jitter(width = 0.5, height = 0.5)
ggplot(mtcars, aes(x=cyl, y=wt)) + geom_jitter(position=position_jitter(width=0.5))

geom_count(mapping = NULL, data = NULL, stat = "sum", position = "identity", ..., na.rm = FALSE, show.legend = NA, inherit.aes = TRUE)
ggplot(mpg, aes(cty, hwy)) + geom_count()
ggplot(mpg, aes(cty, hwy)) + geom_count() + scale_size_area()
d + geom_count(aes(size = ..prop..))
d + geom_count(aes(size = ..prop.., group = cut)) + scale_size_area(max_size = 10)
stat_sum(mapping = NULL, data = NULL, geom = "point", position = "identity", ..., na.rm = FALSE, show.legend = NA, inherit.aes = TRUE)
ggplot(diamonds, aes(x = clarity, y = carat, col = price)) + stat_sum()

geom_bin2d(mapping = NULL, data = NULL, stat = "bin2d", position = "identity", ..., na.rm = FALSE, show.legend = NA, inherit.aes = TRUE)
d + geom_bin2d()
d + geom_bin2d(bins = 30)
d + geom_bin2d(binwidth = c(0.1, 0.1))
stat_bin_2d(mapping = NULL, data = NULL, geom = "tile", position = "identity", ..., bins = 30, binwidth = NULL, drop = TRUE, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE)

geom_quantile(mapping = NULL, data = NULL, stat = "quantile", position = "identity", ..., lineend = "butt", linejoin = "round", linemitre = 1, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE)
m + geom_quantile(quantiles = q10)
m + geom_quantile(method = "rqss", lambda = 0.1)
m + geom_quantile(colour = "red", size = 2, alpha = 0.5)
stat_quantile(mapping = NULL, data = NULL, geom = "quantile", position = "identity", ..., quantiles = c(0.25, 0.5, 0.75), formula = NULL, method = "rq", method.args = list(), na.rm = FALSE, show.legend = NA, inherit.aes = TRUE)

geom_bar(mapping = NULL, data = NULL, stat = "count", position = "stack", ..., width = NULL, binwidth = NULL, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE)
ggplot(driver_years, aes(y=driverdeaths, x=date_year)) + geom_bar(stat="identity")
ggplot(mtcars, aes(x = factor(cyl), fill =factor(am))) +
   geom_bar(alpha = 0.5, position = position_dodge())
ggplot(mtcars, aes(factor(cyl), fill = factor(vs))) + geom_bar(position = "dodge")
ggplot(data=dat1, aes(x=time, y=total_bill, fill=sex)) +
  geom_bar(stat="identity", position=position_dodge(), colour="black") +
  scale_fill_manual(values=c("#999999", "#E69F00"))
ggplot(data=dat1, aes(x=time, y=total_bill, fill=sex)) +
  geom_bar(colour="black", stat="identity", position=position_dodge(), size=.3) +                       
  scale_fill_hue(name="Sex of payer") +     
  xlab("Time of day") + ylab("Total bill") + ggtitle("Average bill for 2 people") +    
  theme_bw()
ggplot(data=uber, aes(x=request_hour, fill=factor(request_status))) +
  geom_bar(position = "dodge", colour="black") +
  xlab("Time of day") + ylab("Total Requests") + ggtitle("Demand and Supply Gap") +  
  scale_fill_manual("Request Status", values=c("green3", "red4")) + theme_bw() +
  theme(legend.position = "top")
p =  n + scale_fill_manual(
    values = c("skyblue", "royalblue", "blue", "navy"),
    limits = c("d", "e", "p", "r"), breaks =c("d", "e", "p", "r"),
    name = "fuel", labels = c("Diesel", "Electric", "Petrol", "Hybrid"))
g +
 geom_bar(aes(fill = drv), position = position_stack(reverse = TRUE)) +
 coord_flip() +
 theme(legend.position = "top")
p <- ggplot(df, aes(x = color)) + 
  geom_bar(aes(y = ..count../sum(..count..), fill = cut)) + 
  scale_fill_brewer(palette = "Set3")
ggplot(dfl, aes(x, y=y, fill=x)) + geom_bar(stat="identity") +
    geom_text(aes(label=y), vjust=0) +
    opts(axis.text.x=theme_blank(),
        axis.ticks=theme_blank(),
        axis.title.x=theme_blank(),
        legend.title=theme_blank(),
        axis.title.y=theme_blank()
)
h + coord_flip() + scale_x_reverse()
ggplot(data=uber, aes(x=request_hour, fill=factor(request_status))) +  
  geom_bar(position = "stack", colour="black") +
  xlab("Time of day") + ylab("Total Requests") + ggtitle("Demand and Supply Gap") +  
  scale_fill_manual("Request Status", values=c("green3", "red4"), labels = c("Request Successful", "Request Unsuccessful")) + 
  coord_flip() + 
  theme(legend.position = "top") + 
  geom_text(stat='count',aes(label=abs(..count..)))
ggplot(linetypes, aes(0, y)) + 
  geom_segment(aes(xend = 5, yend = y, linetype = lty)) + 
  scale_linetype_identity() + 
  geom_text(aes(label = lty), hjust = 0, nudge_y = 0.2) +
  scale_x_continuous(NULL, breaks = NULL) + 
  scale_y_continuous(NULL, breaks = NULL)

hjust and vjust are only defined between 0 and 1:
0 means left-justified
1 means right-justified

ggplot(lc, aes(x=purpose, fill=purpose)) + geom_bar() +
  geom_text(stat = "count", aes(x=purpose, label=..count..), position=position_stack(vjust=0.5)) +
  labs(x="Loan Purpose", y="No of requests", title="Loan Purpose") +
  theme(axis.text.x=element_text(angle = 90, hjust = 0))

geom_col(mapping = NULL, data = NULL, position = "stack", ..., width = NULL, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE)
ggplot(sleep, aes(x = ID, y = extra, fill = group)) + geom_col()
ggplot(sleep, aes(x = ID, y = extra, fill = group)) + geom_col(position="dodge")
ggplot(USArrests, aes(x = row.names(USArrests), y = USArrests$Murder, lab)) +  geom_col()
ggplot(USArrests, aes(x = row.names(USArrests), y = USArrests$Murder, lab)) + 
  geom_col() + 
  theme(axis.text.x=element_text(angle=90, hjust=1))

stat_count(mapping = NULL, data = NULL, geom = "bar", position = "stack", ..., width = NULL, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE)
ggplot(df, aes(factor(var2))) +
      geom_bar(fill="dodgerblue3")+
      labs(x = NULL,y = NULL)+
      stat_count(aes(label = paste(prop.table(..count..) * 100, "%", sep = "")),
               vjust = 1, geom = "text", position = "identity", color ="white")
stat="bin"   (default)
stat="identity"
stat="count"

geom_line(mapping = NULL, data = NULL, stat = "identity", position = "identity", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, ...)
ggplot(economics, aes(x=date, y=unemploy)) + geom_line()
ggplot(economics, aes(x=date, y=unemploy/pop, col=psavert)) + geom_line() + geom_point()
ggplot(ukacc, aes(date)) + 
  geom_line(aes(y = rear, colour = "blue"))+
  geom_line(aes(y = drivers, colour = "red3")) + 
  geom_line(aes(y = front, colour = "green"))
ggplot(ukacc, aes(x=date, y=drivers), col="black", size=1) + geom_line() +
  geom_line(data=ukacc, aes(x=date, y=front), col="red", size=1) +
  geom_line(data=ukacc, aes(x=date, y=rear), col="blue", size=1)
geom_path(mapping = NULL, data = NULL, stat = "identity", position = "identity", ..., lineend = "butt", linejoin = "round", linemitre = 1, arrow = NULL, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE)
ggplot(economics[economics$date > as.Date("2010-01-01"),], aes(x=date, y=unemploy)) + geom_path()
geom_step(mapping = NULL, data = NULL, stat = "identity", position = "identity", direction = "hv", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, ...)
ggplot(economics, aes(x=date, y=unemploy)) + geom_step()
ggplot(economics[economics$date > as.Date("2010-01-01"), ], aes(x=date, y=unemploy)) + geom_step()

geom_boxplot(mapping = NULL, data = NULL, stat = "boxplot", position = "dodge", ..., outlier.colour = NULL, outlier.color = NULL, outlier.fill = NULL, outlier.shape = 19, outlier.size = 1.5, outlier.stroke = 0.5, outlier.alpha = NULL, notch = FALSE, notchwidth = 0.5, varwidth = FALSE, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE)
stat_boxplot(mapping = NULL, data = NULL, geom = "boxplot", position = "dodge", ..., coef = 1.5, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE)
p + geom_boxplot()
p + geom_boxplot() + geom_jitter(width = 0.2)
p + geom_boxplot() + coord_flip()
p + geom_boxplot(notch = TRUE, varwidth = TRUE)
p + geom_boxplot(fill = "white", colour = "#3366FF")
p + geom_boxplot(outlier.colour = "red", outlier.shape = 1)
ggplot(ukacc_new, aes(group=date_year, x=date_year, y=drivers)) + geom_boxplot()
ggplot(lc, aes(x=lc$grade, y=lc$loan_amnt)) + geom_boxplot(aes(fill = grade)) +
  theme(plot.title=element_text(size=14),
        axis.text=element_text(size=14),
        axis.title=element_text(size=14)) +
  labs(list(
    title = "Loan Amount by Grade",
    x = "Grade",
    y = "Loan Amount")
  )  

p<- ggplot(data = diamonds)
p <- p + aes(x = carat, y = depth)
p <- p + geom_point()
p <- p + geom_density2d() + facet_grid(cut~.)

geom_smooth(mapping = NULL, data = NULL, stat = "smooth", position = "identity", ..., method = "auto", formula = y ~ x, se = TRUE, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE)
stat_smooth(mapping = NULL, data = NULL, geom = "smooth", position = "identity", ..., method = "auto", formula = y ~ x, se = TRUE, n = 80, span = 0.75, fullrange = FALSE, level = 0.95, method.args = list(), na.rm = FALSE, show.legend = NA, inherit.aes = TRUE)
ggplot(mpg, aes(displ, hwy)) + geom_point() +  geom_smooth(span = 0.3)
ggplot(mpg, aes(displ, hwy)) +
  geom_point() +
  geom_smooth(method = "lm", formula = y ~ splines::bs(x, 3), se = FALSE)
ggplot(mpg, aes(displ, hwy)) +
  geom_point() +
  geom_smooth(span = 0.8) +
  facet_wrap(~drv)
ggplot(mpg, aes(displ, hwy)) +
  geom_point() +
  facet_wrap(c("cyl", "drv"), labeller = "label_both")
ggplot(economics_long, aes(date, value)) +
  geom_line() +
  facet_wrap(~variable, scales = "free_y", nrow = 2, strip.position = "bottom") +
  theme(strip.background = element_blank(), strip.placement = "outside")
ggplot(diamonds, aes(x=price)) + geom_histogram(binwidth=100) + facet_wrap(~cut)

geom_violin(mapping = NULL, data = NULL, stat = "ydensity", position = "dodge", ..., draw_quantiles = NULL, trim = TRUE, scale = "area", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE)
stat_ydensity(mapping = NULL, data = NULL, geom = "violin", position = "dodge", ..., bw = "nrd0", adjust = 1, kernel = "gaussian", trim = TRUE, scale = "area", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE)
p + geom_violin()
p + geom_violin() + geom_jitter(height = 0, width = 0.1)
p + geom_violin(scale = "count")
p + geom_violin(trim = FALSE)
p + geom_violin(aes(fill = factor(cyl)))
p + geom_violin(fill = "grey80", colour = "#3366FF")
p + geom_violin(draw_quantiles = c(0.25, 0.5, 0.75))
m + geom_violin() + scale_y_log10() + coord_trans(y = "log10")

p1 + theme(
  panel.background = element_rect(fill = NA),
  panel.grid.major = element_line(colour = "grey50"),
  panel.ontop = TRUE
)
p1 + theme(axis.text = element_text(colour = "blue"))
p1 + theme(axis.ticks.length = unit(.25, "cm"))
p2 + theme(legend.justification = "top")
p3 + theme(strip.background = element_rect(colour = "black", fill = "white"))
p2 + theme(
  legend.position = c(.95, .95),
  legend.justification = c("right", "top"),
  legend.box.just = "right",
  legend.margin = margin(6, 6, 6, 6)
)
bar_theme <- theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5), legend.position="none")
box_theme <- theme(axis.line.y=element_blank(),axis.title.y=element_blank(),  axis.ticks.y=element_blank(), axis.text.y=element_blank(), legend.position="none")

scale_x_discrete() 
scale_x_continuous()
ggplot(mtcars, aes(x=mpg)) + geom_histogram()
ggplot(mtcars, aes(x=mpg, fill=factor(cyl))) + geom_histogram(binwidth=1)
ggplot(aes(x = dob_day), data = pf) + geom_histogram(binwidth = 2) +  scale_x_continuous(breaks = 1:31)
ggplot(aes(x = friend_count), data = subset(pf, !is.na(gender))) +
  geom_histogram() +
  scale_x_continuous(limits = c(0, 1000), breaks = seq(0, 1000, 50)) +
  facet_wrap(~gender)
ggplot(aes(x = tenure / 365), data = pf) +
  geom_histogram(color = 'black', fill = '#F79420') +
  scale_x_continuous(breaks = seq(1, 7, 1), limits = c(0, 7)) +
  xlab('Number of years using Facebook') +
  ylab('Number of users in sample')
ggplot(diamonds, aes(price, fill = cut)) + geom_histogram(position="dodge")
ggplot(grades, aes(day)) + geom_histogram(stat = "count")

ggplot(data = melted_cormat, aes(x=Var1, y=Var2, fill=value)) + geom_tile()
ggplot(z.m, aes(X1, X2, fill = value)) + geom_tile() + scale_fill_gradient(low = "blue",  high = "yellow")
ggplot(data = melted_cormat, aes(Var2, Var1, fill = value))+
 geom_tile(color = "white")+
 scale_fill_gradient2(low = "blue", high = "red", mid = "white", 
   midpoint = 0, limit = c(-1,1), space = "Lab", 
   name="Pearson\nCorrelation") +
  theme_minimal()+ 
 theme(axis.text.x = element_text(angle = 45, vjust = 1, 
    size = 12, hjust = 1))+
 coord_fixed()

p + geom_linerange(aes(ymin = y - 1, ymax = y + 1), position = "dodge")
p + geom_errorbar( aes(ymin = y - 1, ymax = y + 1), width = 0.2, position = "dodge")

geom_abline(mapping = NULL, data = NULL, ..., slope, intercept, na.rm = FALSE, show.legend = NA)

geom_hline(mapping = NULL, data = NULL, ..., yintercept, na.rm = FALSE, show.legend = NA)
ggplot(media, aes(day, error)) + geom_point() + 
 scale_x_continuous(name = "days", breaks = seq(0,90,10), limits = c(0,84)) + 
 scale_y_continuous(name = "Error", breaks = seq(-250000,250000,50000), limits = c(-250000,250000)) +
 geom_hline(yintercept = 0)

geom_vline(mapping = NULL, data = NULL, ..., xintercept, na.rm = FALSE, show.legend = NA)
plot_1 + geom_vline(xintercept = as.numeric((ukacc$date)), linetype=4, size = 4, alpha = 0.5)
ggplot(data=mydata,aes(y=somevalues,x=datefield,color=category)) +
      layer(geom="line") + geom_vline(xintercept=mydata$datefield[120],linetype=4)

ggplot(s, aes(x=factor(x1), y=s, width=0.18)) + geom_bar(stat="identity", fill="#f98866", width=0.25) +
    theme_minimal() +
    coord_cartesian(ylim = c(0, 40)) + 
xlab("X (Number of red balls)") + ylab("Frequency") + theme(legend.position = "none") + theme(text = element_text(size=14))

d=expand.grid(h=seq(0,350,10), c=seq(0,100,5), l=seq(0,100,20))
ggplot() +
coord_polar(theta="x")+facet_wrap(~l) +
scale_x_continuous(name="hue", limits=c(0,360), breaks=seq(5,345,20), labels=seq(0,340,20)) +
scale_y_continuous(name="chroma", breaks=seq(0, 100, 20)) +
scale_fill_identity() +
geom_rect(data=d, mapping=aes(xmin=h, xmax=h+resolution(h), ymin=c, ymax=c+resolution(c), fill=hcl(h,c,l)), color="white", size=0.1)

ggplot() +
facet_wrap(~b) +
scale_x_continuous(name="red", breaks=seq(0.05, 1.05, 0.2), labels=seq(0, 1, 0.2)) +
scale_y_continuous(name="green", breaks=seq(0.05, 1.05, 0.2), labels=seq(0, 1, 0.2)) +
scale_fill_identity() +
geom_rect(data=d, mapping=aes(xmin=r, xmax=r+resolution(r), ymin=g, ymax=g+resolution(g), fill=rgb(r,g,b)), color="white", size=0.1)

geom_map(mapping = NULL, data = NULL, stat = "identity", ..., map, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE)
ggplot(values) + geom_map(aes(map_id = id), map = positions) + expand_limits(positions)
ggplot(values, aes(fill = value)) + geom_map(aes(map_id = id), map = positions) + expand_limits(positions) + ylim(0, 3)

usa <- map_data("usa")
w2hr <- map_data("world2Hires")
states <- map_data("state")
ggplot(data = states) + 
  geom_polygon(aes(x = long, y = lat, fill = region, group = group), color = "white") + 
  coord_fixed(1.3) +
  guides(fill=FALSE)


Related aRticles:  R shiny package  ggthemes ggraph lattice ggmap Packages