-2

I have 3 lines representing the mean and I have another 3 dashed lines representing the CV. I would like to have in the legend, only the 3 means and only one black dashed line label as "CV %"

year       date    field               type  value
50   2016 2016-07-09 Lameiras    Mean.High.Yield 4.9080
51   2016 2016-07-19 Lameiras    Mean.High.Yield 4.5190
300  2016 2016-08-18 Lourenco  Mean.Medium.Yield 6.2990
301  2016 2016-08-28 Lourenco  Mean.Medium.Yield 6.1900
500  2016 2016-07-29 Lourenco     Mean.Low.Yield 5.3840
501  2016 2016-08-08 Lourenco     Mean.Low.Yield 6.2070
700  2016 2016-07-09 Lourenco   cv.10.High.Yield 1.1444
701  2016 2016-07-19 Lourenco   cv.10.High.Yield 0.6371
900  2016 2016-06-19 Lourenco cv.10.Medium.Yield 7.6911
901  2016 2016-06-29 Lourenco cv.10.Medium.Yield 2.0656
1200 2017 2017-09-27    Mel_4    cv.10.Low.Yield 3.6191
1201 2017 2017-10-02    Mel_4    cv.10.Low.Yield 3.6529    



bxp_YL<-function(field,year){
      d_test1<-d_test[which(d_test$field==field & d_test$year==year),]
      ggplot(d_test1, mapping=aes(x = date, y = value, shape =type , color = type,
                                  group=type))+
      geom_point()+
      geom_line(aes(linetype=type))+
      scale_y_continuous(sec.axis = sec_axis(~ . * 10,name = "Coeficient of Variation %"),limits = c(0,8))+
      labs(x="Day of Sensing", y ="Mean of gLAI (m2 / m2)", 
          title = field,subtitle = paste("Evolution of gLAI in",year))+
      scale_x_datetime(date_labels = "%d/%b",date_breaks = "10 day")+
      scale_color_manual(name="",values = c("Mean.High.Yield"="chartreuse4", 
                                    "Mean.Medium.Yield"="cornflowerblue",
                                    "Mean.Low.Yield"="red",
                                    "cv.10.High.Yield"="chartreuse4",
                                    "cv.10.Medium.Yield"="cornflowerblue",
                                    "cv.10.Low.Yield"="red"),
                         labels=c("High Yield","Medium Yield","Low Yiweld",
                                  "","",""))+
        scale_linetype_manual(values=c("solid","solid","solid",
                                       "dashed","dashed","dashed"))+
        scale_shape_manual(values = c(8,16,17,8,16,17))

    }

image_example

Dan
  • 11,370
  • 4
  • 43
  • 68

1 Answers1

1

To answer the second part of your question in the title, you can censor your legends by setting the breaks argument in the relevant scale appropriately:

ggplot(iris, aes(Sepal.Width, Sepal.Length)) +
  geom_point(aes(colour = Species, shape = Species)) +
  scale_color_discrete(breaks = c("setosa", "virginica")) +
  scale_shape_discrete(breaks = c("setosa", "versicolor"))

enter image description here

Notice how the legend hides the colour for the 'versicolor' data and hides the shape for the 'virginica' data.

You can hide the legend for a particular mapping by setting the guide in that scale to "none":

ggplot(iris, aes(Sepal.Width, Sepal.Length)) +
  geom_point(aes(colour = Species, shape = Species)) +
  scale_color_discrete(guide = "none")

enter image description here

To answer your question in the text body, you can structure your plot as follows:

# Let leaf be another variable
df <- data.frame(Length = c(iris$Sepal.Length, iris$Petal.Length),
                 Width = c(iris$Sepal.Width, iris$Petal.Width),
                 Leaf = rep(c("Sepal", "Petal"), each = 150),
                 Species = rep(iris$Species, 2))

ggplot(df, aes(Length, Width, 
               colour = Species, linetype = Leaf,
               group = interaction(Species, Leaf))) +
  geom_path() +
  scale_linetype_manual(values = 1:2, breaks = "Sepal")

enter image description here

Adapt for your own data, which I couldn't conveniently import in R.

teunbrand
  • 33,645
  • 4
  • 37
  • 63