UCSBCarpentry / 2022-08-09-ucsb-r-geospatial

This is the repo for UCSB Lib's R Geospatial workshop on August 9 and 11, 2022
https://ucsbcarpentry.github.io/2022-08-09-ucsb-r-geospatial/
Other
0 stars 0 forks source link

Ep 7: HARV_lines$TYPE is not a factor #9

Open djhunter opened 2 years ago

djhunter commented 2 years ago

The discussion in the "Customize Plots" section of episode 7 is wrong because HARV_lines$TYPE is not a factor.

Suggestion, delete the following because it is confusing:

To create this vector we can use the following syntax: c("color_one", "color_two", "color_three")[object$factor] Note in the above example we have a vector of colors - one for each factor value (unique attribute value) the attribute itself ([object$factor]) of class factor.

First we will check how many unique levels our factor has: levels(HARV_lines$TYPE)

Replace with:

Convert the TYPE column to a factor and view its levels:

HARV_lines$TYPE <- factor(HARV_lines$TYPE)
levels(HARV_lines$TYPE)
djhunter commented 2 years ago

Similar issues exist in the solution to the final two challenges

djhunter commented 2 years ago

Add

HARV_lines$BicyclesHo <- factor(HARV_lines$BicyclesHo)

to the first challenge, and add

state_boundary_US$region <- factor(state_boundary_US$region)

to the second.

djhunter commented 2 years ago

Now I'm wondering if I should have used as.factor instead of factor in the above. (Matter of style; I think the result should be identical in this case.)

Alternatively, with the tidyverse, you could use as_factor, but then the levels will be in a different order. To control the ordering of the levels, you can use fct_relevel.