如何在ggplot2中为红色曲线的上升段设置不同颜色?
问题
能否将红色CMg(q)曲线的上升段更换为另一种颜色?希望从青色的企业关闭点(x=2)开始改色,以此突出该段为供给曲线。

原复现代码
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\ncierre de\nla 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( limits=c(0,5.5), expand=expand_scale(mult=c(0,0.1)))+ scale_y_continuous( limits=c(0,30), expand=expand_scale(mult=c(0,0.1)))+ guides(fill=F)+ labs( 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))
解决方案
直接用stat_function无法实现分段颜色,需要先生成CMg曲线的数据集,按x=2(青色点)分段后设置不同颜色。以下是修改后的完整代码:
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} # 生成CMg曲线的分段数据 mc_data <- tibble( x = seq(0, 5, length.out = 1000), # 生成足够多的点保证曲线平滑 y = mc_cc(x), # 按x=2分段,标记供给曲线部分 segment = ifelse(x >= 2, "supply", "other") ) ggplot() + # 绘制分段的CMg曲线,x≥2部分用深绿色(和供给曲线标注颜色一致) geom_line(data = mc_data, aes(x=x, y=y, color=segment), size=2) + scale_color_manual(values = c("other" = "red", "supply" = "darkgreen"), guide="none") + # 其余元素保持原代码样式,修正了原代码中多余的"n"前缀 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\ncierre de\nla 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=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))+ guides(fill=F)+ labs(x = "Producción", y = "Precio")+ theme_classic(base_family = "Fira Sans Condensed", base_size=20)
说明
- 先生成包含x、y值和分段标识的数据集,确保曲线平滑的同时可以精准控制分段颜色
- 将x≥2的部分设置为深绿色,和图中"Curva de Oferta"的标注颜色统一,突出供给曲线
- 修正了原代码中所有多余的"n"前缀(如
ngeom_label改为geom_label),避免运行报错
内容的提问来源于stack exchange,提问作者José
相关产品推荐
相关产品推荐

