DS4PS / cpp-526-fall-2019

Course material for CPP 526 Foundations of Data Science I
http://ds4ps.org/cpp-526-fall-2019
4 stars 4 forks source link

Lab 03 #17

Open jmacost5 opened 5 years ago

jmacost5 commented 5 years ago

I am trying to do what it says to do in part/question 7 I am getting this error>

library(Lahman)
Warning messages:
1: In doTryCatch(return(expr), name, parentenv, handler) :
  "text" is not a graphical parameter
2: In doTryCatch(return(expr), name, parentenv, handler) :
  "text" is not a graphical parameter
3: In doTryCatch(return(expr), name, parentenv, handler) :
  "text" is not a graphical parameter
4: In doTryCatch(return(expr), name, parentenv, handler) :
  "text" is not a graphical parameter

This is my code:

ave.so <- Teams$SO / Teams$G
year <- Teams$yearID
plot.new()
plot.window(xlim=c(1900,2025) , ylim=c(ave.so.min,ave.so.max))
points(year, ave.so, col=gray( level=.8 , alpha=0.4), pch=20, cex=2)
axis( side=1 )
abline( h=c(0,1,2,3,4,5,6,7,8,9), lty=2, col="gray" )
points( league.year, league.ave, type="b", pch=20, col="blue" )
text( 1945, 3.5, text="Players return from World War II \nfrom 1963 to 1968." )
text( 1950, 3.9, text="U.S. enters World War 1 \nfrom 1963 to 1968." )
title( main="Strike outs per page per team by batter" )
lecy commented 5 years ago

You are using the argument text= in the text() function when it should be labels=.

ave.so <- Teams$SO / Teams$G
year <- Teams$yearID

ave.so.min <- min( ave.so, na.rm=T )
ave.so.max <- max( ave.so, na.rm=T )

plot.new()
plot.window(xlim=c(1900,2025) , ylim=c(ave.so.min,ave.so.max))
points(year, ave.so, col=gray( level=.8 , alpha=0.4), pch=20, cex=2)
axis( side=1 )
abline( h=c(0,1,2,3,4,5,6,7,8,9), lty=2, col="gray" )
points( league.year, league.ave, type="b", pch=20, col="blue" )
text( 1945, 3.5, labels="Players return from World War II \nfrom 1963 to 1968." )
text( 1950, 3.9, labels="U.S. enters World War 1 \nfrom 1963 to 1968." )
title( main="Strike outs per page per team by batter" )
jmacost5 commented 5 years ago

This is my code below, it was originally not giving me any error messages and before it was giving me a graph, I made sure the packages were installed but I guess I am not doing this right.

ave.so <- Teams$SO / Teams$G
year <- Teams$yearID
plot.new()
plot.window(xlim=c(1900,2025) , ylim=c(ave.so.min,ave.so.max))
points(year, ave.so, col=gray( level=.8 , alpha=0.4), pch=20, cex=2)
axis( side=1 )
abline( h=c(0,1,2,3,4,5,6,7,8,9), lty=2, col="gray" )
points( league.year, league.ave, type="b", pch=20, col="blue" )
text( 1945, 3.5, text="Players return from World War II lables= 1963 to 1968." )
text( 1950, 3.9, text="U.S. enters World War 1 lables= 1963 to 1968." )
title( main="Strike outs per page per team by batter" )
Warning messages:
1: In doTryCatch(return(expr), name, parentenv, handler) :
  "text" is not a graphical parameter
2: In doTryCatch(return(expr), name, parentenv, handler) :
  "text" is not a graphical parameter
3: In doTryCatch(return(expr), name, parentenv, handler) :
  "text" is not a graphical parameter
4: In doTryCatch(return(expr), name, parentenv, handler) :
  "text" is not a graphical parameter
Error in plot.window(xlim = c(1900, 2025), ylim = c(ave.so.min, ave.so.max)) : 
  object 'ave.so.min' not foundError in points(league.year, league.ave, type = "b", pch = 20, col = "blue") : 
  object 'league.year' not foundWarning message:
In text.default(1945, 3.5, text = "Players return from World War II lables= 1963 to 1968.") :
  "text" is not a graphical parameterWarning message:
In text.default(1950, 3.9, text = "U.S. enters World War 1 lables= 1963 to 1968.") :
  "text" is not a graphical parameter
