DS4PS / ays-r-coding-sum-2022

Introductory data science course in R, taught at the Andrew Young School GSU.
http://ds4ps.org/ays-r-coding-sum-2022/
2 stars 2 forks source link

Lab4 questions #4

Open kyarosiuk1 opened 2 years ago

kyarosiuk1 commented 2 years ago

Hello, in Lab 4 new version of Lab4, are we making dynamic the line that shows the average for each Region? There are so many lines in the code you provided that makes it confusing...

  1. Can you please confirm that the below code (the line) is the one that needs to be dynamic? geom_line(data = region_avgs, size = 1, aes(x = Year, y = Average, group = Region), alpha = 0.4, color = "grey20")

  2. Can you please clarify what should go into renderPlot ()? Do we include only the dynamic portion of the chart or do we include all the lines of the plot: including year_avg lines/points, etc.?

  3. Can you please help me understand the below? I am struggling to understand why we would show the exact same line on the chart in white color and then again in blue? geom_line(data = year_avg, size = 1.5, aes(x = Year, y = Average), color = "white") +

    geom_line(data = year_avg,
              size = 1,
              aes(x = Year,
                  y = Average),
              alpha = 0.6,
              color = "dodgerblue")
  4. Are we supposed to add a legend in this assignment? the assignment does not ask for it but I see one on the example we are replicating...

thank you so much for your help!

jamisoncrawford commented 2 years ago
  1. This code creates the trend lines for each region's average. The dynamic code you create must simply take one of these trend lines and highlight it to make it distinct from the rest of the other group average trendlines.
  2. You should include in the renderPlot() function whatever code is necessary to recreate the graphic, including the base/foundational code and the new, dynamic code which you've added.
  3. I created a thicker white line that underlays the trend data as design decision. The halo effect helps the data pop and makes the trendline compete less with otherwise obfuscating contextual trend lines for each region. Perhaps you can find a more elegant solution for creating the halo!
  4. Yes, don't forget your legend!
kyarosiuk1 commented 2 years ago

Thank you, your answers are helpful, but I am still struggling...any hints or help on how I should think on making the line specific to each region only? I tried to modify per below, but it does not work... geom_line(data = region_avgs, size = 1, aes(x = Year, y = Average, group = input$Region)

Also, I am having issue with the legend... I tried both codes below, but my legend is still not showing on the graph

legend() I tried to use legend function

or

scale_color_manual(name="legend", breaks=c('Region', 'Global Average'), values=c('Region'='yellow', 'Global Average'='blue')

jamisoncrawford commented 2 years ago

@kyarosiuk1 sure thing! So what you need to do is "add on" another ggplot2 element.

Say your plot is in an object called p. You can add a new graphical layer with the following approach:

p + my_new_layer

In our case:

p + geom_line()

Now, you can actually change the data source for this "geometry layer":

p + geom_line(data = region_avgs, aes(x = Year, y = Average), color = "red", size = 3)

I don't recall the specific code or variable names, but it would look like this.


Now - how do we make it so that line ONLY displays for the selected region? Well, simply filter it using the shiny input id:

region_subset <- region_avgs[region_avgs$Region == input$my_region, ]

Now repeat with your new geometry layer:

p + geom_line(data = region_subset , aes(x = Year, y = Average), color = "red", size = 3)

This should get you over the finish line!