How to Create a Pareto Graph in R

98 Views Asked by At

I am a graduate student and one of the graphs my advisor wants is a pareto graph. I am comparing two variables one is an ordinal and the other is continuous. I am trying to compare aggresison data to courtship times. I have created a skyline plot, but they did not like that and I have seen some of the graphs that have the bar charts with lines but I don't particularly like or understand those ones either. Sorry I'm just being a little picky. If I could create something more like the graph below in r I would really appreciate it and any help. this is an example of what I would like to graph, its the pareto front on wikipedia

I have tried other graphs that either havent worked or looked not right. I did a skyline plot but they did not like it I think theyre looking for more of a curved line look I guess.

3

There are 3 best solutions below

0
On

The Pareto front Wikipedia example you provided is a slight modification of the code in this Pareto front answer to get the furthest away from the origin instead of the closest. Example using mtcars below.

library(ggplot2)
d <- data.frame(x = mtcars$wt, y = mtcars$mpg)
D <- d[order(d$x,d$y,decreasing=TRUE),]

pareto_front <- which(!duplicated(cummax(D$y)))
front <- D[pareto_front,]
other <- D[-pareto_front,]

ggplot(mapping = aes(x, y)) +
  geom_point(data = other) +
  geom_line(data = front, colour = 'red') +
  geom_point(data = front, colour = 'red') +
  lims(x = c(0, NA), y = c(0, NA))

pareto front

0
On

It's not fully purified, yet it can help you.

x_values <- 1:10
y_values <- c(35, 30, 30, 25, 15, 17, 14, 10, 9, 7)

df <- data.frame(x_values, y_values)
df

library(ggplot2)

myplot <- ggplot(data = df, aes(x=x_values, y=y_values)) +
  geom_line(linewidth = 3, color = "red") +
  annotate("point", x = 2.5, y = 25, colour = "grey60", size = 10) +
  annotate("point", x = 6, y = 17, colour = "red", size = 10) +
  annotate("text", x = 6, y = 17, label = "C", size = 5) +
  theme_classic() +
  labs(x = "QUANTITY OF ITEM 1", y = "QUANTITY OF ITEM 2")

myplot

enter image description here

1
On

1) ggplot2 Using the built in mtcars data frame and following an example in the rPref package we define mtcars.lev which adds a .level column such that a .level of 1 is the pareto frontier. Then we plot with ggplot2 placing the gear number within each point.

library(dplyr)
library(ggplot2)
library(rPref)

mtcars.lev <- psel(mtcars, high(mpg) * high(hp), top = nrow(mtcars))

ggplot(mtcars.lev, aes(hp, mpg, col = .level == 1)) +
  geom_point(alpha = 0.3, size = 7) + 
  geom_text(label = format(mtcars.lev$gear), color = "black") + 
  geom_line(data = filter(mtcars.lev, .level == 1), lwd = 2, alpha = 0.3) +
  theme(legend.position = "none")

screenshot

2) Classic graphics A classic graphics version of the above follows.

library(dplyr)
library(rPref)

mtcars.lev <- mtcars %>%
  psel(high(mpg) * high(hp), top = nrow(.)) %>%
  arrange(hp, mpg)

col <- adjustcolor(c("lightblue", "pink"), 
  alpha = 0.3)

plot(mpg ~ hp, mtcars.lev, cex = 4, pch = 20,
  col = ifelse(.level == 1, col[1], col[2]))
with(mtcars.lev, text(hp, mpg, gear, cex = 0.7))
lines(mpg ~ hp, mtcars.lev, subset = .level == 1, col = col[1], lwd = 7)
title(main = "Classic graphics", sub = "(circled numbers are number of gears)")

screenshot