Chris M
Chris M

Reputation: 23

Plotting multiple dataframes with ggplot with different colors

I very new to both r and ggplot2. I am trying to create a plot using the code below the idea is that the primary data set (dat) contains the data I want to plot, and each 'tp' in dat has been assigned a unique color, so that I can see the thick associated with the specific 'tp'. This code runs fine with output as expected.

#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)

My issue arises when I try to add to my plot using data from my second dataframe, ldat. The code above contains the line that follows, but it is commented out.

geom_smooth(data=ldat, aes(x=Date, y=Mean), method='lm',se=FALSE, fullrange=TRUE, colour="black", linetype="dotdash", size=0.25) +

When this codeline is uncommented, I get an error:

Error in eval(expr, envir, enclos) : object 'tpcolor' not found*

And I do not understand why. I have tried using a geom_line instead, but that doesn't seem to work either. I have tried putting it in different locations within the code with no effect.

If I remove the , group=tpcolor,color=tpcolor from the initial plot (ggplot2 aesthetic), the error disappears, but I lose the colors I am looking to assign. If I explicitly assign the colors in the ggplot2 aesthetics using dat$tpcolor, I get a different error:

Error: Aesthetics must either be length one, or the same length as the dataProblems:Date, Mean

Here I thought I was just adding another layer within the same date range to my plot separate from the originally plotted data- but this seems to imply that the colors assigned to primary data set are being applied to the second dataset, which I am not trying to do.

I am not really sure what to try next. Any ideas that might help me get both sets of data onto the same plot using the assigned colors for each tp in my original dat dataframe?

Upvotes: 2

Views: 3103

Answers (1)

jeremycg
jeremycg

Reputation: 24945

When you call ggplot, all of the following geoms get the initial aes you gave. This ls leading your geom_smooth call to look for tpcolor so it knows how to colour the groups, and not finding it, thus your error.

To fix, you can try using the dat in each geom separately:

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)

or you can add inherit.aes=FALSE to your geom_smooth call to make sure it doesn't inherit the previous aes:

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)) +
       geom_smooth(data = ldat, aes(x = Date, y = Mean), method = 'lm', se = FALSE, fullrange = TRUE, colour = "black", linetype = "dotdash", size = 0.25, inherit.aes = FALSE)

Upvotes: 3

Related Questions