-1

使用 ggplot2 创建绘图时遇到问题

> setwd("c:/tesis/emisiones")

e<-read.csv("fh3pco1.csv",header=T) attach(e)

names(e)

1 “VehCat” “组件” “TrafficSit” “子段” “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)
4

2 回答 2

1

Using some fake data since none was provided, you can do this without 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)

Then just add another geom_point that contains the output of your function:

g + geom_point(aes(x=1:10, y=vpge0(1:10), colour='vpge0'))

I find this cleaner and easier syntax than monkeying with stat_function.

于 2013-05-24T19:04:48.607 回答
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)
于 2013-08-14T14:32:07.940 回答