2

Problem

So I have made a plot for my data, where I compare two groups (A and B) with a third group (C). I plot both the data and confidence intervals for each of the three groups, and I plot a comparison value for the two comparisons. But now, if I make a legend, the legend also shows three colors for the 'comparison level', while there are only two in the plot. I would like to remove that legend entry that is not in the plot.

I use ggplot2in R, with the functions geom_line for the found values, geom_ribbon for the confidence intervals, geom_point and stat_smooth for the comparison value and scale_y_continuous and labs for the labels.

There are a lot of questions that are similar, and I actually did find and read a bunch of them and googled for the set of scale_-functions , but could not figure out how to do this. Apperently there are different functions for different plot types or easthetic types. Not in any answer is this explained, which is why I couldnt get the solution I found online to work.

I have now edited the question to ask how can I tell which function I should use. Is it depenent on the plot function? On the aesthetic? On a single argument of a plot function or a single argument of a plot function? How do people know which function to use? Is there an information source where this should be obvious from?

Not understanding why the answers to the other questions worked kept me from applying it to my own question. Maybe with this question people who read the answer will be able to apply it to their own question.

First try

My plot and code, in yellow I have highlighted the legend entry that I want to delete:

enter image description here

scalerightY = 0.3
ggplot(mydata) + 
  geom_ribbon(aes(x=x, ymin=minc, ymax=maxc, fill="C", linetype = NA), alpha = 0.25) + 
  geom_line(aes(x=x, y=meanc, color="C"), size = 2, show.legend = FALSE) + 
  geom_ribbon(aes(x=x, ymin=mina, ymax=maxa, fill="A", linetype = NA), alpha = 0.25) +
  geom_line(aes(x=x, y=meana, color="A"), size = 2, show.legend = FALSE) +
  geom_ribbon(aes(x=x, ymin=minb, ymax=maxb, fill="B", linetype = NA), alpha = 0.25) +
  geom_line(aes(x=x, y=meanb, color="B"), size = 2, show.legend = FALSE) +
  geom_point(aes(x=x, y=ca/scalerightY, color="A")) + 
  stat_smooth(aes(x=x, y=ca/scalerightY, color="A"),
     level = 0, method = 'lm', formula = y ~ poly(x, 2), size = 1, alpha = 0) + 
  geom_point(aes(x=x, y=cb/scalerightY, color="B")) + 
  stat_smooth(aes(x=x, y=cb/scalerightY, color="B"),
     level = 0, method = 'lm', formula = y ~ poly(x, 2), size = 1, alpha = 0) + 
  scale_y_continuous(sec.axis = sec_axis(~.*scalerightY, name="rightY"), name="leftY") + 
  labs(x = "X", colour = "rightY", fill = "leftY") 

note that I 'abuse' the second axis by scaling my 'comparison value' to use that second axis, as pointed out here and here

Second try

After looking around I found this solution, which did not work. I added this line of code:

+ scale_fill_brewer(breaks=c("B", "A"))

Which resulted in this plot:

enter image description here

Third try

So now from the comments I understand there is a difference between several scale_ functions. Adding + scale_color_discrete(breaks = c("A", "B")) instead had the intended effect. I dont exactly understand why this works.

Resulting plot:

enter image description here

My data (dont know how to make this field appear smaller):

   x      mina     meana      maxa         ca      minc     meanc      maxc      minb     meanb      maxb         cb
