graph - R: Customize Slope of Curve in Plot -


i want graphically represent slope of line, line determined formula taking weighted average of set of points. weights based on external factors not represented.

i have aggregated graph x variable (price) on x-axis , dependent (quantity) on y. trying overlay graph set of lines representing steepness of change in quantity on price derived empirically tha not equivalent slope of curve.

the x axis should price; y should volume; , slopes of curve should elasticity. there way me in r?

elasticity change in quantity $1 change in price. it's different @ different price points.

df  category       price   volume_band     elasticity   alpha        $1      50,000          -0.5  beta         $2      100,000         -1  gamma        $3      200,000         -1.5  delta        $4      250,000         -2 

i still not sure, how graph should like. i'll give 2 variants, maybe provide mock-up?

possibility 1:

df <- read.table(text="category       price   volume_band     elasticity  alpha        $1      50,000          -0.5 beta         $2      100,000         -1  gamma        $3      200,000         -1.5  delta        $4      250,000         -2",header=true)  df$price <- as.numeric(gsub("\\$","",df$price)) df$volume_band <- as.numeric(gsub(",","",df$volume_band))  dx <- 0.2 dy <- dx*df$elasticity   df$xstart <- df$price-dx df$xend <- df$price+dx df$ystart <- df$volume_band-dy df$yend <- df$volume_band+dy   library(ggplot2)  p1 <- ggplot(df, aes(x=price, y=volume_band)) +   geom_point() +   geom_segment(aes(x=xstart,y=ystart,xend=xend,yend=yend))  print(p1) 

enter image description here

note slopes small see.

possibility 2:

p2 <- ggplot(df, aes(x=price, y=volume_band)) +   geom_point(aes(colour=elasticity),size=3) +   scale_colour_gradient(high="black",low="red")  print(p2) 

enter image description here


Comments