📖 Scenario: You are working with a simple map of a city park. The park has several landmarks, and you want to store their positions using coordinates.
🎯 Goal: Create a list of landmarks with their x and y coordinates, set a reference point, calculate the distance of each landmark from the reference point, and print the distances.
📋 What You'll Learn
Create a list called landmarks with named vectors for each landmark's x and y coordinates
Create a variable called reference_point as a named vector with x and y coordinates
Calculate the Euclidean distance from each landmark to the reference_point using a for loop and store results in a vector called distances
Print the distances vector
💡 Why This Matters
🌍 Real World
Coordinate systems help us locate places on maps, in games, or in real life, like finding landmarks in a park.
💼 Career
Understanding coordinates and distance calculations is useful in fields like geography, game development, robotics, and data visualization.
Progress0 / 4 steps
1
Create landmarks with coordinates
Create a list called landmarks with these exact entries: Fountain = c(x = 3, y = 4), Bench = c(x = 7, y = 1), Playground = c(x = 2, y = 8)
R Programming
Hint
Use list() to create a list and c(x = value, y = value) for coordinates.
2
Set the reference point coordinates
Create a variable called reference_point as a named vector with x = 0 and y = 0
R Programming
Hint
Use c(x = 0, y = 0) to create the reference point vector.
3
Calculate distances from reference point
Create a numeric vector called distances and use a for loop with variable name to calculate the Euclidean distance from each landmark in landmarks to reference_point. Store each distance in distances with the same names as landmarks.
R Programming
Hint
Use sqrt(dx^2 + dy^2) to calculate distance and assign it to distances[name].
4
Print the distances
Write a print() statement to display the distances vector
R Programming
Hint
Use print(distances) to show the distances.
Practice
(1/5)
1. Which function in R's ggplot2 package is used to flip the x and y axes on a plot?
easy
A. coord_flip()
B. coord_cartesian()
C. coord_polar()
D. coord_fixed()
Solution
Step 1: Understand coordinate functions in ggplot2
The ggplot2 package provides several coordinate functions to control plot appearance.
Step 2: Identify the function that flips axes
coord_flip() swaps the x and y axes, effectively flipping the plot.
Final Answer:
coord_flip() -> Option A
Quick Check:
Flip axes = coord_flip() [OK]
Hint: Flip axes with coord_flip() in ggplot2 [OK]
Common Mistakes:
Confusing coord_flip() with coord_cartesian()
Using coord_polar() which changes to polar coordinates
Thinking coord_fixed() flips axes
2. Which of the following is the correct syntax to set fixed aspect ratio in ggplot2?
easy
A. coord_fixed_ratio(1)
B. coord_fixed(scale = 1)
C. coord_fixed(aspect = 1)
D. coord_fixed(ratio = 1)
Solution
Step 1: Recall the function for fixed aspect ratio
coord_fixed() sets a fixed aspect ratio between x and y axes.
Step 2: Check correct argument name
The argument to specify ratio is ratio, so coord_fixed(ratio = 1) is correct syntax.
Final Answer:
coord_fixed(ratio = 1) -> Option D
Quick Check:
Fixed ratio uses ratio= in coord_fixed() [OK]
Hint: Use ratio= argument in coord_fixed() for aspect ratio [OK]
Common Mistakes:
Omitting argument name and just passing 1
Using aspect= instead of ratio=
Using a non-existent function coord_fixed_ratio()
3. What will be the effect of this code snippet in R using ggplot2?
D. coord_flip() should be replaced with coord_cartesian()
Solution
Step 1: Check arguments accepted by coord_flip()
coord_flip() flips axes but does not accept ratio argument.
Step 2: Identify correct way to set aspect ratio when flipping
Aspect ratio should be set using coord_fixed(), not inside coord_flip().
Final Answer:
coord_flip() does not accept ratio argument -> Option B
Quick Check:
coord_flip() has no ratio argument [OK]
Hint: coord_flip() does not accept ratio argument [OK]
Common Mistakes:
Trying to set ratio inside coord_flip()
Confusing coord_flip() with coord_cartesian()
Assuming geom_point() needs extra arguments
5. You want to create a pie chart in ggplot2 using coord_polar(). Which coordinate system transformation should you apply before coord_polar() to make the pie chart correctly represent proportions?
hard
A. Use geom_point() with coord_polar()
B. Use geom_bar() with stat = 'identity'
C. Use geom_bar() with default settings and map fill to factor variable
D. Use geom_line() with coord_polar()
Solution
Step 1: Understand pie chart creation in ggplot2
Pie charts are created by using bar charts with polar coordinates.
Step 2: Identify correct geom and mapping
Using geom_bar() with default settings and mapping fill to a factor variable creates bars representing counts, which coord_polar() converts to pie slices.
Final Answer:
Use geom_bar() with default settings and map fill to factor variable -> Option C
Quick Check:
Pie chart = geom_bar() + coord_polar() with fill mapped [OK]
Hint: Pie charts use geom_bar() + coord_polar() with fill mapped [OK]
Common Mistakes:
Using geom_point() or geom_line() which don't create bars
Setting stat='identity' unnecessarily for counts
Not mapping fill to a factor variable
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.