Johnson & Johnson data:

plot(JohnsonJohnson)

Iris Data

View the first few lines and look at a few charts

require(ggplot2)
## Loading required package: ggplot2
head(iris)
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 2          4.9         3.0          1.4         0.2  setosa
## 3          4.7         3.2          1.3         0.2  setosa
## 4          4.6         3.1          1.5         0.2  setosa
## 5          5.0         3.6          1.4         0.2  setosa
## 6          5.4         3.9          1.7         0.4  setosa
ggplot(iris, aes(Sepal.Length, Sepal.Width)) + geom_point()

ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, shape = Species, color =
                   Petal.Width)) + geom_point(size = 5)

ggplot(iris) + geom_point(aes(x = Sepal.Length, y = Sepal.Width))

ggplot(iris ) + geom_point(aes(Sepal.Length, Sepal.Width,color = Species, shape=
                                 Species))

Note: install.packages(“ggplot”) ~ this is not available anymore, renamed “ggplot 2”

ggplot(iris, aes(Species, Sepal.Length)) + geom_bar(stat = "identity")

ggplot(iris, aes(Species, Sepal.Length, fill = Species)) + geom_bar(stat =
                                                                      "identity")

ggplot(iris, aes(Sepal.Length, Sepal.Width, shape = Species)) + geom_point()

ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species, size =
                   Petal.Width)) + geom_point()

saving plots to variables:

d <- ggplot(iris)
bar_chart <- d + geom_bar(stat = "identity", aes(Species, Sepal.Length, fill =
                                                   Species))
point_chart <- d + geom_point(aes(Sepal.Length, Sepal.Width,color = Species,
                                  shape= Species))

d <- ggplot(iris, aes(Sepal.Length))
d + stat_bin()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

saving the ggplot graphs

ggsave("Iris_graph.jpg")
## Saving 7 x 5 in image
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

subsets

ggplot(iris) %+% subset(iris,Species == "setosa") + geom_point(aes(Sepal.Length,
                                                                   Sepal.Width))

ggplot(iris,aes(x = Sepal.Length,y = Sepal.Width,color = Species)) +
  geom_point(data = subset(iris, Species %in% c("setosa","virginica")))

set titles and labels

d <- ggplot(iris, aes(Species, Sepal.Length, fill = Species)) + geom_bar(stat =
                                                                           "identity")
d + ggtitle("Iris data: Species vs Sepal Length")

d <- ggplot(iris ) + geom_point(aes(Sepal.Length, Sepal.Width,color = Species,
                                    shape= Species))
d + scale_x_continuous("Sepal Length") +
  scale_y_continuous("Sepal Width")

swap axis:

d + coord_flip()

optimize:

d <- ggplot(iris, aes(Sepal.Length, Sepal.Width, colour = Species)) +
  geom_point()


require(ggthemes)
## Loading required package: ggthemes
## Warning: package 'ggthemes' was built under R version 3.4.4
d + theme_economist() + scale_color_economist() + ggtitle("Iris Species: Sepal Length vs Sepal Width")

d + theme_wsj(base_size = 7)+ggtitle("Iris Species: Sepal Length vs Sepal Width")