0

Super-simple, but I've spent hours trying the examples already here, and can't fix it.

I'm making a diagram with three fixed horizontal lines reaching a curve. From the curve, I want dashed lines going from the three points to the bottom.

The three lines are 11080,15320 and 22400 and they reach the curve NetWage (which goes from 0,0 to 30000,23000). At the moment, they reach zero using full lines: http://www.mpbi.se/bidragsbarriar.html but I want it with dashed lines, to make it easier.

There are sooo many ways to fix this, but sad me don't know any of it so please :)

alistaire
  • 42,459
  • 4
  • 77
  • 117
Mondainai
  • 35
  • 2
  • 2
    Can you make this reproducible - https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example. Also, look at `linetype` – Chase Jan 12 '19 at 19:50
  • 1
    `geom_abline()`? "The abline geom adds a line with specified slope and intercept to the plot." – Jared Wilber Jan 12 '19 at 19:52

1 Answers1

1

This can be solved with geom_segment setting argument lty to the appropriate value. I also define a helper function to compute the interceptions with the diagonal line.

library(ggplot2)

df1 <- data.frame(y = c(11080, 15320, 22400))

fx <- function(y) 30000/23000*y

ggplot(df1) +
  xlim(0, 30000) +
  ylim(0, 23100) +
  geom_segment(aes(x = 0, y = y, xend = fx(y), yend = y)) +
  geom_segment(aes(x = fx(y), y = 0, xend = fx(y), yend = y),
               lty = "dotted") +
  geom_abline(aes(intercept = 0, slope = 23000/30000))

enter image description here

Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
  • Thank you very much! The function, however, is not a straight line but a function of x, as in y=NetWage[x] (it looks a bit like a straight line, but isn't, but based on taxes and sht). So I tried to just replace fx(y) with NetWage(y) and NetWage[x] but none of them worked. – Mondainai Jan 12 '19 at 20:53
  • @Mondainai You need the inverse function. Another way is to interpolate with `help("approxfun")`. Can you post the function form or code? – Rui Barradas Jan 12 '19 at 21:59