R For Data Science | by: Wickham & Grolemund
Chapter 2 | Workflow Basics
Read the following line of code as:
–> # Object name “gets” value
object_name <- value
The arrow in this case is an assignment.
You will make lots of assignments, and <- is a pain to
type. You can save time with RStudio’s keyboard shortcut: Alt + - (the
minus sign).
# alt and - at same time makes arrow like this:
hi<-2
my<-3
alt_minus<-4
seq(1, 10, 2)
## [1] 1 3 5 7 9
seq(1, 10)
## [1] 1 2 3 4 5 6 7 8 9 10
seq(0, 10, 2)
## [1] 0 2 4 6 8 10
x <- "hello"
my_variable <- 10
my_variable
## [1] 10
This code did not work because my_variable was spelled wrong.
libary(todyverse)
mpg |>
ggplot()+
geom_point(aes(displ, hwy))
I could not get geom_smooth() to work here. IDK why.
my_bar_plot <- ggplot(mpg, aes(x = class)) +
geom_bar()
my_scatter_plot <- ggplot(mpg, aes(x = cty, y = hwy)) +
geom_point()
ggsave(filename = "mpg-plot.png", plot = my_bar_plot)
## Saving 7 x 5 in image
Because you have now assigned the plot to a name and used that name within the ggsave function. You are now being more specific.
2.2 Comments
Anytime you change the default of a code, i.e. you change the binwidth for a histogram – state why you did that as a
#comment.