00R Basic and Intro Flashcards
how to explicitly specify that u are using a particular function from a package
ggplot2::ggplot()…package::function().
How to install package ?
install.packages(“tidyverse”)
how to load package?
library(tidyverse)
which one has double quote? Loading or installing package?
Installing package
ggpplot
gplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy),)
To set an aesthetic manually, set the aesthetic by name as an argument of your geom function; i.e. it goes outside of aes(). You’ll need to pick a level that makes sense for that aesthetic:
geom_point(mapping = aes(x = displ, y = hwy), color = “blue”)
what is the first argument in ggplot??
data
which geom create scatter plot?
geom_point()
what is an aesthetics?
An aesthetic is a visual prop‐ erty of the objects in your plot. Aesthetics include things like the size, the shape, or the color of your points
Once you map an aesthetic, ggplot2 takes care of the rest. It selects a reasonable scale to use with the aesthetic, and it constructs a legend that explains the mapping between levels and values.
What does this does glimpse(mpeg)?
displays the type of each column
What happened when u see + and code does not execute?
Sometimes you’ll run the code and nothing happens. Check the left-hand of your console: if it’s a +, it means that R doesn’t think you’ve typed a complete expression and it’s waiting for you to finish it. In this case, it’s usually easy to start from scratch again by pressing ESCAPE to abort processing the current command.
“The simple graph has brought more information to the data analyst’s mind than any other device.”
— John Tukey
What is ggplot2?
R has several systems for making graphs, but ggplot2 is one of the most elegant and most versatile. ggplot2 implements the grammar of graphics, a coherent system for describing and building graphs. With ggplot2, you can do more faster by learning one system and applying it in many places
what is relationship between ggplot2 and tidyverse?
ggplot2, one of the core members of the tidyverse.
How to view all ur data set in R studio pane?
View(flights)
What are tibbles?
Tibbles are data frames, but slightly tweaked to work better in the tidyverse.
What is data frame?
A data frame is a rectangular collection of variables (in the columns) and observations (in the rows)
what is difference between ggplot2::mpg and mpg?
The first we explicitly call the data frame mpg and second we have already import the ‘tidyverse’ which ia a collection of packages including mpg
what is the graphing template ?
ggplot(data = ) +
(mapping = aes())
What is aesthetics ?
An aesthetic is a visual property of the objects in your plot. Aesthetics include things like the size, the shape, or the color of your points
in aesthetic , how do u use colour?
(If you prefer British English, like Hadley, you can use colour instead of color.)
what is scaling? what is relation with colour?
To map an aesthetic to a variable, associate the name of the aesthetic to the name of the variable inside aes(). ggplot2 will automatically assign a unique level of the aesthetic (here a unique color) to each unique value of the variable, a process known as scaling
aesthetic
For each aesthetic, you use aes() to associate the name of the aesthetic with a variable to display. The aes() function gathers together each of the aesthetic mappings used by a layer and passes them to the layer’s mapping argument. The syntax highlights a useful insight about x and y: the x and y locations of a point are themselves aesthetics, visual properties that you can map to variables to display information about the data.
can u select ads properties manually?
yes …ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy), color = “blue”). To set an aesthetic manually, set the aesthetic by name as an argument of your geom function; i.e. it goes outside of aes(). You’ll need to pick a level that makes sense for that aesthetic: 1. The name of a color as a character string. 2. The size of a point in mm. 3. The shape of a point as a number, as shown in Figure 3.1.
aes can be ?
colour , size , shape etc?
What does glimpse() does?
glimpse() displays the type of each column.
What happens if you map the same variable to multiple aesthetics?
n the above plot, hwy is mapped to both location on the y-axis and color, and displ is mapped to both location on the x-axis and size. The code works and produces a plot, even if it is a bad one. Mapping a single variable to multiple aesthetics is redundant. Because it is redundant information, in most cases avoid mapping a single variable to multiple aesthetics.
what are diff ways to use ggplot?
ggplot(mtcars, aes(wt, mpg)) +
geom_point(shape = 21, colour = “black”, fill = “white”, size = 5, stroke = 5)
ggplot(mpg, aes(x = displ, y = hwy, shape = cty)) +
geom_point()
What happens if you map an aesthetic to something other than a variable name, like aes(colour = displ < 5)? Note, you’ll also need to specify x and y.
Aesthetics can also be mapped to expressions like displ < 5. The ggplot() function behaves as if a temporary variable was added to the data with with values equal to the result of the expression. In this case, the result of displ < 5 is a logical variable which takes values of TRUE or FALSE
One common problem when creating ggplot2 graphics is to put the + in the wrong place:Where does it supposed to come?
it has to come at the end of the line, not the start. In other words, make sure you haven’t accidentally written code like this:
ggplot(data = mpg)
+ geom_point(mapping = aes(x = displ, y = hwy))
Facets
One way to add additional variables is with aesthetics. Another way, particularly useful for categorical variables, is to split your plot into facets, subplots that each display one subset of the data.
What is geom?
A geom is the geometrical object that a plot uses to represent data. People often describe plots by the type of geom that the plot uses. For example, bar charts use bar geoms, line charts use line geoms, boxplots use boxplot geoms, and so on. Scatterplots break the trend; they use the point geom.
Does each geom have same aesthetic and mapping?
Every geom function in ggplot2 takes a mapping argument. However, not every aesthetic works with every geom
how many geom ggplot2 provide?
ggplot2 provides over 30 geoms, and extension packages provide even more (see https://www.ggplot2-exts.org for a sampling). The best way to get a comprehensive overview is the ggplot2 cheatsheet, which you can find at http://rstudio.com/cheatsheets. To learn more about any single geom, use help: ?geom_smooth.
how to display multiple geom ?
To display multiple geoms in the same plot, add multiple geom functions to ggplot():
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy)) +
geom_smooth(mapping = aes(x = displ, y = hwy))
better one
ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) +
geom_point() +
geom_smooth()
If you place mappings in a geom function, ggplot2 will treat them as local mappings for the layer. It will use these mappings to extend or overwrite the global mappings for that layer only. This makes it possible to display different aesthetics in different layers.
ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) +
geom_point(mapping = aes(color = class)) +
geom_smooth()
Diff kind of geom
line chart: geom_line()
boxplot: geom_boxplot()
histogram: geom_hist()
area chart: geom_area()
What does show.legend = FALSE do? What happens if you remove it? Why do you think I used it earlier in the chapter?
The theme option show.legend = FALSE hides the legend box.
Many graphs, like scatterplots, plot the raw values of your dataset. Other graphs, like bar charts, calculate new values to plot:
bar charts, histograms, and frequency polygons bin your data and then plot bin counts, the number of points that fall in each bin.
smoothers fit a model to your data and then plot predictions from the model.
boxplots compute a robust summary of the distribution and then display a specially formatted box.