使用ggplot2
创建绘图时遇到问题> setwd("c:/tesis/emisiones")
e<-read.csv("fh3pco1.csv",header=T)
attach(e)
names(e)
1“VehCat”“组件”“TrafficSit”“Subsegment”“SizeClasse”“V”“EFA”
[8]“norma”“cat”“cat1”
library(ggplot2)
length(e)
1 10
vpge0 <- function(x) {(281* x^-0.63)}
ggplot(e, aes(x=V,y=EFA, colour=norma)) + geom_point(size=4)
ggplot(e, aes(x=V,y=EFA, colour=norma)) +
geom_point(size=4)+
stat_function(data = data.frame(x = 1:100, FE = factor(1)),fun = vpge0, size=1)
答案 0 :(得分:1)
使用某些虚假数据,因为没有提供任何数据,您可以在没有stat_function
的情况下执行此操作。
library(ggplot2)
e <- data.frame(V=1:10, EFA=1:10, norma=c('a', 'b'))
g <- ggplot(e, aes(x=V, y=EFA, colour=norma)) + geom_point(size=4)
然后只需添加包含函数输出的另一个geom_point
:
g + geom_point(aes(x=1:10, y=vpge0(1:10), colour='vpge0'))
我发现这种语法更清晰,更简单,而不是使用stat_function进行编辑。
答案 1 :(得分:0)
library(scales)
ggplot(f1, aes(x=V,y=EFAnoxE3, colour=NormaGEuro3))+
geom_point(size=4)+
stat_function(fun = m5noxe3, size=1, colour="green")+
stat_function(fun = c4noxe3, size=1, colour="green")+
stat_function(fun = unoxe3, size=1, colour="red")+
scale_colour_manual(values = c("green", "blue", "green","red"),
name = "",
labels = c("MODEM 5 Euro 3","HBEFA 3 Euro 3", "COPERT 4 Euro 3","UNTEC Euro 3"))+
theme(plot.title = element_text(lineheight=.8, face="bold"),
axis.text.x=element_text(size=12, face="bold"),
axis.text.y=element_text(size=12, face="bold"),
legend.position=c(1,0.82),legend.justification=c(1,1),
legend.background = element_rect(fill=alpha("grey", 0.2)))+
ggtitle("Factores de Emisión NOx Vehiculos \n Particulares Gasolineros (g/km)")+
ylab("(g/km)") +
xlab("Velocidad Promedio (km/h)")+
annotate("rect", xmin = 25, xmax = 35, ymin = 0, ymax = 0.6, alpha = .2)