1

This is my data frame and my ggplot:

df_f <- tibble(
    seq_n = seq(1, 11, 1),
    top_white_values = c(1970, 1985, 1963, 1949, 1943, 1941, 1930, 1942, 1985, 1955, 1971)
)

ggplot(data = df_f) + 
    geom_point(aes(x = seq_n, y = top_white_values)) +
    geom_text(aes(x = seq_n, y = top_white_values, label = top_white_values))

I need to connect the dots labeled by year to the x-axis numbers but I need to use geom_function(). I would like to have some rounded functions like x^2, x^2, x^(1/2).

This is the result I want to achieve:

enter image description here

PS: Is there anything like a "Math Function Generator" that could help me to know the function related to the draw I did in this graphics? For example, I draw a function like the above and the App generates to me the mathematical function similar to this shape/draw?

1 Answer 1

4

These curves look like tangent functions, so:

library(ggplot2)
df_f <- data.frame(
  seq_n = seq(1, 11, 1),
  top_white_values = c(1970, 1985, 1963, 1949, 1943, 1941, 1930, 1942, 1985, 1955, 1971)
)

my_tan <- function(x0, x1, y1){
  ymin <- layer_scales(last_plot())$y$range$range[1]
  ymax <- layer_scales(last_plot())$y$range$range[2]
  mult <- (ymax - ymin)/10
  function(x) mult * tan(pi/1.15 * (x - (x0 + x1) / 2) / (x1 - x0)) + (y1 + ymin)/2
}

plt <- ggplot(data = df_f) + 
  geom_point(aes(x = seq_n, y = top_white_values)) +
  geom_text(aes(x = seq_n, y = top_white_values, label = top_white_values))
plt + geom_function(fun = my_tan(3, 9, 1985), xlim = c(3, 9)) + 
  geom_function(fun = my_tan(9, 2, 1985), xlim = c(2, 9))

Which gives: enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.