0% found this document useful (0 votes)
20 views5 pages

R

The document discusses various exercises involving using different geoms and statistical transformations in ggplot2 to visualize data. It covers creating scatter plots, adding regression lines, faceting plots, adjusting position of geoms to avoid overplotting, and using different coordinate systems.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views5 pages

R

The document discusses various exercises involving using different geoms and statistical transformations in ggplot2 to visualize data. It covers creating scatter plots, adding regression lines, faceting plots, adjusting position of geoms to avoid overplotting, and using different coordinate systems.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

CHAPTER 1

EXERCISE 1:/32
1. Having run ggplot(data = mpg), I see a blank map.
2. There are 32 observations and 11 numeric variables.
3. The drv variable describe the type of drive train, where f = front-wheel
drive, r = rear wheel drive, 4 = 4wd.
4. ggplot(data = mpg) + geom_point(mapping = aes(x = hwy, y = cyl))
5. Having made a scatterplot of class versus drv, you can see 2 variables are
independent. The plot is not useful because both of them are categorical.

EXERCISE 2:/38
1. This code should be ggplot(data = mpg) + geom_point(mapping = aes(x =
displ, y = hwy), color = “blue”)
The points are not blue because in the given code: color = “blue” means a
color aesthetic containing a value named blue.

2. In pmg, the variables that have <chr> below are categorical and the variables
that have <dbl> and <int> below are continuous.

3. Categorical variable
ggplot(data = mpg) + geom_point(mapping = aes(x = trans, y =
manufacturer, color = fl))
ggplot(data = mpg) + geom_point(mapping = aes(x = trans, y =
manufacturer, size = fl))
ggplot(data = mpg) + geom_point(mapping = aes(x = trans, y =
manufacturer, shape = fl))
Continuous variable
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = cty, color=
year))
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = cty, size=
year))
A continuous variable cannot be mapped to the shape aesthetic.
4. If you map the same variable to multiple aesthetics, you still draw a plot.
5. The stroke aesthetic defines the size of the border, of shapes.
6. If you map an aesthetic to something other than a variable name, it will
become a logical variable.

EXERCISE 3:/42
1. Some continuous variables will be changed into categorical variables.
ggplot(mpg, aes(x = displ, y = hwy)) + geom_point() + facet_grid(~ cty)
2. The empty cells in a plot does not mean it will create a plot.
If you separate facet…. gplot, you have a plot with code
3. The first plot only has the x-axis and the second one only has a y-axis.
4. The advantages to using faceting instead of the color aesthetic are:
- We can easily analyze the plot without remembering the colors of each variable.
The disadvantages are:
-  It is difficult to compare the values because of the different plots.
The balance change if you had a larger dataset is using faceting because you can
analyze the plot without remembering more colors.

5.
- nrow: number of rows
- ncol: number of columns
facets, nrow, ncol, scales, schrink, labeller, as.table, switch, drop, dir,
strip.positioncontrol the layout of the individual panels.
Còn 1 ý

6.When using facet_grid() you should usually put the variable with more unique
levels in the columns since there will more space for columns horizontally.

EXERCISE 4:/46
1. geom_line(): draw a line chart, geom_boxplot(): draw a boxplot,
geom_histogram(): draw a histogram, geom_area(): draw an area chart
2.

3. It will hide the legend board. If I remove it, the legend box will appear =>
show.legend = TRUE.
4. se argument will add grey areas around the lines, which are called standard error
band.
5. These two graphs won’t look different because they have the same data and
mapping from ggplot.
6. ggplot(mpg, aes(x = displ, y = hwy)) + geom_point() +
geom_smooth(se=FALSE)
ggplot(mpg, aes(x = displ, y = hwy)) + geom_point() + geom_smooth(se=FALSE,
aes(group = drv))
ggplot(mpg, aes(x = displ, y = hwy, color = drv)) + geom_point() +
geom_smooth(se=FALSE, aes(group = drv))
ggplot(mpg, aes(x = displ, y = hwy)) + geom_point(aes(color = drv)) +
geom_smooth(se=FALSE)
ggplot(mpg, aes(x = displ, y = hwy)) + geom_point(aes(color = drv)) +
geom_smooth(se=FALSE, aes(group = drv, linetype= drv))
ggplot(mpg, aes(x = displ, y = hwy)) + geom_point(size=4, color="white") +
geom_point(aes(color = drv))
EXERCISE 5:/52
1. The default geom associated with stat_summary() is geom_pointrange
ggplot(data = diamonds) + geom_pointrange(mapping = aes(x = cut, y =
depth), fun.ymin = min, fun.ymax = max, fun.y = median)
2. If you want the heights of the bars to represent values in the data,
use geom_col() instead. – stat_identity
If you want the heights of the bars to represent values in the data,
use geom_bar() instead. – stat_count

3. Use stat_smooth() if you want to display the results with a non-standard


geom.
4. ggplot(data = diamonds) + geom_bar(mapping = aes(x = cut, y = ..prop..,
group = 1))

EXERCISE 6:/57
1. This plot is overplotting. I use geom_jitter to avoid it.

2. width, : amount of vertical and horizontal jitter. The jitter is added in both
height positive and negative directions, so the total spread is twice the
value specified here.
3. Having compared and contrasted geom_jitter() with geom_count(), we can
see clearly that the plot with geom_count is overplotting.
4. The default position adjustment for geom_boxplot()? is dodge2.
ggplot(data = mpg, aes(x = displ, y = cty, color = class)) + geom_boxplot()
EXERCISE 7:/59
1.
2.
3. The difference between coord_quickmap() and coord_map() is:
- coord_quickmap(): 3D
- coord_map(): 2D
4.

You might also like