-2

For writing my economics papers I need to create graphs that show the intuition of an underlying process. A simple example would be the graph linked at the bottom. Note the dashed lines that emphasize a point on the graph.

How would I replicate this graph in ggplot, with

  • both vertical and horizontal dashed lines,
  • the round intercepting dot where the dashed lines meet
  • Q and NIR labels at the axes?

To give an example of a graph:

x <- c(10:100)
y <- 1/x
data <- data.frame(x, y)

ggplot(data, aes(x, y)) + geom_line()

Let's say I want to indicate the point (25, 0.04) as done in the example graph. How would I do that?

economics graphs

MightyMauz
  • 65
  • 5

1 Answers1

2

You can assign a subset of data -- i.e. your 2 desired points to geom_point, here I use c(25,50)

Use geom_segment to create dashed lines to the subsetted points

Use scale_x_continuous and scale_y_continuous to create axis labels

Use theme to change plot theme elements

# Create geom_point and geom_segments
gg1 <- ggplot(data, aes(x, y)) + geom_line(lwd=2) +
  geom_point(data=data[data$x %in% c(25, 50),], aes(x, y), pch=16, size=5) +
  geom_segment(data=data[data$x %in% c(25, 50),], aes(x=x, xend=x, y=0, yend=y), lty=2, lwd=1) +
  geom_segment(data=data[data$x %in% c(25, 50),], aes(x=0, xend=x, y=y, yend=y), lty=2, lwd=1)

gg1

# Define new labels
x_label <- NA
x_label[! x %in% c(25, 50)] <- ""
x_label[x %in% c(25, 50)] <- c("Q", "Q1")
y_label <- NA
y_label[! x %in% c(25, 50)] <- ""
y_label[x %in% c(25, 50)] <- c("NIR", "NIR1")

gg2 <- gg1 + scale_x_continuous(breaks=x, labels=x_label) +
  scale_y_continuous(breaks=y, labels=y_label)

gg2


# Add axis labels, change theme elements, remove ticks and grid lines
gg3 <- gg2 + xlab("Quantity of Investment") +
ylab("Norminal Interest Rate") +
theme_bw() +
theme(panel.grid.major = element_blank(),
  panel.grid.minor = element_blank(),
  axis.ticks = element_blank(),
  axis.title = element_text(size=16),
  axis.text = element_text(size=14)) 

gg3

enter image description here

Djork
  • 3,319
  • 1
  • 16
  • 27