I have been dabbling with r on and off for the past 2 years, and only recently began using ggplot for building graphs. I am stuck with my situation described below:
I have a multilayer plot, where all data is in the same dataframe referenced in the initial ggplot call.
- Layer 1 = Points
- Layer 2 = Avg trend
- Layer 3 = Derivative of the trend
- Layer 4&5 = Prediction interval of the Points given the average trend
What I want to do is manually adjust the legend text to reflect the names and the colors/geom for each layer (layer4&5 can be one reference). When I to do this the legend for the geom_lines all come back as red.
The other issue is that I want to rename the gradient scale, but when I do the gradient turns to discrete points rather than a bar.
ggplot(D_ff_NWn,aes(NW_norm,FF_Det,color = CYC))+
geom_point()+
scale_color_gradient(guide = guide_legend(title = "Feeder Cycle"))+
geom_line(aes(NW_norm,FF10.fit,fill="black"), color="black", show.legend = TRUE) +
geom_line(aes(NW_norm,Diff1*SCL_rg+SCL_FF[1],fill="red"),
color="red", show.legend = TRUE)+
geom_line(aes(NW_norm,FF_UCL,fill="Prediction"),color="green")+
geom_line(aes(NW_norm,FF_LCL),color="green")+
labs(x = "Normalized Net Weight (%)")+
scale_y_continuous("Feed Factor (g/rev)",
sec.axis = sec_axis(~ (. - SCL_FF[1])/SCL_rg,
name = "1st Derivative ([g/rev]/%)"))+
scale_fill_manual(name="",
labels = c("Avg FF (g/min)", "1st Derivative","95% Prediction"),
values = c("black","red","green"))+
theme(axis.text.y.right = element_text(color = "red"),
axis.title.y.right = element_text(color = "red"))
So in summary, I would ideally like to have:
- the gradient bar with my custom name got the first layer
- a representative line for each layer with custom names
Note: The second y axis is scaled using SCL_FF
and SCL_rg
I am sure I can't add enough data to generate the image shown above to this post, but the dataframe structure is shown below.
'data.frame': 16141 obs. of 19 variables:
$ key : Factor w/ 6 levels "ATAB","CCNa",..: 1 1 1 1 1 1 1 1
1 1 ...
$ Process_Time : num 5.65 5.67 5.68 5.7 5.72 ...
$ CONC_PCT : num 32 32 31.8 31.7 31.6 ...
$ STATE : Factor w/ 4 levels "Blind","Gravimetric",..: 2 2 2 2
2 2 2 2 2 2 ...
$ NW : num 1.16 1.15 1.15 1.15 1.15 ...
$ SRW_SP : num 56.7 56.4 56.3 56.2 56 ...
$ FF : num 2.36 2.37 2.37 2.37 2.37 ...
$ MF : num 8 7.98 7.95 7.93 7.9 ...
$ CYC : int 1 1 1 1 1 1 1 1 1 1 ...
$ Max_Mass : num 1.72 1.72 1.72 1.72 1.72 ...
$ NW_norm : num 0.673 0.672 0.671 0.67 0.668 ...
$ FF_Det : num 2.33 2.33 2.33 2.34 2.34 ...
$ FF10.fit : num 2.34 2.34 2.34 2.34 2.34 ...
$ FF10.se.fit : num 0.000121 0.000121 0.000121 0.000121 0.000121
...
$ FF10.residual.scale: num 0.00458 0.00458 0.00458 0.00458 0.00458 ...
$ FF10.df : num 16128 16128 16128 16128 16128 ...
$ Diff1 : num 0.0363 0.0363 0.0361 0.0344 0.0323 ...
$ FF_UCL : num 2.35 2.35 2.35 2.35 2.35 ...
$ FF_LCL : num 2.34 2.34 2.34 2.34 2.34 ...
Is there something I am blatantly missing? I thought I had a decent understanding of how the ggplot layers worked.
Appreciate any help or guidance.
Update 07May2018
The solution described below from Z.Lin worked. I reversed the gradient bar labels and changed the "1st derivative" layer to point to hide the unwanted raster at the high end of the "Normalized Net Weight".
- The new legend results in point/lines for all layers. Can this be adjusted. I don't understand why it must default to this
- The other remaining issue is that the borders from using the shape detracts from the gradient bar map to the main plot. It might be ok as the main plot is a detrended version of the Feed Factor in the window plotted against "Normalized Net Weight" instead of "Process Time"
ggplot(D_ff_NWn, aes(x = NW_norm))+
geom_point(aes(y = FF_Det, fill = CYC), shape = 21,stroke = 0.1) +
geom_point(aes(y = Diff1 * SCL_rg + SCL_FF[1], colour = "1st
Derivative"),size=0.5) +
geom_line(aes(y = FF10.fit, colour = "Avg FF (g/min)")) +
geom_line(aes(y = FF_UCL, colour = "95% Prediction")) +
geom_line(aes(y = FF_LCL, colour = "95% Prediction")) +
labs(x = "Normalized Net Weight (%)")+
scale_y_continuous(name = "Feed Factor (g/rev)",
sec.axis = sec_axis(~ (. - SCL_FF[1])/SCL_rg,
name = "1st Derivative ([g/rev]/%)")) +
scale_fill_gradient(name = "Feeder Cycle",guide = guide_colourbar(reverse =
TRUE))+
scale_colour_manual(name = "",
values = c("Avg FF (g/min)" = "black",
"1st Derivative" = "red",
"95% Prediction" = "green"))+
theme(axis.text.y.right = element_text(color = "red"),
axis.title.y.right = element_text(color = "red"))