Classwork 9

Add Labels and Make Notes on ggplot

Author

Byeong-Hak Choe

Published

March 25, 2026

Modified

March 25, 2026

Part 1. Electric Vehicles in Washington State

ev <- read_csv("https://bcdanl.github.io/data/electric_vehicles_WA.csv")

ev |> 
  paged_table()
  • county: County where the vehicle is registered.
  • state: State where the vehicle is registered.
  • postal_code: ZIP code of the vehicle registration location.
  • model_year: Model year of the vehicle.
  • make: Manufacturer brand of the vehicle.
  • model: Specific model name of the vehicle.
  • electric_vehicle_type: Type of electric vehicle, such as BEV or PHEV.
    • BEV (Battery Electric Vehicle): A fully electric vehicle powered only by a battery and electric motor.
    • PHEV (Plug-in Hybrid Electric Vehicle): A vehicle that has both a battery-powered electric motor and a gasoline engine.
  • clean_alternative_fuel_vehicle_cafv_eligibility: Whether the vehicle is eligible for Clean Alternative Fuel Vehicle (CAFV) programs.
  • electric_range: Estimated number of miles the vehicle can travel on electric power.
  • base_msrp: Base manufacturer’s suggested retail price (MSRP) of the vehicle in U.S. dollars.
  • vehicle_location: Geographic location of the vehicle, recorded as a point with longitude and latitude.


Question 1. Data Transformation

Create summary datasets:

  • Top 10 counties by number of EVs
  • Top 10 makes by number of EVs
  • Average electric range for makes with at least 500 vehicles
# write your code here


Question 2. Basic geom_text() on a scatterplot

Create a scatterplot using range_by_make with:

  • x = n_make
  • y = avg_range

Then add text labels showing the make name using geom_text().

# write your code here


Question 3. Improve label placement in geom_text()

Modify your plot from Question 2 and improve readability by changing some arguments inside geom_text().

Tasks

  • Try vjust
  • Try hjust
  • Try size
  • Try color
# write your code here


Question 4. Use geom_text_repel()

Recreate the scatterplot from Question 2, but use geom_text_repel() instead of geom_text().

Tasks

Use at least these arguments:

  • size
  • box.padding
  • max.overlaps
# write your code here


Question 5. Add a custom note with annotate()

Using the range_by_make scatterplot, add a custom annotation with annotate() that points out the make with the highest average electric range.

Tasks

  • Add a short text note such as "Highest average range"
  • Place the annotation near the relevant point
  • Also, add a segment or arrow.
# write your code here


Question 6. geom_col() with numbers on top of bars

Use county_counts to create a bar chart with:

  • EV counts on the x-axis
  • counties on the y-axis
  • bars created with geom_col()

Then add the count values on top of the bars using geom_text().

Tasks

  • reorder counties by count
  • place the numbers slightly above the bars
# write your code here


Question 7. geom_bar() with numbers that match bar heights

Use the original ev data to create a bar chart of electric_vehicle_type with geom_bar().

Then add labels that show the number of vehicles in each category.

Tasks

  • build the bars with geom_bar()
  • add count labels on top of the bars
  • make sure the numbers correspond to the bar heights
# write your code here


Question 8. Highlight one bar with annotate()

Using your county bar chart from Question 6, add an annotation that highlights the county with the largest number of EVs.

# write your code here



Discussion

Welcome to our Classwork 9 Discussion Board! 👋

This space is designed for you to engage with your classmates about the material covered in Classwork 9.

Whether you are looking to delve deeper into the content, share insights, or have questions about the content, this is the perfect place for you.

If you have any specific questions for Byeong-Hak (@bcdanl) regarding the Classwork 9 materials or need clarification on any points, don’t hesitate to ask here.

All comments will be stored here.

Let’s collaborate and learn from each other!

Back to top