2.00 0.6461458 0.7610732 0.8760006 0.16435343 0.7778383 0.8639354 0.9500326 0.6674855 0.7535827 0.8396799 0.16390539
2.05 0.6618140 0.7763001 0.8907862 0.14754071 0.7976274 0.8830411 0.9684548 0.6831575 0.7685712 0.8539849 0.14355787
2.10 0.6655039 0.7821243 0.8987446 0.13122437 0.8088066 0.8955030 0.9821994 0.6974479 0.7841443 0.8708408 0.16991653
2.15 0.6753198 0.7936612 0.9120026 0.12618473 0.8225222 0.9101487 0.9977751 0.7020113 0.7896377 0.8772642 0.14124471
2.20 0.6885779 0.8071658 0.9257537 0.13857250 0.8324007 0.9199134 1.0074261 0.7106201 0.7981328 0.8856455 0.13816100
2.25 0.6966932 0.8140025 0.9313117 0.11289402 0.8475134 0.9334828 1.0194523 0.7253234 0.8112928 0.8972622 0.13602207
2.30 0.7130090 0.8303145 0.9476200 0.11870421 0.8621798 0.9474941 1.0328084 0.7306850 0.8159993 0.9013136 0.10827476
2.35 0.7161802 0.8329748 0.9497693 0.09397712 0.8739884 0.9584388 1.0428891 0.7358591 0.8203095 0.9047599 0.09270097
2.40 0.7249105 0.8404453 0.9559802 0.08518759 0.8849365 0.9679303 1.0509241 0.7395166 0.8225104 0.9055042 0.07253144
2.45 0.7309820 0.8474562 0.9639304 0.08096595 0.8942898 0.9777555 1.0612213 0.7484336 0.8318993 0.9153651 0.07607660
2.50 0.7448127 0.8622723 0.9797319 0.10189884 0.9012492 0.9845239 1.0677986 0.7516805 0.8349552 0.9182298 0.06822410
2.55 0.7497016 0.8680181 0.9863345 0.10082080 0.9076971 0.9915657 1.0754342 0.7535831 0.8374517 0.9213202 0.06279295
2.60 0.7567609 0.8764196 0.9960782 0.11476332 0.9114863 0.9962185 1.0809506 0.7569412 0.8416733 0.9264055 0.06591004
2.65 0.7597420 0.8790883 0.9984347 0.10013129 0.9196737 1.0036932 1.0877127 0.7651536 0.8491732 0.9331927 0.06359087
2.70 0.7576068 0.8777147 0.9978227 0.06370573 0.9355187 1.0203918 1.1052649 0.7753468 0.8602199 0.9450930 0.05798623
2.75 0.7687779 0.8880122 1.0072465 0.06399479 0.9453239 1.0293681 1.1134124 0.7810068 0.8650510 0.9490953 0.05346628
2.80 0.7750593 0.8958139 1.0165685 0.06048174 0.9558725 1.0411292 1.1263859 0.8019675 0.8872242 0.9724809 0.07992454
2.85 0.7814893 0.9038054 1.0261214 0.06422896 0.9623621 1.0488041 1.1352462 0.8073701 0.8938121 0.9802542 0.08131500
2.90 0.7943512 0.9177549 1.0411586 0.06603144 0.9759653 1.0627718 1.1495783 0.8089413 0.8957479 0.9825544 0.06494231
2.95 0.8047134 0.9315883 1.0584632 0.08106105 0.9834652 1.0724684 1.1614715 0.8119702 0.9009733 0.9899765 0.06543932
3.00 0.8049914 0.9324786 1.0599659 0.05806431 0.9975345 1.0873743 1.1772141 0.8191974 0.9090372 0.9988771 0.05883210
3.05 0.8138321 0.9462364 1.0786407 0.07463853 1.0040828 1.0963512 1.1886196 0.8251199 0.9173883 1.0096567 0.06192272
3.10 0.8142506 0.9456681 1.0770857 0.05775772 1.0133886 1.1049525 1.1965165 0.8371856 0.9287495 1.0203135 0.06404170
3.15 0.8289644 0.9632873 1.0976103 0.08345103 1.0179335 1.1101953 1.2024571 0.8470854 0.9393471 1.0316089 0.07107393
3.20 0.8319758 0.9704551 1.1089345 0.08122525 1.0276616 1.1234056 1.2191496 0.8310997 0.9268437 1.0225877 0.04595206
3.25 0.8527423 0.9904988 1.1282554 0.11060893 1.0342651 1.1285709 1.2228767 0.8344678 0.9287736 1.0230794 0.04066026
3.30 0.8581813 0.9955275 1.1328737 0.08940326 1.0485558 1.1426180 1.2366802 0.8471285 0.9411907 1.0352529 0.03765280
3.35 0.8608498 1.0031897 1.1455296 0.09573609 1.0550938 1.1524787 1.2498635 0.8584508 0.9558357 1.0532205 0.04765752
3.40 0.8642790 1.0056253 1.1469715 0.06629366 1.0732143 1.1702380 1.2672616 0.8604650 0.9574887 1.0545123 0.03266336
3.45 0.8677770 1.0127129 1.1576487 0.08579514 1.0703502 1.1700298 1.2697094 0.8734595 0.9731391 1.0728187 0.05968259
3.50 0.8772613 1.0254325 1.1736038 0.10418563 1.0756049 1.1766409 1.2776769 0.8769335 0.9779695 1.0790055 0.05870895
3.55 0.8827386 1.0302883 1.1778379 0.10426107 1.0802684 1.1807800 1.2812915 0.8774008 0.9779124 1.0784240 0.05375628
3.60 0.8963371 1.0455869 1.1948368 0.13591836 1.0830703 1.1846982 1.2863261 0.8907250 0.9923529 1.0939808 0.07071473
3.65 0.9066654 1.0559085 1.2051517 0.14553138 1.0900412 1.1914477 1.2928542 0.8965193 0.9979258 1.0993323 0.07526640
3.70 0.9109158 1.0586797 1.2064435 0.13675123 1.0957062 1.1959312 1.2961562 0.9158639 1.0160888 1.1163138 0.09498432
3.75 0.9175108 1.0643390 1.2111671 0.13874568 1.1003755 1.2000271 1.2996786 0.9219649 1.0216165 1.1212680 0.09689967
3.80 0.9247998 1.0748388 1.2248779 0.16992115 1.1014572 1.2027613 1.3040654 0.9327890 1.0340930 1.1353971 0.11995379
3.85 0.9248931 1.0745703 1.2242475 0.14912451 1.1077500 1.2091058 1.3104617 0.9416573 1.0430132 1.1443691 0.12523666
3.90 0.9278648 1.0775893 1.2273138 0.13201484 1.1173173 1.2181110 1.3189047 0.9422336 1.0430273 1.1438210 0.10583221
3.95 0.9258139 1.0794600 1.2331061 0.13368080 1.1191619 1.2233362 1.3275106 0.9397362 1.0439106 1.1480849 0.10951927
4.00 0.9330049 1.0877007 1.2423965 0.13813411 1.1260516 1.2307773 1.3355031 0.9419072 1.0466329 1.1513587 0.10380683
1.96 0.6371099 0.7509032 0.8646965 0.17252464 0.7654580 0.8507632 0.9360683 0.6562682 0.7415733 0.8268785 0.16496086 
Leo
  • 1,757
  • 3
  • 20
  • 44
  • `+ scale_color_discrete(breaks = c("A", "B"))`? – Axeman Aug 14 '17 at 12:06
  • @Axeman, that does not work, I can add it to the question but will make it less legible, one sec – Leo Aug 14 '17 at 12:07
  • @Leo you do plot a colored line for C though. The aesthetic is shared. – Axeman Aug 14 '17 at 12:07
  • 1
    The solution of duplicate question and @Axeman works for me – Didzis Elferts Aug 14 '17 at 12:10
  • My solution seems to work fine for me? What's wrong with it? You can change the legend order if you need to. – Axeman Aug 14 '17 at 12:11
  • @Axeman I tried `scale_fill_brewer` before which only changed the `leftY` legend, but now that I use `scale_color_discrete` it works on the `rightY` legend. I googled a lot on `scale_`-something functions but could not figure this out. Can you explain why this approach does exactly what I want? That would be exactly the answer I was looking for. – Leo Aug 14 '17 at 12:11
  • 1
    Because the legend you pointed out to change is the color legend, not the fill legend. Color and fill are not the same in ggplot. – Axeman Aug 14 '17 at 12:14
  • Well the answer happens to work, but thequestion is slightly different. It could also be a duplicate of https://stackoverflow.com/questions/23338830/ggplot2-use-scale-colour-brewer-and-specify-legend-name-entry-labels-manual but that answer does not solve my question. And neither answers explain why the one solution works and the other does not. I'm responding to your comment on it being duplicate. – Leo Aug 14 '17 at 12:15
  • 1
    Didzis and I disagree, the second question there is in principal the same question as yours. The one you link is about labels, not legend entries. Don't see it as a criticism, it probably is not so easy to find. I do agree that the answer at the duplicate could do with some more explanation. – Axeman Aug 14 '17 at 12:21
  • Ok I get it, thanks. Should I edit the answer in the other question if I find out how people know which function to use? And should I delete this question? – Leo Aug 14 '17 at 12:36
  • It's probably best to put a comment at that answer, and you can leave this question here, it will serve as a link for others. – Axeman Aug 14 '17 at 14:19

0 Answers0