我对r和ggplot2都很新。我正在尝试使用下面的代码创建一个图表,主要数据集(dat)包含我想要绘制的数据,并且dat中的每个'tp'都被赋予了唯一的颜色,这样我就可以看到厚度与特定'tp'相关联。此代码可以按预期正常运行。
#Example
require(ggplot2)
#DataFrame
mytp<-c(80.01,80.01,80.01,80.01,80.01,80.01,80.02,80.02,80.02,80.02,80.02,80.02,80.03,80.03,80.03,80.03,80.03,80.03,80.04,80.04,80.04,80.04,80.04,80.04,83.01,83.01,83.01,83.01,83.01,83.01,83.02,83.02,83.02,83.02,83.02,83.02,83.03,83.03,83.03,83.03,83.03,83.03,83.04,83.04,83.04,83.04,83.04,83.04)
mycompid<-c(4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4)
mytml<-c(80,80,80,80,80,80,80,80,80,80,80,80,80,80,80,80,80,80,80,80,80,80,80,80,83,83,83,83,83,83,83,83,83,83,83,83,83,83,83,83,83,83,83,83,83,83,83,83)
myyears<-c(7.33470226,23.50171116,20.25188227,15.8329911,26.58726899,11.00068446,7.33470226,26.58726899,23.50171116,11.00068446,20.25188227,15.8329911,7.33470226,11.00068446,26.58726899,23.50171116,15.8329911,20.25188227,7.33470226,11.00068446,15.8329911,20.25188227,26.58726899,23.50171116,7.33470226,11.00068446,15.8329911,20.25188227,23.50171116,26.58726899,7.33470226,11.00068446,15.8329911,20.25188227,23.50171116,26.58726899,7.33470226,11.00068446,15.8329911,20.25188227,23.50171116,26.58726899,7.33470226,11.00068446,15.8329911,20.25188227,23.50171116,26.58726899)
mythick<-c(0.29,0.28,0.28,0.28,0.28,0.28,0.29,0.28,0.28,0.28,0.28,0.28,0.28,0.28,0.28,0.28,0.28,0.28,0.27,0.27,0.27,0.27,0.27,0.27,0.28,0.27,0.27,0.27,0.27,0.27,0.28,0.28,0.28,0.28,0.28,0.28,0.3,0.29,0.29,0.28,0.28,0.28,0.3,0.28,0.28,0.27,0.27,0.27)
mycomp<-c("6PIPE","6PIPE","6PIPE","6PIPE","6PIPE","6PIPE","6PIPE","6PIPE","6PIPE","6PIPE","6PIPE","6PIPE","6PIPE","6PIPE","6PIPE","6PIPE","6PIPE","6PIPE","6PIPE","6PIPE","6PIPE","6PIPE","6PIPE","6PIPE","6PIPE","6PIPE","6PIPE","6PIPE","6PIPE","6PIPE","6PIPE","6PIPE","6PIPE","6PIPE","6PIPE","6PIPE","6PIPE","6PIPE","6PIPE","6PIPE","6PIPE","6PIPE","6PIPE","6PIPE","6PIPE","6PIPE","6PIPE","6PIPE")
myDate<-c("1994-10-01","2010-12-01","2007-08-31","2003-03-31","2013-12-31","1998-05-31","1994-10-01","2013-12-31","2010-12-01","1998-05-31","2007-08-31","2003-03-31","1994-10-01","1998-05-31","2013-12-31","2010-12-01","2003-03-31","2007-08-31","1994-10-01","1998-05-31","2003-03-31","2007-08-31","2013-12-31","2010-12-01","1994-10-01","1998-05-31","2003-03-31","2007-08-31","2010-12-01","2013-12-31","1994-10-01","1998-05-31","2003-03-31","2007-08-31","2010-12-01","2013-12-31","1994-10-01","1998-05-31","2003-03-31","2007-08-31","2010-12-01","2013-12-31","1994-10-01","1998-05-31","2003-03-31","2007-08-31","2010-12-01","2013-12-31")
mytpcolor<-c("gray53","gray53","gray53","gray53","gray53","gray53","lightyellow4","lightyellow4","lightyellow4","lightyellow4","lightyellow4","lightyellow4","salmon2","salmon2","salmon2","salmon2","salmon2","salmon2","pink","pink","pink","pink","pink","pink","gray52","gray52","gray52","gray52","gray52","gray52","mistyrose1","mistyrose1","mistyrose1","mistyrose1","mistyrose1","mistyrose1","dodgerblue","dodgerblue","dodgerblue","dodgerblue","dodgerblue","dodgerblue","violetred1","violetred1","violetred1","violetred1","violetred1","violetred1")
#Primary Data for Plot
dat<-data.frame(tp=mytp, compid=mycompid, tml=mytml,years=myyears,thick=mythick,comp=mycomp,Date=myDate,tpcolor=mytpcolor)
dat$Date=as.Date(dat$Date)
ldatcompid<-c(4,4,4,4,4,4,4,4,4)
ldatnps<-c(6,6,6,6,6,6,6,6,6)
ldatcomp<-c("PIPE","PIPE","PIPE","PIPE","PIPE","PIPE","PIPE","PIPE","PIPE")
ldatcase<-c("data","data","data","data","data","data","data","data","data")
ldatyear<-c(0,2.5,7.33,11,15.83,19.83,20.25,23.5,26.59)
ldatMean<-c(0.2849,0.2843,0.2831,0.2822,0.2807,0.2798,0.2797,0.2788,0.2778)
ldatDate<-c("1987-06-01","1989-11-30","1994-09-29","1998-05-31","2003-03-30","2007-03-30","2007-08-31","2010-11-30","2014-01-01")
ldatsizecomp<-c("6PIPE","6PIPE","6PIPE","6PIPE","6PIPE","6PIPE","6PIPE","6PIPE","6PIPE")
#Additional Data in separate Date Set
ldat<-data.frame(compid=ldatcompid, nps=ldatnps, comp=ldatcomp,case=ldatcase,year=ldatyear, Mean=ldatMean,Date=ldatDate,sizecomp=ldatsizecomp)
ldat$Date=as.Date(ldat$Date)
ggplot(data=dat,aes(x=as.Date(Date),y=thick, group=tpcolor,color=tpcolor)) + geom_point(shape=18,size=3,position = position_jitter(w = 50, h = 0)) + # Plot Main Data
gtitle(paste0("MyCircuit","_","MyComponent","(",dat$compid,")")) + #Plot Title
theme_bw() + theme(plot.title= element_text(size=16, face="bold")) +
labs(x="Survey Dates and Beyond",y="Thickness(in Inches)") + #Axis labels
***#MeanLine of 2nd Set Of Data --- WHEN LINE 39 (geom_smooth) or LINE 40 (geom_line) below are UNCOMMENTED, the PLOT NO LONGER RECOGNIZES tpcolor used in line 31 (ggplot2) above. Instead I receive an error Message: Error in eval(expr, envir, enclos) : object 'tpcolor' not found --WHY?
#geom_smooth(data=ldat, aes(x=Date, y=Mean), method='lm',se=FALSE, fullrange=TRUE, colour="black", linetype="dotdash", size=0.25) +
#geom_line(data=ldat, aes(x=Date, y=Mean), colour="black", linetype="dotdash", size=0.25) +***
scale_colour_discrete(name ="Test Points",
breaks=dat$tpcolor,
labels=dat$tp)
当我尝试使用第二个数据帧ldat中的数据添加到我的绘图时出现问题。上面的代码包含后面的行,但它被注释掉了。
geom_smooth(data=ldat, aes(x=Date, y=Mean), method='lm',se=FALSE, fullrange=TRUE, colour="black", linetype="dotdash", size=0.25) +
取消注释此代码行时,出现错误:
eval(expr,envir,enclos)中的错误:找不到对象'tpcolor'*
我不明白为什么。我尝试过使用geom_line,但这似乎也不起作用。我已经尝试将它放在代码中的不同位置而没有效果。
如果我从初始图(ggplot2 aesthetic)中删除, group=tpcolor,color=tpcolor
,则错误消失,但我失去了我想要分配的颜色。如果我使用dat $ tpcolor明确指定ggplot2美学中的颜色,我会得到一个不同的错误:
错误:美学必须是长度为1或与dataProblems相同的长度:日期,平均值
这里我以为我只是在同一日期范围内添加了另一个图层,与原始绘图数据分开,但这似乎意味着分配给主数据集的颜色应用于第二个数据集,即我我不是想做。
我不确定下一步该尝试什么。任何想法可以帮助我使用我原始dat数据帧中每个tp的指定颜色将两组数据放到同一个图上?
答案 0 :(得分:2)
当您调用ggplot时,以下所有geom都会获得您提供的初始yyyy,,yyyy
。这将引导你的geom_smooth调用以寻找aes
,因此它知道如何为组着色,而不是找到它,从而导致错误。
要修复,您可以尝试分别使用每个geom中的数据:
tpcolor
或者您可以将ggplot() + geom_point(data = dat, aes(x = as.Date(Date), y = thick, group = tpcolor, color = tpcolor), shape = 18, size = 3, position = position_jitter(w = 50, h = 0)) +
geom_smooth(data = ldat, aes(x = Date, y = Mean), method = 'lm', se = FALSE, fullrange = TRUE, colour = "black", linetype = "dotdash", size = 0.25)
添加到您的geom_smooth调用中,以确保它不会继承之前的inherit.aes=FALSE
:
aes