lecy commented 5 years ago

I would think about it as a data creation step. The data is organized at the team level and by seasons (years), so you need ave strike-outs per game for each team, as well as a league average. You were missing the league average.

You also need to calculate your range for Y (strikeouts) for your plot.window() function.

library( Lahman )

ave.so <- Teams$SO / Teams$G
year <- Teams$yearID

# league average
league.ave <- tapply( X=ave.so, INDEX=as.factor(year), FUN="mean", na.rm=T )
league.year <- as.numeric( names( league.ave ) )

ave.so.min <- min( ave.so, na.rm=T )
ave.so.max <- max( ave.so, na.rm=T )

Then you can call your graphs. See above - you are still using text= as an argument, when it's the function name. It should be labels=.

plot.new()
plot.window(xlim=c(1900,2025) , ylim=c(ave.so.min,ave.so.max))
points(year, ave.so, col=gray( level=.8 , alpha=0.4), pch=20, cex=2)
axis( side=1 )
abline( h=c(0,1,2,3,4,5,6,7,8,9), lty=2, col="gray" )
points( league.year, league.ave, type="b", pch=20, col="blue" )
text( 1945, 3.5, labels="Players return from World War II 1963 to 1968." )
text( 1950, 3.9, labels="U.S. enters World War 1 1963 to 1968." )
title( main="Strike outs per page per team by batter" )
jmacost5 commented 5 years ago

So the code below should be putting the labels on the graph and it is not. Also is there a way to have a line connecting the trend line that is made by the graph, is there a way to have the numbers on the y axis without axis( side=2 ) . because it gives the line on the side, and is there a way to make the numbers go down by 10 instead of by 20 on the x axis? text( 1945, 3.5, labels="Players return from World War II 1963 to 1968." ) text( 1950, 3.9, labels="U.S. enters World War 1 1963 to 1968." )

lecy commented 5 years ago

Those labels work on my computer.

is there a way to have a line connecting the trend line that is made by the graph

segments( x0=1915, y0=1, y1=league.ave[ league.year == 1915 ] )

is there a way to have the numbers on the y axis without axis( side=2 ). Try:

text( 2020, c(1,2,3,4,5), c(1,2,3,4,5), pos=3 )

is there a way to make the numbers go down by 10 instead of by 20 on the x axis

axis( side=1, at=c(1920,1930,1940) )
jmacost5 commented 5 years ago
segments( x0=1915, y0=1, y1=league.ave[ league.year == 1915 ] )
labels( 1915, 4.9, \nlabels="Players return from World War II" )
segments( x0=1945, y0=1, y1=league.ave[ league.year == 1945 ] )
labels( 1945, 3.5, \nlabels="U.S. enters World War 1" )

I am still not getting the label on the graph, and the trend line is not popping up. I am not understanding how to get the trend line.

lecy commented 5 years ago

This is what I'm getting from your code.

image

Are you creating the league average anywhere in your code? See step 6.

# create the league average vector
league.ave <- tapply( X=ave.so, INDEX=as.factor(year), FUN="mean", na.rm=T )
league.year <- as.numeric( names( league.ave ) )

You are still using the text() function incorrectly. The function is text() and the argument is labels=.

segments( x0=1915, y0=1, y1=league.ave[ league.year == 1915 ] )
text( 1915, 1, labels="Players return from \nWorld War II", pos=1 )
segments( x0=1945, y0=1, y1=league.ave[ league.year == 1945 ] )
text( 1945, 1, labels="U.S. enters \nWorld War 1", pos=1 )
jmacost5 commented 5 years ago

So I got the text to finally show up, I thought I was reading your things correctly but I guess I was not I am sorry I was making you answer the same question a bunch. Is there not a way to make a trend line then?

castower commented 5 years ago

To make a line, I used the following code:

text( 1973, 1.5, "Designated hitter \nrule took effect in 1973.", col="gray47" ) 

lines(x=c(1973,1973), y=c(1.75, 4.8), col="gray")
jmacost5 commented 5 years ago

I think I am about to sound so dumb because I do not know what it is called, but I mean a way to connect the blue dots together like in the image.

castower commented 5 years ago

Oh! Got it, add type="o" to your blue points and it adds a line to connect them.

lecy commented 5 years ago

Thanks @castower!