Watts-College / cpp-527-spr-2022

https://watts-college.github.io/cpp-527-spr-2022/
0 stars 1 forks source link

Final Project - Build Graph Error #38

Open mrowland1 opened 2 years ago

mrowland1 commented 2 years ago

Hi @Dselby86 , I pasted in the build graph code exactly as it is on the instructions and keep getting this error: Warning in max(t.salary$q50) : no non-missing arguments to max; returning -Inf Error in plot.window(xlim = c(40000 - 10000, xmax), ylim = c(0, ymax + : need finite 'xlim' values

Are we supposed to edit this code in some way or should it work as is? All my previous steps are working perfectly.

add_position <- function( t, position, y, xmax, scale.f=8 ) {

t.original <- t t <- filter( t.original, title==position ) dot.size <- 2 + scale.fsum(t$p) offset.n <- 1 + sum(t$p)2

male.median <- NA n.male <- NA t <- filter( t.original, title==position & gender == "male" ) if( nrow(t) > 0 ) { male.median <- t$q50 n.male <- t$n }

female.median <- NA n.female <- NA t <- filter( t.original, title==position & gender == "female" ) if( nrow(t) > 0 ) { female.median <- t$q50 n.female <- t$n }

dumbell plots

segments( x0=female.median, x1=male.median, y0=y, col=gray(0.3,0.5), lwd=7 ) points( male.median, y, col=adjustcolor( "darkblue", alpha.f = 0.5), pch=19, cex=dot.size ) points( female.median, y, col=adjustcolor( "firebrick", alpha.f = 0.5), pch=19, cex=dot.size )

pos.f <- 2 pos.m <- 4 if( ! ( is.na(female.median) | is.na(male.median) ) ) { pos.f <- ifelse( female.median > male.median, 4, 2 ) pos.m <- ifelse( female.median > male.median, 2, 4 ) }

add salaries to right and left

text( female.median, y, paste0("$",round(female.median/1000,0),"k"), col=adjustcolor( "firebrick", alpha.f = 0.7), cex=1.2, pos=pos.f, offset=offset.n ) text( male.median, y, paste0("$",round(male.median/1000,0),"k"), col=adjustcolor( "darkblue", alpha.f = 0.7), cex=1.2, pos=pos.m, offset=offset.n )

add faculty counts

n.female <- ifelse( is.na(n.female), 0, n.female ) n.female <- ifelse( nchar(n.female)==1, paste0( " ", n.female), n.female ) n.male <- ifelse( is.na(n.male), 0, n.male ) n.male <- ifelse( nchar(n.male)==1, paste0( " ", n.male), n.male ) text( xmax-0.1xmax, y+0.14, paste0( "f = ", n.female), col="gray50", cex=1.1, pos=4 ) text( xmax-0.1xmax, y-0.14, paste0( "m = ", n.male), col="gray50", cex=1.1, pos=4 )

axis( side=2, at=y, labels=position, las=2, tick=F, cex.axis=1.5, col.axis="gray50" ) }

build_graph <- function( t.salary, unit ) { unique.titles <- unique( t.salary$title ) ymax <- length(unique.titles) xmax <- round( max(t.salary$q50), -3 ) + 50000 color.key.pos <- 40000 + ( xmax - 40000 ) / 2 color.key.inc <- ( xmax - 40000 ) / 10

t.mf <- filter( t.salary, gender %in% c("male","female") ) N <- sum( t.mf$n )

par( mar=c(6,15,4.1,0) ) plot.new() plot.window( xlim=c(40000-10000,xmax), ylim=c(0,ymax+1) )

abline( v=seq(40000,xmax-40000,20000), lwd=1.5, lty=2, col=gray(0.5,0.5) ) axis( side=1, at=seq(40000,xmax-40000,20000), labels=paste0("$",seq(40,(xmax-40000)/1000,20),"k"), cex.axis=1.1, col.axis="gray40", tick=FALSE )

y <- ymax

if( "Full Professor" %in% unique.titles ) { add_position( t.salary, position="Full Professor", y, xmax ) y <- y-1 } if( "Associate Professor" %in% unique.titles ) { add_position( t.salary, position="Associate Professor", y, xmax ) y <- y-1 } if( "Assistant Professor" %in% unique.titles ) { add_position( t.salary, position="Assistant Professor", y, xmax ) y <- y-1 } if( "Teaching Faculty" %in% unique.titles ) { add_position( t.salary, position="Teaching Faculty", y, xmax ) y <- y-1 } if( "Researcher" %in% unique.titles ) { add_position( t.salary, position="Researcher", y, xmax ) y <- y-1 }

text( color.key.pos + 3color.key.inc, 0, "MALE", col=adjustcolor( "darkblue", alpha.f = 0.7), cex=1.2 ) text( color.key.pos + 1.8color.key.inc, 0, "FEMALE", col="firebrick", cex=1.2 ) text( xmax - 0.1*xmax, 0, paste0("N = ",N), col="gray40", cex=1.2, pos=4 )

title( main="Median Salary by Rank and Gender", cex.main=1.5, col.main="gray30" ) title( xlab=unit, col.lab="gray50", cex.lab=1.5, line=5 ) title( xlab="dot size represents proportion of faculty at that rank", col.lab="gray50", cex.lab=0.9 )

return(NULL) }

TEST:

t.salary is the salary table from the previous step

unit is a category from Department.Description

#

build_graph( t.salary, unit="Psychology" )

#

d %>%
filter( Department.Description == "Psychology" ) %>% create_salary_table() %>% build_graph( unit="Psychology" )

mmonakho commented 2 years ago

Hi @mrowland1 , were you able to figure this out?

Dselby86 commented 2 years ago

My guess is that this is related to the issues with t.salary you posted about previously.

According to the error statement:

Warning in max(t.salary$q50) :

no non-missing arguments to max; returning -Inf *This is returning negative infinity as a result because *either t.salary does not exist or q50 is not a column of t.salary.

Then when you you use xmax it is equal to -Infinity which is note a finite value.

hence this error:

Error in plot.window(xlim = c(40000 - 10000, xmax), ylim = c(0, ymax + :need finite 'xlim' values

Walk through the code line by line to see where the code is not working. Trying to comb through several pages of code is like trying to find a needle in a haystack.

Use the error message to narrow down where the error is occurring.

On Sat, Feb 26, 2022 at 5:23 PM mrowland1 @.***> wrote:

Hi @Dselby86 https://github.com/Dselby86 , I pasted in the build graph code exactly as it is on the instructions and keep getting this error:

Warning in max(t.salary$q50) : no non-missing arguments to max; returning -Inf Error in plot.window(xlim = c(40000 - 10000, xmax), ylim = c(0, ymax + : need finite 'xlim' values

Are we supposed to edit this code in some way or should it work as is? All my previous steps are working perfectly.

add_position <- function( t, position, y, xmax, scale.f=8 ) {

t.original <- t t <- filter( t.original, title==position ) dot.size <- 2 + scale.fsum(t$p) offset.n <- 1 + sum(t$p)2

male.median <- NA n.male <- NA t <- filter( t.original, title==position & gender == "male" ) if( nrow(t) > 0 ) { male.median <- t$q50 n.male <- t$n }

female.median <- NA n.female <- NA t <- filter( t.original, title==position & gender == "female" ) if( nrow(t) > 0 ) { female.median <- t$q50 n.female <- t$n } dumbell plots

segments( x0=female.median, x1=male.median, y0=y, col=gray(0.3,0.5), lwd=7 ) points( male.median, y, col=adjustcolor( "darkblue", alpha.f = 0.5), pch=19, cex=dot.size ) points( female.median, y, col=adjustcolor( "firebrick", alpha.f = 0.5), pch=19, cex=dot.size )

pos.f <- 2 pos.m <- 4 if( ! ( is.na(female.median) | is.na(male.median) ) ) { pos.f <- ifelse( female.median > male.median, 4, 2 ) pos.m <- ifelse( female.median > male.median, 2, 4 ) } add salaries to right and left

text( female.median, y, paste0("$",round(female.median/1000,0),"k"), col=adjustcolor( "firebrick", alpha.f = 0.7), cex=1.2, pos=pos.f, offset=offset.n ) text( male.median, y, paste0("$",round(male.median/1000,0),"k"), col=adjustcolor( "darkblue", alpha.f = 0.7), cex=1.2, pos=pos.m, offset=offset.n ) add faculty counts

n.female <- ifelse( is.na(n.female), 0, n.female ) n.female <- ifelse( nchar(n.female)==1, paste0( " ", n.female), n.female ) n.male <- ifelse( is.na(n.male), 0, n.male ) n.male <- ifelse( nchar(n.male)==1, paste0( " ", n.male), n.male ) text( xmax-0.1

xmax, y+0.14, paste0( "f = ", n.female), col="gray50", cex=1.1, pos=4 ) text( xmax-0.1xmax, y-0.14, paste0( "m = ", n.male), col="gray50", cex=1.1, pos=4 )

axis( side=2, at=y, labels=position, las=2, tick=F, cex.axis=1.5, col.axis="gray50" ) }

build_graph <- function( t.salary, unit ) { unique.titles <- unique( t.salary$title ) ymax <- length(unique.titles) xmax <- round( max(t.salary$q50), -3 ) + 50000 color.key.pos <- 40000 + ( xmax - 40000 ) / 2 color.key.inc <- ( xmax - 40000 ) / 10

t.mf <- filter( t.salary, gender %in% c("male","female") ) N <- sum( t.mf$n )

par( mar=c(6,15,4.1,0) ) plot.new() plot.window( xlim=c(40000-10000,xmax), ylim=c(0,ymax+1) )

abline( v=seq(40000,xmax-40000,20000), lwd=1.5, lty=2, col=gray(0.5,0.5) ) axis( side=1, at=seq(40000,xmax-40000,20000), labels=paste0("$",seq(40,(xmax-40000)/1000,20),"k"), cex.axis=1.1, col.axis="gray40", tick=FALSE )

y <- ymax

if( "Full Professor" %in% unique.titles ) { add_position( t.salary, position="Full Professor", y, xmax ) y <- y-1 } if( "Associate Professor" %in% unique.titles ) { add_position( t.salary, position="Associate Professor", y, xmax ) y <- y-1 } if( "Assistant Professor" %in% unique.titles ) { add_position( t.salary, position="Assistant Professor", y, xmax ) y <- y-1 } if( "Teaching Faculty" %in% unique.titles ) { add_position( t.salary, position="Teaching Faculty", y, xmax ) y <- y-1 } if( "Researcher" %in% unique.titles ) { add_position( t.salary, position="Researcher", y, xmax ) y <- y-1 }

text( color.key.pos + 3

color.key.inc, 0, "MALE", col=adjustcolor( "darkblue", alpha.f = 0.7), cex=1.2 ) text( color.key.pos + 1.8color.key.inc, 0, "FEMALE", col="firebrick", cex=1.2 ) text( xmax - 0.1*xmax, 0, paste0("N = ",N), col="gray40", cex=1.2, pos=4 )

title( main="Median Salary by Rank and Gender", cex.main=1.5, col.main="gray30" ) title( xlab=unit, col.lab="gray50", cex.lab=1.5, line=5 ) title( xlab="dot size represents proportion of faculty at that rank", col.lab="gray50", cex.lab=0.9 )

return(NULL) } TEST: t.salary is the salary table from the previous step unit is a category from Department.Description build_graph( t.salary, unit="Psychology" )

d %>% filter( Department.Description == "Psychology" ) %>% create_salary_table() %>% build_graph( unit="Psychology" )

— Reply to this email directly, view it on GitHub https://github.com/Watts-College/cpp-527-spr-2022/issues/38, or unsubscribe https://github.com/notifications/unsubscribe-auth/AB4EHB7B37O7P3KVUGOCYJ3U5FVJBANCNFSM5PN45CZQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you were mentioned.Message ID: @.***>