msh855
msh855

Reputation: 1571

adding customised lines within a graph in ggplot

I want to add some customised lines within a graph.

My MWC producing the graph is:

df_tra %>%
    filter(Theta_param==1, Int_dis=='Bench', Gamma_param==0.76, Rho_param==0) %>%
    ggplot(aes(x = Debt))+
    geom_line(aes(y = Gini_tra,  colour = "Gini Coeff."), size = 1.2, colour="blue") +
    xlab("Public Debt") +
    ylab("Wealth Inequality") +
    geom_hline(yintercept=1, linetype="dashed", color = "black")+
    geom_vline(xintercept = 0.02, linetype="dashed", 
               color = "black")+
    theme_minimal()+

Into the above code how I introduce a customise lines within the graph, where above those lines have some text:

For example, what I intent to do is something that should look like this:

enter image description here

Upvotes: 1

Views: 854

Answers (1)

Alfonso
Alfonso

Reputation: 713

Updated based on OP comments

First, two different options with example data. Since you did not include the df_tra object your MWC is not working and I used the mtcars dataset as in the annotate help

1. Annotate

Here is a solution using annotate of ggplot2 library

ggplot(mtcars, aes(x = wt, y = mpg)) + 
  geom_point() + 
  annotate("text", x = 4, y = 25, label = "Some text") +
  annotate("segment", x = 3.45, xend = 4.25, y = 19.5, yend = 25,
       colour = "blue", arrow=arrow(ends = "last"))
# from arrow help:
# arrow(angle = 30, length = unit(0.25, "inches"),
#       ends = "last", type = "open")
# Arguments
# 
# angle 
# The angle of the arrow head in degrees (smaller numbers 
# produce narrower, pointier arrows). Essentially describes 
# the width of the arrow head.
# length    
# A unit specifying the length of the arrow head (from tip to base).
# ends  
# One of "last", "first", or "both", indicating which ends of the line
# to draw arrow heads.
# type  
# One of "open" or "closed" indicating whether the arrow head 
# should be a closed triangle.

enter image description here

With ends="last (default) the arrow head is in the point defined by x.end= and y.end= and with ends="first the arrow head is in the point defined by x= and y=. ends="both shows both arrow heads.


2. Annotate & geom_segment

Other option would be combine annotate and geom_segment as described in this post

ggplot(mtcars, aes(x = wt, y = mpg)) + 
  geom_point() + 
  annotate("text", x = 4, y = 25, label = "Some text") +
  geom_segment(aes(x = 3.45, xend = 4.25, y = 19.5, yend = 25),
       colour = "blue", 
       arrow=arrow(ends = "last"))

enter image description here

3. Your plot

First simulate the data

x <- seq(-5,10,0.5)
y <- 10*x^2 + 10*x - 100
df <- data.frame(x=x, y=y)

the code to generate the plot using annotate to generate the arrows. I chose this option because the arrow looks better than the one generated by geom_segment, at least in my computer.

ggplot(df) + geom_line(aes(x=x, y=y), color="blue", size = 3) +
  geom_hline(aes(yintercept=100), linetype = 2) +
  geom_vline(aes(xintercept=4), linetype = 2) +
  annotate("segment", x=-5, xend = 4, y=250, yend = 250, 
           arrow=arrow(ends = "both"), color="black", size=1.5) +
  annotate("text", x=(-5+4)/2, y=300, label="Text", color = "red") +
  annotate("segment", x=4, xend = 8.5, y=750, yend = 750, 
           arrow=arrow(ends = "both"), color="black", size=1.5) +
  annotate("text", x=(8.55+4)/2, y=800, label="Text", color = "red")

and the plot

enter image description here

Upvotes: 2

Related Questions