suppressPackageStartupMessages({
library(ggplot2)
library(grid)
})
# ---- Funções (suas) ----
CTMe <- function(q) ifelse(q == 0, NA_real_, 12/q + q)
CVMe <- function(q) q
CMg <- function(q) 2*q
P <- function(q) 24 - q
RMg <- function(q) 24 - 2*q
CT <- function(q) 12 + q^2
RT <- function(q) (24 - q) * q
# ---- Pontos-chave ----
q_m <- 6; p_m <- P(q_m)
q_0 <- 8; p_c <- 16
q_int <- 24
# deslocamento visual para separar linhas coincidentes
y_reg_horiz <- 16.05 # Demanda regulada (0..8)
y_rmg_horiz <- 15.95 # RMg_r (0..8) um pouco abaixo
# ---- Dados contínuos ----
q_max <- 26; y_max <- 26
qs <- seq(0, q_int, length.out = 400)
df <- data.frame(q = qs, Dem = P(qs), RMg = RMg(qs), CMg = CMg(qs))
# RMg em duas cores (claro até 8; escuro depois)
df_rmg1 <- subset(df, q <= q_0)
df_rmg2 <- subset(df, q >= q_0)
# ---- Áreas ----
qA <- seq(0, q_m, length.out = 200)
dfA <- data.frame(q = qA, ymin = p_m, ymax = P(qA)) # A (azul médio)
qD <- seq(0, q_m, length.out = 200)
dfD <- data.frame(q = qD, ymin = CMg(qD), ymax = p_c) # D (laranja)
qC <- seq(q_m, q_0, length.out = 150)
dfC <- data.frame(q = qC, ymin = p_c, ymax = P(qC)) # C (azul claro)
qE <- seq(q_m, q_0, length.out = 150)
dfE <- data.frame(q = qE, ymin = CMg(qE), ymax = p_c) # E (laranja = D)
# ---- Gráfico base ----
p <- ggplot() +
# eixos
annotate("segment", x = 0, xend = q_max*1.02, y = 0, yend = 0,
colour = "grey55", linewidth = 1,
arrow = grid::arrow(length = unit(7, "pt"))) +
annotate("segment", x = 0, xend = 0, y = 0, yend = y_max,
colour = "grey55", linewidth = 1,
arrow = grid::arrow(length = unit(7, "pt"))) +
# áreas (D e E MESMA cor)
geom_ribbon(data = dfD, aes(q, ymin = ymin, ymax = ymax), fill = "#F4C27E", alpha = 0.70) + # D
geom_rect(aes(xmin = 0, xmax = q_m, ymin = p_c, ymax = p_m), fill = "#B9DBF2", alpha = 0.70, inherit.aes = FALSE) + # B
geom_ribbon(data = dfA, aes(q, ymin = ymin, ymax = ymax), fill = "#9EC7EB", alpha = 0.70) + # A
geom_ribbon(data = dfC, aes(q, ymin = ymin, ymax = ymax), fill = "#B9DBF2", alpha = 0.85) + # C
geom_ribbon(data = dfE, aes(q, ymin = ymin, ymax = ymax), fill = "#F4C27E", alpha = 0.70) + # E
# curvas principais
geom_line(data = df, aes(q, Dem), colour = "#1565C0", linewidth = 1.2) + # Demanda de mercado
geom_line(data = df_rmg1, aes(q, RMg), colour = "#81C784", linewidth = 1.2) + # RMg (claro) q<=8
geom_line(data = df_rmg2, aes(q, RMg), colour = "#2E7D32", linewidth = 1.2) + # RMg (escuro) q>8
geom_line(data = df, aes(q, CMg), colour = "#D32F2F", linewidth = 1.2) +# CMg
# Demanda regulada — CINZA CLARO (0..8 horizontal deslocada; após 8 descendente real)
geom_segment(aes(x = 0, xend = q_0, y = y_reg_horiz, yend = y_reg_horiz),
colour = "#9CA3AF", linewidth = 1.4) + # <- cinza claro
geom_segment(aes(x = q_0, xend = q_int, y = p_c, yend = 0),
colour = "#9CA3AF", linewidth = 1.4) + # <- cinza claro
# RMg regulada (um pouco abaixo, visível)
geom_segment(aes(x = 0, xend = q_0, y = y_rmg_horiz, yend = y_rmg_horiz),
colour = "#2E7D32", linewidth = 1.0, linetype = "dashed") +
# (REMOVIDO) a linha vertical verde em x=8 entre ~16 e ~22
# geom_segment(... x = q_0, xend = q_0, y = p_c + 6, yend = p_c - 6, ...)
# guias (mantém preço-teto real em 16)
geom_segment(aes(x = 0, xend = q_m, y = p_m, yend = p_m),
linetype = "dashed", colour = "grey55") +
geom_segment(aes(x = 0, xend = q_0, y = p_c, yend = p_c),
linetype = "dashed", colour = "grey55") +
geom_segment(aes(x = q_m, xend = q_m, y = 0, yend = p_m),
linetype = "dashed", colour = "grey55") +
geom_segment(aes(x = q_0, xend = q_0, y = 0, yend = p_c),
linetype = "dashed", colour = "grey55") +
# pontos
geom_point(aes(x = q_m, y = p_m), colour = "#1565C0", size = 3) +
geom_point(aes(x = q_0, y = p_c), colour = "#1565C0", size = 3) +
# tema/escala
scale_x_continuous(breaks = c(0, 6, 8, 12, 24),
labels = c("0","6","8","12","24"),
limits = c(0, q_max*1.02), expand = c(0,0)) +
scale_y_continuous(breaks = c(0, 16, 18, 24),
labels = c("0","16","18","24"),
limits = c(0, y_max), expand = c(0,0)) +
coord_cartesian(clip = "off") +
theme_minimal(base_size = 13) +
theme(panel.grid = element_blank(), plot.title = element_text(face = "bold", hjust = 0.5)) +
labs(title = "Regulação Ótima de Preços", x = "Q, Unidades por dia", y = "P, preço por unidade")
# ---- Anotações (no topo; letras pretas) ----
p +
annotate("text", x = q_m*0.35, y = 19.4, label = "A", size = 4.6, colour = "black") +
annotate("text", x = q_m*0.28, y = 17.1, label = "B", size = 4.6, colour = "black") +
annotate("text", x = q_m*0.35, y = 13.8, label = "D", size = 4.6, colour = "black") +
annotate("text", x = (q_m+q_0)/2, y = 16.6, label = "C", size = 4.6, colour = "black") +
annotate("text", x = (q_m+q_0)/2, y = 15.4, label = "E", size = 4.6, colour = "black") +
annotate("text", x = 11, y = 5.5, label = "RMg", colour = "#2E7D32", size = 5) +
annotate("text", x = 12.5, y = 22.0, label = "CMg", colour = "#D32F2F", size = 5) +
annotate("text", x = 6.0, y = 21, label = "Demanda de mercado", colour = "#1565C0", size = 4.8) +
annotate("text", x = 10.9, y = 16.9, label = "Demanda regulada", colour = "#4B5563", size = 4.6) + # label em cinza escuro
annotate("text", x = 6, y = y_rmg_horiz + 0.8, label = "RMg[r]",
colour = "#2E7D32", size = 4.4, vjust = 2.6, parse = TRUE) +
annotate("text", x = q_m - 0.25, y = p_m + 0.8, label = "italic(e[m])",
colour = "#C62828", size = 4.5, parse = TRUE) +
annotate("text", x = q_0 + 0.25, y = p_c - 0.8, label = "italic(e[0])",
colour = "#C62828", size = 4.5, parse = TRUE)