Alex Bădoi
Alex Bădoi

Reputation: 830

How to add a line through two points on scatter plot and remove axis scales?

I have the following data:

> dput(all_data)
structure(list(X = structure(1:8, .Label = c("DMU 1", "DMU 2", 
"DMU 3", "DMU 4", "DMU 5", "DMU 6", "DMU 7", "DMU 8"), class = "factor"), 
    x = c(7L, 13L, 7L, 18L, 12L, 14L, 10L, 16L), y = c(7L, 5L, 
    3L, 5L, 9L, 14L, 3L, 10L)), .Names = c("X", "x", "y"), class = "data.frame", row.names = c(NA, 
-8L))

And the following graph:

plot(all_data[,2], all_data[,3], ylim = c(0, 20), xlim = c(0,20),
     xlab = "Input", ylab = "Output")
text(all_data[,2], all_data[,3], labels = all_data[,1], cex = 0.7, pos = 4)

I am trying to replicate this graph without plagiarizing:

enter image description here

It's close enough but i am having problems adding the line through points at DMU 1 and DMU 6.

I would also like to remove the axis scales since the graph is only for a graphical representation of a concept and does not measure anything. Any other additions that might help improve it aesthetically are appreciated.

Upvotes: 1

Views: 564

Answers (2)

rawr
rawr

Reputation: 20811

all_data <- structure(list(X = structure(1:8, .Label = c("DMU 1", "DMU 2","DMU 3", "DMU 4", "DMU 5", "DMU 6", "DMU 7", "DMU 8"), class = "factor"), 
                           x = c(7L, 13L, 7L, 18L, 12L, 14L, 10L, 16L),
                           y = c(7L, 5L, 3L, 5L, 9L, 14L, 3L, 10L)),
                      .Names = c("X", "x", "y"),
                      class = "data.frame", row.names = c(NA,-8L))

with(all_data, {
  par(family = 'serif')
  plot(x, y, xlim = c(0,20), ylim = c(0,20), pch = 16, bty = 'l',
       xaxt = 'n', yaxt = 'n', ann = FALSE)
  text(x, y, labels = parse(text = gsub('(....)(.)', 'italic(\\1[\\2])', X)),
       adj = c(0, 1.5))
  abline(0, 1, lty = 2)
  p <- par('usr')
  text(c(p[1], p[2], 10), c(p[4], p[3], p[4]), xpd = NA, adj = c(1.5, 1.5),
       labels = c('output','input', 'Efficient frontier'))
  arrows(7, 18, 10, 11, length = .1)
  mtext('Figure 1: Efficient frontier - CCR model.', font = 3,
        side = 1, line = 3, at = 2)
})

enter image description here

Upvotes: 2

MLavoie
MLavoie

Reputation: 9836

this give you almost what you want. You can change the color, linetype of abline() as you want. and thanks @thelatemail I have edited the way to subset and fit a linear model.

plot(all_data[,2], all_data[,3], ylim = c(0, 20), xlim = c(0,20), xlab = "Input", ylab = "Output", xaxt='n', yaxt='n')
text(all_data[,2], all_data[,3], labels = all_data[,1], cex = 0.7, pos = 4)
abline(lm(y~x, data=all_data, subset=X %in% c("DMU 1","DMU 6") ))
arrows(x0=5, y0=15, x1 = 10, y1 = 10, length = 0.10, angle = 20,
        code = 2, col = par("fg"), lty = par("lty"),
        lwd = par("lwd"))
text(6, 15.5, "Efficient Frontier", cex = .8)

enter image description here

Upvotes: 3

Related Questions