22

already searched all related threads about this but could not find a solution.

Here is my code and the attached plot result:

g <-ggplot(NDVI2, aes(LAI2, NDVI, colour = Legend)) + 
    theme_bw (base_family = "Times") + 
    scale_colour_manual (values = c("purple", "green", "blue", "yellow",  "magenta","orange", "cyan", "red", "black")) + 
    geom_point (size = 3) +
    geom_smooth (aes(group = 1, colour = "Trendline"), method = "loess", size = 1, linetype = 5, se = FALSE) +
    geom_smooth (aes(group = 1, colour = "Regression (log)"),linetype = 1, size=1.2,method = "lm", formula = y~ log(x), se = FALSE) + 
    labs (title = "Correlation of LAI and NDVI")+ 
    theme (legend.title = element_text (size = 15)) 

Which results in this plot:

enter image description here

As you can see, all Legend Icons look the same. What I want is that the points are shown as points and the two lines ("Regression" and "Trendline") are shown as lines.

I tried to use

guides (colour = guide_legend (override.aes = list(size = 1.5)))

but that gives me again all icons in the same way and I can not figure out how to distinguish between them

I´m new to R and this is my first "complex" plot. Try to figure out most with online helps and google but can´t find a solution for this problem. Thank you all for your time and help!

Here a dput of my data:

NDVI2 <- read.table(header = TRUE, text = "
          MeanRED    MeanNIR          NDVI              LAI2 Legend
'LAI 1-1' 3.240264   46.8226195806452 0.870552242769623 1.1  LAI 1
'LAI 1-2' 6.97950484 48.4417953548387 0.748129155560663 1.2  LAI 1
'LAI 1-3' 3.75052276 47.8913064516129 0.854748647859414 1.3  LAI 1
'LAI 1-4' 4.62617908 43.9416386774194 0.809496111062421 1.4  LAI 1
'LAI 2-1' 4.07743944 44.7524788709677 0.832994214160536 2.1  LAI 2
'LAI 2-2' 4.88961572 52.2142607741935 0.828746627367857 2.2  LAI 2
'LAI 2-3' 3.15865532 48.6422146774194 0.878046244390978 2.3  LAI 2
'LAI 2-4' 2.28368236 44.6617992580645 0.902709173224405 2.4  LAI 2
'LAI 3-1' 3.40793788 57.7213822580645 0.888500710549276 3.1  LAI 3
'LAI 3-2' 4.28833416 58.5066447096774 0.863417928083076 3.2  LAI 3
'LAI 3-3' 4.52529496 56.6924350967742 0.852157374806182 3.3  LAI 3
'LAI 3-4' 2.45698208 57.4100250967742 0.917918660181389 3.4  LAI 3
'LAI 4-1' 3.84003364 58.0419292903226 0.875891666709934 4.1  LAI 4
'LAI 4-2' 4.31006672 58.7054423225806 0.863206160341016 4.2  LAI 4
'LAI 4-3' 3.29672264 58.5283540645161 0.893353221193523 4.3  LAI 4
'LAI 4-4' 4.21926652 54.7658463548387 0.856937918252258 4.4  LAI 4
'LAI 5-1' 4.64357012 58.8950077096774 0.853834622095331 5.1  LAI 5
'LAI 5-2' 3.94445908 58.2421209354839 0.873141147848366 5.2  LAI 5
'LAI 5-3' 3.95942484 57.8538210645161 0.871890732089488 5.3  LAI 5
'LAI 5-4' 1.22673756 50.209727516129  0.952300860559358 5.4  LAI 5
'LAI 6-1' 4.70933136 59.5780209354839 0.853491201866442 6.1  LAI 6
'LAI 6-2' 5.33718396 60.1662100645161 0.837040994913869 6.2  LAI 6
'LAI 6-3' 5.71857348 62.1929408387097 0.831587513918106 6.3  LAI 6
'LAI 6-4' 5.7014266  60.3309026451613 0.827314084928549 6.4  LAI 6
'LAI 7-1' 3.85938572 57.859932516129  0.874937512911774 7.1  LAI 7
'LAI 7-2' 6.07816804 63.5678422258065 0.825455384542418 7.2  LAI 7
'LAI 7-3' 2.93602476 55.2536370967742 0.899087753174211 7.3  LAI 7
'LAI 7-4' 5.00289296 60.1808743548387 0.846498808949291 7.4  LAI 7
")
0

1 Answer 1

44

override.aes is definitely a good start for customizing the legend. In your case you may remove unwanted shape in the legend by setting them to NA, and set unwanted linetype to blank:

ggplot(data = NDVI2, aes(x = LAI2, y = NDVI, colour = Legend)) + 
  geom_point(size = 3) +
  geom_smooth(aes(group = 1, colour = "Trendline"),
               method = "loess", se = FALSE, linetype = "dashed") +
  geom_smooth(aes(group = 1, colour = "Regression (log)"),
               method = "lm", formula = y ~ log(x), se = FALSE, linetype = "solid") +
  scale_colour_manual(values = c("purple", "green", "blue", "yellow", "magenta","orange", "cyan", "red", "black"),
                       guide = guide_legend(override.aes = list(
                         linetype = c(rep("blank", 7), "solid", "dashed"),
                         shape = c(rep(16, 7), NA, NA))))

enter image description here

2
  • 1
    Thank you very much Henrik. You inserted "guide" in "scale_colour_manual". If I do that, R tells me that it does not know the function "guide". I modified it then to "+ guides (colour = guide_legend (override.aes = list(linetype = c(rep("blank", 7), "solid", "dashed"), shape = c(rep(16, 7), NA, NA))))" and it worked. Can you explain why? Furthermore, I do not get all the the commands deeply for me in this line. Is this really the only way to do this? I find it a very standard procedure and for that the code is quite complicated to me.
    – pat-s
    Oct 27, 2014 at 15:08
  • By design ggplot has a strong link between what you put in aes (or not) and the resulting appearance of the legend. Customizing legends may be slightly hackish. You may search SO for "guide_legend(override.aes" to look for alternative solutions among the answers. Cheers.
    – Henrik
    Oct 27, 2014 at 15:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.