R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

diamonds %>%
  ggplot(aes(x = cut))  

  geom_bar()
## geom_bar: na.rm = FALSE, just = 0.5, lineend = butt, linejoin = mitre
## stat_count: na.rm = FALSE
## position_stack
diamonds %>%
  ggplot(mapping = aes(x =carat))

  geom_histogram(binwidth = 0.5)
## geom_bar: na.rm = FALSE, orientation = NA, lineend = butt, linejoin = mitre
## stat_bin: na.rm = FALSE, binwidth = 0.5, bins = NULL, orientation = NA
## position_stack
diamonds %>%
  
  filter(carat < 3) %>%
  
  
  ggplot(aes(x = carat)) +
  geom_histogram(binwidth = 0.5)

diamonds %>%
  ggplot(aes(x = carat, color = cut)) +
  geom_freqpoly()
## `stat_bin()` using `bins = 30`. Pick better value `binwidth`.

diamonds %>%
  
  #Filter ot bigger diamonds
  filter(carat < 3) %>%
  
  #Plot
  ggplot(aes(x = carat)) +
  geom_histogram(binwidth = 0.01)

faithful %>%
  
  ggplot(aes(x = eruptions)) +
  geom_histogram()
## `stat_bin()` using `bins = 30`. Pick better value `binwidth`.

diamonds %>%
  
  ggplot(aes(x = y)) +
  geom_histogram()
## `stat_bin()` using `bins = 30`. Pick better value `binwidth`.

diamonds %>%
  
  ggplot(aes(x = y)) +
  geom_histogram() +
  coord_cartesian(ylim = c(0,50))
## `stat_bin()` using `bins = 30`. Pick better value `binwidth`.

diamonds %>%
  
  #filter (y < | y > 20) %>%
  mutate(y = ifelse(y < 3 | y > 20, NA, y)) %>%
  
  #plot
  ggplot(aes(x =, y = y))

  geom_point()
## geom_point: na.rm = FALSE
## stat_identity: na.rm = FALSE
## position_identity
diamonds %>%
  
  ggplot(aes(x = cut, y = price)) +
  geom_boxplot()

diamonds %>% 
  
  count(color, cut) %>%
  
  ggplot(aes(x = color, y = cut, fill = n)) +
  geom_tile()

library(hexbin)
diamonds %>%
  ggplot(aes(x = carat, y = price)) +
  geom_hex()

diamonds %>%
  filter(carat < 3) %>%
  ggplot(aes(x = carat, y = price)) +
  geom_boxplot(aes(group = cut_width(carat, 0.1)))
## Warning: Orientation is not uniquely specified when both the x and y aesthetics are
## continuous. Picking default orientation 'x'.

library(modelr)
mod <- lm(log(price) ~ log(carat), data = diamonds)

diamonds4 <- diamonds %>%
  modelr::add_residuals(mod) %>%
  mutate(resid = exp(resid))

diamonds4 %>% 
  ggplot(aes(carat, resid)) +
  geom_point()

diamonds4 %>% 
  ggplot(aes(cut, resid)) +
  geom_boxplot()

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.