Trying to add Horizontal lines to one Facet_Grid only

I am trying to add geom_line to only one of my facet grids. The code below puts the line on both plots

tiff("RBbBriph.tif", width = 1049, height = 268, units = "px")
xph <- df %>%
gather(variable, value,-NST_DATI)
ggplot(data = xph, aes(x = NST_DATI, y = value, colour = variable)) +
stat_smooth(colour = "gray50") +
facet_grid(variable~.,scales="free_y", labeller = labeller(variable=labels))+
labs(x="Date",y="Measurement",colour="Paremeters",title = "RBbB",subtitle = "pH")+

testing horizontal lines

geom_hline(data= xph, aes(yintercept=6.56), colour="purple", linetype=2)+

End testing horizontal lines

theme(legend.position = "none")

In order to plot a geom on only one facet, the only faceting group in the data should be the one to which the geom should be applied. For example, I have attempted to recreate your plot with the iris data. I filtered the iris data to only include the species "setosa" for the geom_hline:


ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, col = Species))+
  geom_hline(data = iris %>% filter(Species == "setosa"),
             aes(yintercept = 3.5), col = "purple", linetype = 2)+
  facet_grid(Species ~., scales = "free_y")

and the purple horizontal line is only shown in the setosa facet.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

I am not sure what you mean by two parameters.

In order to have geom_hline not be global, you need to subset the data supplied to geom_hline to only the values of 'variable' which you want to plot the geom_hline as in my example. I only want geom_hline to apply to the 'setosa' facet of 'Species' so I subset the data provided to geom_hline to only include 'setosa'.

Sorry I am new to R, so I cannot graph two parameters with the one facet call?

ok thanks I will try that.