This is an R HTML document. When you click the Knit HTML button a web page 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:

Basic scatter plot
  library(ggplot2)
 ggplot(diamonds) +
   geom_point(aes(x=carat, y=price, color=cut))  +
   geom_smooth(aes(x=carat, y=price, color=cut))
## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'
plot of chunk unnamed-chunk-1
With geom_smooth but no title
  library(ggplot2)
  ggplot(diamonds) +
    geom_point(aes(x=carat, y=price, color=cut)) +
    geom_smooth(aes(x=carat, y=price))
## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'
plot of chunk unnamed-chunk-2
                                # Remove color from geom_smooth
  ggplot(diamonds, aes(x=carat, y=price)) +
    geom_point(aes(color=cut)) +
    geom_smooth()               # same but simpler
## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'
plot of chunk unnamed-chunk-2
With title but no geom_smooth()
 library(ggplot2)
  gg <- ggplot(diamonds, aes(x=carat, y=price, color=cut)) +
    geom_point() +
                                # add axis lables and plot title.
    labs(title="Scatterplot", x="Caratzzz", y="Priceee")
  print(gg)
plot of chunk unnamed-chunk-3
Perfect Code = With title and geom_smooth()
  library(ggplot2)
  ggplot(diamonds) +
    geom_point(aes(x=carat, y=price, color=cut)) +
    geom_smooth(aes(x=carat, y=price))
## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'
plot of chunk unnamed-chunk-4
                                # Remove color from geom_smooth
  ggplot(diamonds, aes(x=carat, y=price)) +
    geom_point(aes(color=cut)) + labs(title="Scatterplot", x="Caratzzz", y="Priceee") +
    geom_smooth() +       # same but simpler
    facet_wrap(~ cut)
## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'
plot of chunk unnamed-chunk-4
  print(gg)
plot of chunk unnamed-chunk-4
Perfeact Code = With title, geom_smooth() & facet_wrap()
  library(ggplot2)
  ggplot(diamonds) +
    geom_point(aes(x=carat, y=price, color=cut)) +
    geom_smooth(aes(x=carat, y=price))
## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'
plot of chunk unnamed-chunk-5
                                # Remove color from geom_smooth
  ggplot(diamonds, aes(x=carat, y=price)) +
    geom_point(aes(color=cut)) + labs(title="Scatterplot", x="Caratzzz", y="Priceee") +
    geom_smooth() +       # same but simpler
    facet_wrap(~ cut)
## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'
plot of chunk unnamed-chunk-5
  print(gg)
plot of chunk unnamed-chunk-5
  library(ggplot2)
  ggplot(diamonds) +
    geom_point(aes(x=carat, y=price, color=cut)) +
    geom_smooth(aes(x=carat, y=price))
## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'
plot of chunk unnamed-chunk-6
                                # Remove color from geom_smooth
  ggplot(diamonds, aes(x=carat, y=price)) +
    geom_point(aes(color=cut)) + labs(title="Scatterplot", x="Caratzzz", y="Priceee") +
    geom_smooth() +       # same but simpler

    theme(plot.title=element_text(size=30, face="bold"),
                  axis.text.x=element_text(size=15),
                  axis.text.y=element_text(size=15),
                  axis.title.x=element_text(size=25),
                  axis.title.y=element_text(size=25)) +
  scale_color_discrete(name="Cut of diamonds")  # add title and axis text, change legend title.
## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'
plot of chunk unnamed-chunk-6

You can also embed plots, for example: