cssearcy / AYS-R-Coding-SPR-2020

Coding in R for Policy Analytics
https://cssearcy.github.io/AYS-R-Coding-SPR-2020/
3 stars 3 forks source link

HW3 issues with labeling (points() and text() functions) #46

Open Cam-Hoff opened 2 years ago

Cam-Hoff commented 2 years ago

Im having a lot of difficulty with parts 7 and 8 of the checklist for the homework (Emphasize averages in 1924 and 2012. Annotate plot with historical events). I get how the functions for text and points work, but I'm having issues getting the proper x,y coordinates, i think (this is hard to tell as I'm not getting error messages, the text or extra points just doesn't appear on the plot). I have tried a multitude of ways, but the current iteration looks like the below.

this.1924 <- league.ave.cut[year == 1924] 
this.2012 <- league.ave.cut[year == 2012] 

points(league.ave[this.1924], league.ave[this.1924],
        col="firebrick", cex=2, lwd=2 )
text(this.1924, labels = "test")

Any guidance? @jamisoncrawford

PS. For some reason I couldn't add labels or assignees? Clicking either of the option to try didn't result in the expected pop-ups from the guide.

jamisoncrawford commented 2 years ago

Hi @Cam-Hoff - don't worry about the dropdown labels or interactivity. That will come in Lab 05.

Without knowing what league.year.cut looks like, it's hard to tell whether and how your code works.

One thing I'm noticing - it looks like you are passing league.ave[this.1924] to both x and y - the everage needs to be passed to y and the year value to x. That immediately comes to mind - can you switch this up?

Cam-Hoff commented 2 years ago

Sorry about that. here is that variable

league.ave.cut <- league.ave[league.year > 1900 & league.year < 2012]
league.year.cut <- league.year[league.year > 1900 & league.year < 2012]

Ill try working on your suggestion now as well.

jamisoncrawford commented 2 years ago

@Cam-Hoff and I discussed this during office hours but for everyone's understanding, there were a few ways of resolving this - the troublesome part being the year value is tucked away as "names" for each value. One such way is simply finding the element name by using function names() and indexing the position with which().

which(names(league.ave) == "2012")
league.ave[index]

Here's the console output.

> index <- which(names(league.ave) == "2012")
> league.ave[index]
    2012 
7.495062