ggplot(mtcars, aes(x = factor(cyl), y = mpg)) + geom_point()

ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point()

ggplot(mtcars, aes(x = wt, y = mpg, color = disp)) + geom_point()

ggplot(mtcars, aes(x = wt, y = mpg, size = disp)) + geom_point()

ggplot(diamonds, aes(x = carat, y = price)) +geom_point()

ggplot(diamonds, aes(x = carat, y = price)) +geom_point() + geom_smooth()

ggplot(diamonds, aes(x = carat, y = price)) + geom_smooth()

ggplot(diamonds, aes(x = carat, y = price, color=clarity)) + geom_smooth()

ggplot(diamonds, aes(x = carat, y = price, color=clarity)) + geom_point( alpha=0.4)

Adding aes() inside geom_point() :

ggplot(diamonds, aes(x = carat, y = price)) + geom_point()

ggplot(diamonds, aes(x = carat, y = price)) + geom_point(aes(color = clarity))

ggplot(diamonds, aes(x = carat, y = price)) + geom_point(alpha=0.2)

ggplot(diamonds, aes(x = carat, y = price)) + geom_point(alpha=0.2) + geom_smooth(se = FALSE)

ggplot(diamonds, aes(x = carat, y = price)) + geom_point(alpha=0.2) + geom_smooth(aes(col = clarity), se = FALSE)

mtcars$cyl <- as.factor(mtcars$cyl)
ggplot(mtcars, aes(x = wt, y = mpg, col = cyl))
+ geom_point()
+ geom_smooth(method='lm', se=FALSE)

Include the lines of the linear models, per cyl.

Notegeom_smooth() will automatically draw a line per cyl subset. It recognizes the groups you want to identify by color in the aes() call within the ggplot() command.

ggplot(mtcars, aes(x = wt, y = mpg, col = cyl)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE) +
geom_smooth(aes(group = 1), method = "lm", se = FALSE, linetype = 2)

Include also a lm for the entire dataset in its whole

Note: the group=1 aesthetic will tell ggplot() to draw a single linear model through all the points.

mtcars$cyl <- as.factor(mtcars$cyl)
ggplot(mtcars, aes(x = wt, y = mpg, col = cyl))
+ geom_point()
+ geom_smooth(method='lm', se=FALSE)

Include the lines of the linear models, per cyl.

Notegeom_smooth() will automatically draw a line per cyl subset. It recognizes the groups you want to identify by color in the aes() call within the ggplot() command.

ggplot(mtcars, aes(x = wt, y = mpg, col = cyl)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE) +
geom_smooth(aes(group = 1), method = "lm", se = FALSE, linetype = 2)

Include also a lm for the entire dataset in its whole

Note: the group=1 aesthetic will tell ggplot() to draw a single linear model through all the points.

iris.tidy <- iris %>%
  gather(key, Value, -Species) %>%
  separate(key, c("Part", "Measure"), "\\.")
iris$Flower <- 1:nrow(iris)
iris.wide <- iris %>%
  gather(key, value, -Species, -Flower) %>%
  separate(key, c("Part", "Measure"), "\\.") %>%
  spread(Measure, value)

Gittering to avoid overplotting

Width of the jitter is a bit too wide to be useful.
Setting the position argument here to position_jitter(0.1), which will set the jittering width directly inside a points layer.

ggplot(mtcars, aes(x = cyl, y = wt)) + geom_jitter(width=0.1)

ggplot(mtcars, aes(x = cyl, y = wt)) +
  geom_point(position = position_jitter(0.1))

Note:  jitter can be

  • an argument in geom_point(position = ‘jitter’)
  • a geom itself, geom_jitter()
  • a position function, position_jitter(0.1)
ggplot(Vocab, aes(education, vocabulary)) + geom_point()
ggplot(Vocab, aes(education, vocabulary)) + geom_jitter()
ggplot(Vocab, aes(education, vocabulary)) + geom_jitter(alpha=0.2)
ggplot(Vocab, aes(education, vocabulary)) + geom_jitter(alpha=0.2, shape=1)

 Note: Jittering and alpha blending serves as a great solution to the overplotting problem here. Setting the shape to 1 didn’t really help, but it was useful in the previous exercises when you had less data.

  • se : logical value. If TRUE, confidence interval is displayed around smooth.