问题
I simply represent the curves of Average Total Costs, Average Variable Costs, Average Fixed Costs and Marginal Costs.
plot(Q,ATCosts,ylab=NA,ylim=c(0,62),type="l")
par(new=T)
plot(Q,AVCosts,ylab=NA,ylim=c(0,62),type="l",col="blue")
par(new=T)
plot(Q,AFCosts,ylab=NA,ylim=c(0,62),type="l",col="red")
par(new=T)
plot(Q,MCosts,ylab=NA,ylim=c(0,62),type="l",col="green")
The main is:
title(main="ATC,AVC,AFC,MC")
What I would like to know is if there is a way to give each piece of the main (containing a certain type of cost) the color related to that cost in the plot, in order to avoid to use a legend. Thus, ATC must be written in black; AVC must be written in blue...and so on.
I tried to overlap another title in the following way:
title(main=" ,AVC, , ",col.main="blue")
But it didn't give a decent result.
回答1:
Not a general solution, because you have to play with the adj
to get the right spacing, but it works:
plot(1)
mtext("ATC, ",col='black',line=2,adj=0.4)
mtext("AVC, ",col='blue',line=2,adj=0.45)
mtext("AFC, ",col='red',line=2,adj=0.5)
mtext("MC",col='green',line=2,adj=0.54)

回答2:
If you don't want to use a legend, you can use directLables
package. But you should use move to some high level plot package (lattice/ggplot2)

set.seed(10)
dat = data.frame(Q= factor(paste0('Q',1:12)),
AT = runif(12,1,100),
AV = runif(12,1,100),
AF = runif(12,1,100),
MC = runif(12,1,100))
library(lattice)
library(directlabels)
p <- xyplot(AT+AV+AF+MC~Q,data=dat,type='l')
direct.label(p,"first.qp")
来源:https://stackoverflow.com/questions/20907421/how-to-give-different-colors-to-parts-of-the-main-of-a-plot