rggplot2colorsr-markdown

How can I fill with another color the increasing part of a curve in ggplot2?


Is there a way to use another colour when plotting the increasing part of the red curve, I mean, I want to change the color from the cyan point onwards, that is the increasing part just to remark that it is the supply curve.

Supply curve

I provide the code for replication:

library("tidyverse")
library("mosaic")
library("ggrepel")
library("fontawesome")

mc_cc<-function(x){3*x^2-8*x+10}
ac_cc<-function(x){x^2-4*x+10+10/x}
atc_cc<-function(x){x^2-4*x+10}

ggplot(data.frame(x=c(0,11)), aes(x=x))+
stat_function(fun=mc_cc, geom="line", size=2, color = "red")+
geom_label(aes(x=4.3,y=mc_cc(4.1)), color = "red", label="CMg(q)", size = 5)+
stat_function(fun=ac_cc, geom="line", size=2, color = "blue")+
geom_label(aes(x=4.6,y=ac_cc(5)), color = "blue", label = "CTMe(q)", size = 5)+
stat_function(fun=atc_cc, geom="line", size=2, color = "deepskyblue")+
geom_label(aes(x=4.6,y=atc_cc(4.5)), color = "deepskyblue", label = "CVMe(q)", size = 5)+
geom_segment(x=0, xend=2, y=6.1, yend=6.1, size=1, linetype="longdash", color = "cyan")+
geom_segment(x=3.3, xend=3.3, y=0, yend=16, size=1, linetype="dotted")+
geom_segment(x=0, xend=3.3, y=16, yend=16, size=1, linetype="dotted")+
geom_segment(x=4, xend=4, y=0, yend=26, size=1, linetype="dotted")+
geom_segment(x=0, xend=4, y=26, yend=26, size=1, linetype="dotted")+
annotate("point", x = 2, y = 6.1, color = "cyan", size = 4) +
annotate("text", x = 2, y = 3.5, label = "Punto de\n cierre de\n la empresa", colour = "deepskyblue", size = 4)+
annotate("text", x = 3.4, y = 20, label = "Curva de Oferta", colour = "darkgreen", size = 6, angle = 63)+
annotate("point", x = 3.3, y = 16, color = "black", size = 4) +
annotate("point", x = 4, y = 26, color = "black", size = 4) +
scale_x_continuous(#breaks=NULL,
                 limits=c(0,5.5),
                 expand=expand_scale(mult=c(0,0.1)))+
scale_y_continuous(#breaks=NULL,
                 limits=c(0,30),
                 expand=expand_scale(mult=c(0,0.1)))+
guides(fill=F)+
labs(#title = "Representative Firm",
   x = "Producción",
   y = "Precio")+
theme_classic(base_family = "Fira Sans Condensed", base_size=20) +
scale_x_continuous(breaks=c(0,2, 3.3, 4),
                 labels=c(0, expression(""), expression(q["1"]), expression(q["2"])),
                 limits=c(0,5),
                 expand=c(0,0))+
scale_y_continuous(breaks=c(0, 6.1, 16, 26),
                 labels=c(0,expression("P=CVMe"), expression(P["1"]), 
expression(P["2"])),
                 limits=c(0,30),
                 expand=c(0,0))

Solution

  • You may put the x values along with the y calculated by atc_cc in a dataframe and create an indicator variable that switches when x > 2. Then you can use geom_line and map the indicator variable to the color esthetic and use scale_color_manual to change the colors and suppress the legend:

    library("tidyverse")
    library("mosaic")
    library("ggrepel")
    library("fontawesome")
    
    mc_cc<-function(x){3*x^2-8*x+10}
    ac_cc<-function(x){x^2-4*x+10+10/x}
    atc_cc<-function(x){x^2-4*x+10}
    
    ## Create a dataframe with x values, y values for the curve and indicator variable
    
    df <- data.frame(x = seq(0, 11, len = 100)) |> 
      mutate(y = atc_cc(x),
             z = factor(ifelse(x < 2, 0, 1)))
    
    ggplot(data = df, aes(x = x)) + ## Use the dataset just created
    stat_function(fun=mc_cc, geom="line", size=2, color = "red")+
    geom_label(aes(x=4.3,y=mc_cc(4.1)), color = "red", label="CMg(q)", size = 5)+
    stat_function(fun=ac_cc, geom="line", size=2, color = "blue")+
    geom_label(aes(x=4.6,y=ac_cc(5)), color = "blue", label = "CTMe(q)", size = 5)+
    ## stat_function(fun=atc_cc, geom="line", size=2, color = "deepskyblue")+
    geom_line(aes(y = y, color = z), size = 2) + # instead of stat_function()
    scale_color_manual(values = c("green", "deepskyblue"), guide = "none") + # Change colors and suppress legend
    geom_label(aes(x=4.6,y=atc_cc(4.5)), color = "deepskyblue", label = "CVMe(q)", size = 5)+
    geom_segment(x=0, xend=2, y=6.1, yend=6.1, size=1, linetype="longdash", color = "cyan")+
    geom_segment(x=3.3, xend=3.3, y=0, yend=16, size=1, linetype="dotted")+
    geom_segment(x=0, xend=3.3, y=16, yend=16, size=1, linetype="dotted")+
    geom_segment(x=4, xend=4, y=0, yend=26, size=1, linetype="dotted")+
    geom_segment(x=0, xend=4, y=26, yend=26, size=1, linetype="dotted")+
    annotate("point", x = 2, y = 6.1, color = "cyan", size = 4) +
    annotate("text", x = 2, y = 3.5, label = "Punto de\n cierre de\n la empresa", colour = "deepskyblue", size = 4)+
    annotate("text", x = 3.4, y = 20, label = "Curva de Oferta", colour = "darkgreen", size = 6, angle = 63)+
    annotate("point", x = 3.3, y = 16, color = "black", size = 4) +
    annotate("point", x = 4, y = 26, color = "black", size = 4) +
    scale_x_continuous(#breaks=NULL,
                     limits=c(0,5.5),
                     expand=expand_scale(mult=c(0,0.1)))+
    scale_y_continuous(#breaks=NULL,
                     limits=c(0,30),
                     expand=expand_scale(mult=c(0,0.1)))+
    guides(fill=F)+
    labs(#title = "Representative Firm",
       x = "Producción",
       y = "Precio")+
    theme_classic(base_family = "Fira Sans Condensed", base_size=20) +
    scale_x_continuous(breaks=c(0,2, 3.3, 4),
                     labels=c(0, expression(""), expression(q["1"]), expression(q["2"])),
                     limits=c(0,5),
                     expand=c(0,0))+
    scale_y_continuous(breaks=c(0, 6.1, 16, 26),
                     labels=c(0,expression("P=CVMe"), expression(P["1"]), 
    expression(P["2"])),
                     limits=c(0,30),
                     expand=c(0,0))