R syntax

What do you mean “assign”?

What do you mean “assign”?

  • name <- value or name = value

  • Using <- or = we assign a value to a name and store the name in Environment.

  • By calling name we can use the value repeatedly without typing it every time we use it.

For example, in the following code chunk, having assigned c(1,2,3) to x makes the code more neat and tidy to get the same result.

c(1,2,3)^2 + 2*c(1,2,3) - sqrt(c(1,2,3))
## [1]  2.000000  6.585786 13.267949
x <- c(1,2,3)
x^2 + 2*x - sqrt(x)
## [1]  2.000000  6.585786 13.267949

Why is ggplot(data = mydata, mapping = aes(x = [x-variable], y = [y-variable])) not enough to draw a plot?

Why is ggplot() not enough to draw a plot?

The function ggplot() is simply to tell R to prepare a canvas for a plot. It is a declaration that you will draw a plot. What you will draw on the canvas should be specified by geom_xxxx() functions, e.g. geom_point for scatterplots, geom_bar for bar plots, etc.

You might think “…why? I even specified mapping variables there!” Yes, I hear you. But, that is again a part of preparation for drawing a plot. We specify mapping variables in ggplot() so that it applies to all subsequent geom_xxxx() functions globally.

I am confused about when to use %>% vs. +.

I am confused about when to use %>% vs. +.

  • Are you manipulating data by creating a new variable, filtering rows, arranging rows, selecting columns, etc.? Then use %>%.

  • Are you working on plots? Use +.

Remember, plotting is all about adding components of a graphic layer by layer.