Fixing legend in ggplot

I am plotting a scatter plot with 4 geom_ablines. The current plot looks like below:
stackoverflow

I want to make two changes in the legend of this plot.

  1. Removing the dot points on lines in the legend.
  2. Making the lines in the legend horizontal instead of tilted. Is it possible because the lines have been drawn using geom_abline.
fig <- ggplot(scalingdata, aes(x=Re, y=Sw, color=Size)) + 
  geom_point(size=3) +
  theme_minimal() +
  theme(panel.background = element_rect(fill = "#f0f4f5",
                                        size = 0.5, linetype = "solid"),
        panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.border = element_rect(color = "black",fill = NA,size = 1),
        plot.title = element_text(size = 25, face = "italic"),
        axis.title.y = element_text(size=17.5),
        axis.text.y = element_text(size=15, face="bold"),
        axis.title.x = element_blank(),
        axis.text.x = element_blank())+
  scale_x_log10(limits = c(1,10^8), breaks = trans_breaks("log10", function(x) 10^x),
                labels = trans_format("log10", math_format(10^.x))) + 
  scale_y_log10(limits = c(1,10^8), breaks = trans_breaks("log10", function(x) 10^x),
                labels = trans_format("log10", math_format(10^.x))) + annotation_logticks()

lines <- data.frame(name = c("Sw ~ Re^13/15", "Sw ~ Re^9/10", "Sw ~ Re^2/3", "Sw ~ Re^3/4"),
                    intercept = c(1, 0.79, 1.35, 1.1),
                    slope = c(13/15, 9/10, 2/3, 3/4))

fig + geom_abline(data = lines, aes(intercept=intercept, slope=slope, color=name, size=name, linetype=name))+ # color=name, size=name, linetype=name)+
  scale_color_manual(name='Size',
                     values=c("Sw ~ Re^13/15" ='Blue', "Sw ~ Re^9/10" ='Blue',
                              "Sw ~ Re^2/3" ='Red', "Sw ~ Re^3/4" ='Red',
                              'Fish' = 'Blue', 'Larvae' = 'Red'),
                     breaks=lines$name) +
  scale_size_manual(name='Size',
                    values=c("Sw ~ Re^13/15" =1, "Sw ~ Re^9/10" =1.25, "Sw ~ Re^2/3" =1, "Sw ~ Re^3/4" = 1.25)) +
  scale_linetype_manual(name='Size',
                        values=c("Sw ~ Re^13/15" ='solid', "Sw ~ Re^9/10" ='dashed',
                                 "Sw ~ Re^2/3" ='solid', "Sw ~ Re^3/4" ='dashed'))

The minimum reproducible example is:

structure(list(X = 400:585, Sw = c(762000, 781000, 794000, 823000, 
846000, 849000, 853000, 882000, 887000, 898000, 941000, 979000, 
1030000, 1060000, 1070000, 1080000, 1090000, 1140000, 1170000, 
6469900, 7584900, 7914000, 7995300, 8139300, 8254100, 8482100, 
8485600, 8557900, 8642000, 8730800, 8734400, 8779000, 8812900, 
8869100, 8902600, 8914600, 8975200, 9075000, 9203000, 9269900, 
9481200, 9565700, 9656200, 9664300, 9821400, 9825700, 9989900, 
10410400, 10552900, 10820800, 10959400, 11449900, 11855900, 41766.8, 
44815, 46421, 51017, 52530, 54453, 55151, 60703, 60748, 61022, 
61850, 62083, 64308, 65524, 66017, 66115, 66963, 69001, 70887, 
74872, 74928, 76174, 85619, 85940, 300720, 349790, 414460, 507870, 
538620, 540430, 570270, 595730, 617120, 667810, 687150, 697620, 
704690, 717830, 741100, 754920, 760010, 769000, 825210, 875180, 
899020, 1042220, 1665250, 1687780, 1930520, 2071620, 1484730, 
1560040, 2205640, 3198090, 3225130, 4840500, 73.698, 91.148, 
91.843, 126.687, 130.467, 155.199, 232.717, 602.22, 615.05, 619.05, 
626.62, 655.73, 677.34, 747.73, 768.65, 778.66, 816.82, 859.61, 
885.06, 906.12, 925.43, 975.49, 1020.79, 1024.12, 1091.84, 1094.5, 
1114.2, 1122.36, 1165.94, 1290.31, 1291.33, 1302.88, 1361.19, 
1477.34, 1543.46, 1564.86, 1609.89, 1679.22, 1720.21, 1748.66, 
1769.04, 1778.18, 1804.09, 1816.32, 1842.21, 1861.27, 1861.88, 
1875.09, 1957.71, 1975.41, 2028.23, 2049.21, 2193.15, 2214.41, 
2320.91, 190.301, 213.437, 215.963, 217.451, 226.594, 227.932, 
243.172, 244.607, 292.403, 297.318, 308.91, 310.428, 319.697, 
332.16, 339.738, 345.449, 363.165, 388.971, 399.016, 414.572, 
415.794, 453.714), Re = c(401000, 343000, 537000, 414000, 595000, 
392000, 448000, 421000, 633000, 496000, 633000, 466000, 648000, 
524000, 729000, 516000, 504000, 795000, 802000, 4114000, 3809200, 
4687400, 4130800, 4228400, 4891300, 3757100, 3974800, 4798000, 
4516600, 4074300, 4310400, 4479500, 3904400, 4358000, 6464600, 
3851100, 4824400, 4412200, 5069000, 4642300, 4316300, 6473400, 
4096700, 4572600, 5340600, 5041200, 5224500, 5429300, 5481800, 
5952800, 7097300, 7165900, 7781600, 25778, 29630, 20784, 28523, 
38410, 36744, 33414, 56160, 41702, 49165, 30384, 42771, 33623, 
46439, 47329, 24810, 38161, 56150, 51390, 47929, 54400, 56860, 
59810, 67890, 303110, 236540, 282380, 354240, 408170, 391180, 
377560, 408170, 391180, 402420, 526760, 435040, 411070, 585800, 
508420, 450730, 497730, 463680, 477010, 745400, 553540, 846800, 
889800, 935100, 1047300, 1148400, 553800, 723800, 746600, 1720200, 
2939400, 2374500, 10.115, 10.11, 12.227, 29.025, 20.106, 10.165, 
17.613, 83.12, 105.2, 123.1, 148.64, 113.79, 111.14, 333.77, 
120.21, 283, 206.68, 287.45, 341.67, 318.34, 291.98, 366.66, 
456.8, 296.57, 467.7, 439.2, 415.7, 390.39, 456.8, 308.38, 478.8, 
671.2, 606, 587.2, 542.8, 749, 760.9, 692.4, 659.3, 667.6, 643, 
560.6, 777.7, 686.5, 735.2, 856.6, 690.8, 870, 710.4, 894.7, 
730.6, 825, 817.3, 920.1, 880.8, 25.031, 32.901, 29.787, 27.193, 
23.62, 29.056, 30.791, 23.425, 30.537, 36.339, 50.199, 58.272, 
52.758, 54.085, 68.21, 27.193, 71.09, 50.617, 48.967, 44.702, 
73.49, 73.49), Species = c("Dace, Trout, and Goldfish", "Dace, Trout, and Goldfish", 
"Dace, Trout, and Goldfish", "Dace, Trout, and Goldfish", "Dace, Trout, and Goldfish", 
"Dace, Trout, and Goldfish", "Dace, Trout, and Goldfish", "Dace, Trout, and Goldfish", 
"Dace, Trout, and Goldfish", "Dace, Trout, and Goldfish", "Dace, Trout, and Goldfish", 
"Dace, Trout, and Goldfish", "Dace, Trout, and Goldfish", "Dace, Trout, and Goldfish", 
"Dace, Trout, and Goldfish", "Dace, Trout, and Goldfish", "Dace, Trout, and Goldfish", 
"Dace, Trout, and Goldfish", "Dace, Trout, and Goldfish", "Giant Bluefin Tuna", 
"Giant Bluefin Tuna", "Giant Bluefin Tuna", "Giant Bluefin Tuna", 
"Giant Bluefin Tuna", "Giant Bluefin Tuna", "Giant Bluefin Tuna", 
"Giant Bluefin Tuna", "Giant Bluefin Tuna", "Giant Bluefin Tuna", 
"Giant Bluefin Tuna", "Giant Bluefin Tuna", "Giant Bluefin Tuna", 
"Giant Bluefin Tuna", "Giant Bluefin Tuna", "Giant Bluefin Tuna", 
"Giant Bluefin Tuna", "Giant Bluefin Tuna", "Giant Bluefin Tuna", 
"Giant Bluefin Tuna", "Giant Bluefin Tuna", "Giant Bluefin Tuna", 
"Giant Bluefin Tuna", "Giant Bluefin Tuna", "Giant Bluefin Tuna", 
"Giant Bluefin Tuna", "Giant Bluefin Tuna", "Giant Bluefin Tuna", 
"Giant Bluefin Tuna", "Giant Bluefin Tuna", "Giant Bluefin Tuna", 
"Giant Bluefin Tuna", "Giant Bluefin Tuna", "Giant Bluefin Tuna", 
"Stingrays", "Stingrays", "Stingrays", "Stingrays", "Stingrays", 
"Stingrays", "Stingrays", "Stingrays", "Stingrays", "Stingrays", 
"Stingrays", "Stingrays", "Stingrays", "Stingrays", "Stingrays", 
"Stingrays", "Stingrays", "Stingrays", "Stingrays", "Stingrays", 
"Stingrays", "Stingrays", "Stingrays", "Stingrays", "Saithe and Mackerel", 
"Saithe and Mackerel", "Saithe and Mackerel", "Saithe and Mackerel", 
"Saithe and Mackerel", "Saithe and Mackerel", "Saithe and Mackerel", 
"Saithe and Mackerel", "Saithe and Mackerel", "Saithe and Mackerel", 
"Saithe and Mackerel", "Saithe and Mackerel", "Saithe and Mackerel", 
"Saithe and Mackerel", "Saithe and Mackerel", "Saithe and Mackerel", 
"Saithe and Mackerel", "Saithe and Mackerel", "Saithe and Mackerel", 
"Saithe and Mackerel", "Saithe and Mackerel", "Saithe and Mackerel", 
"Saithe and Mackerel", "Saithe and Mackerel", "Saithe and Mackerel", 
"Saithe and Mackerel", "Sharks", "Sharks", "Sharks", "Sharks", 
"Sharks", "Sharks", "Ascidian Larvae", "Ascidian Larvae", "Ascidian Larvae", 
"Ascidian Larvae", "Ascidian Larvae", "Ascidian Larvae", "Ascidian Larvae", 
"Larval Zebrafish40", "Larval Zebrafish40", "Larval Zebrafish40", 
"Larval Zebrafish40", "Larval Zebrafish40", "Larval Zebrafish40", 
"Larval Zebrafish40", "Larval Zebrafish40", "Larval Zebrafish40", 
"Larval Zebrafish40", "Larval Zebrafish40", "Larval Zebrafish40", 
"Larval Zebrafish40", "Larval Zebrafish40", "Larval Zebrafish40", 
"Larval Zebrafish40", "Larval Zebrafish40", "Larval Zebrafish40", 
"Larval Zebrafish40", "Larval Zebrafish40", "Larval Zebrafish40", 
"Larval Zebrafish40", "Larval Zebrafish40", "Larval Zebrafish40", 
"Larval Zebrafish40", "Larval Zebrafish40", "Larval Zebrafish40", 
"Larval Zebrafish40", "Larval Zebrafish40", "Larval Zebrafish40", 
"Larval Zebrafish40", "Larval Zebrafish40", "Larval Zebrafish40", 
"Larval Zebrafish40", "Larval Zebrafish40", "Larval Zebrafish40", 
"Larval Zebrafish40", "Larval Zebrafish40", "Larval Zebrafish40", 
"Larval Zebrafish40", "Larval Zebrafish40", "Larval Zebrafish40", 
"Larval Zebrafish40", "Larval Zebrafish40", "Larval Zebrafish40", 
"Larval Zebrafish40", "Larval Zebrafish40", "Larval Zebrafish40", 
"Larval Zebrafish61", "Larval Zebrafish61", "Larval Zebrafish61", 
"Larval Zebrafish61", "Larval Zebrafish61", "Larval Zebrafish61", 
"Larval Zebrafish61", "Larval Zebrafish61", "Larval Zebrafish61", 
"Larval Zebrafish61", "Larval Zebrafish61", "Larval Zebrafish61", 
"Larval Zebrafish61", "Larval Zebrafish61", "Larval Zebrafish61", 
"Larval Zebrafish61", "Larval Zebrafish61", "Larval Zebrafish61", 
"Larval Zebrafish61", "Larval Zebrafish61", "Larval Zebrafish61", 
"Larval Zebrafish61"), Size = c("Fish", "Fish", "Fish", "Fish", 
"Fish", "Fish", "Fish", "Fish", "Fish", "Fish", "Fish", "Fish", 
"Fish", "Fish", "Fish", "Fish", "Fish", "Fish", "Fish", "Fish", 
"Fish", "Fish", "Fish", "Fish", "Fish", "Fish", "Fish", "Fish", 
"Fish", "Fish", "Fish", "Fish", "Fish", "Fish", "Fish", "Fish", 
"Fish", "Fish", "Fish", "Fish", "Fish", "Fish", "Fish", "Fish", 
"Fish", "Fish", "Fish", "Fish", "Fish", "Fish", "Fish", "Fish", 
"Fish", "Fish", "Fish", "Fish", "Fish", "Fish", "Fish", "Fish", 
"Fish", "Fish", "Fish", "Fish", "Fish", "Fish", "Fish", "Fish", 
"Fish", "Fish", "Fish", "Fish", "Fish", "Fish", "Fish", "Fish", 
"Fish", "Fish", "Fish", "Fish", "Fish", "Fish", "Fish", "Fish", 
"Fish", "Fish", "Fish", "Fish", "Fish", "Fish", "Fish", "Fish", 
"Fish", "Fish", "Fish", "Fish", "Fish", "Fish", "Fish", "Fish", 
"Fish", "Fish", "Fish", "Fish", "Fish", "Fish", "Fish", "Fish", 
"Fish", "Larvae", "Larvae", "Larvae", "Larvae", "Larvae", "Larvae", 
"Larvae", "Larvae", "Larvae", "Larvae", "Larvae", "Larvae", "Larvae", 
"Larvae", "Larvae", "Larvae", "Larvae", "Larvae", "Larvae", "Larvae", 
"Larvae", "Larvae", "Larvae", "Larvae", "Larvae", "Larvae", "Larvae", 
"Larvae", "Larvae", "Larvae", "Larvae", "Larvae", "Larvae", "Larvae", 
"Larvae", "Larvae", "Larvae", "Larvae", "Larvae", "Larvae", "Larvae", 
"Larvae", "Larvae", "Larvae", "Larvae", "Larvae", "Larvae", "Larvae", 
"Larvae", "Larvae", "Larvae", "Larvae", "Larvae", "Larvae", "Larvae", 
"Larvae", "Larvae", "Larvae", "Larvae", "Larvae", "Larvae", "Larvae", 
"Larvae", "Larvae", "Larvae", "Larvae", "Larvae", "Larvae", "Larvae", 
"Larvae", "Larvae", "Larvae", "Larvae", "Larvae", "Larvae", "Larvae", 
"Larvae"), St = c(0.302433374165808, 0.362389853900744, 0.235323518267981, 
0.316386985184939, 0.226293726046568, 0.344699568138944, 0.303033145076212, 
0.333430716272689, 0.223017542504579, 0.288146783640142, 0.236594709692005, 
0.334361212296077, 0.252977172911813, 0.321953903004644, 0.233601359236799, 
0.333114218203138, 0.344203337900817, 0.228221648865378, 0.232183102213908, 
0.250295128437197, 0.316909457357351, 0.268709577645423, 0.308048935041855, 
0.306358622430061, 0.268574355248053, 0.359310368633893, 0.339771064454215, 
0.283874302307228, 0.3045241557553, 0.341051647591493, 0.322503638696486, 
0.311913824779625, 0.359239145081061, 0.323900368274474, 0.219176667244061, 
0.368414047393468, 0.296087410852063, 0.327348653980426, 0.288952360656955, 
0.317805173505505, 0.349599499061661, 0.23518164782688, 0.375138127690756, 
0.336377010833042, 0.292686346632698, 0.310204924915008, 0.304323523404437, 
0.30517060120053, 0.306385178491378, 0.289305836077696, 0.245760867447159, 
0.254302170218483, 0.242484923070044, 0.257870165748749, 0.240719274240546, 
0.355471244064845, 0.2846681185215, 0.217661796677678, 0.235860109140741, 
0.262690301692974, 0.17202920082822, 0.231843122013434, 0.197537480456343, 
0.323976771042564, 0.231016184479705, 0.304402110063851, 0.224562202134299, 
0.221997238656593, 0.424123516982184, 0.279276421804133, 0.195580134425664, 
0.219536681625896, 0.248622358777944, 0.219212016357345, 0.213215622321579, 
0.227832389358595, 0.20146919759615, 0.157899648862937, 0.235354178413181, 
0.233597292524979, 0.228178161508506, 0.210019930393074, 0.219878073423756, 
0.240388487016993, 0.232288390949214, 0.251079985698921, 0.264114641433334, 
0.207614593756514, 0.255216558855981, 0.272835854188107, 0.19502547907645, 
0.231992157091137, 0.266565280053204, 0.243021448263837, 0.263953297612413, 
0.27533163553866, 0.186864632918074, 0.258487448689974, 0.195883416277148, 
0.297855865613987, 0.287261150364813, 0.293374469828208, 0.28710187409247, 
0.426691163020906, 0.343033292092993, 0.47018174096222, 0.295890385833758, 
0.17462549544427, 0.32444207224037, 1.15960193451398, 1.43487842039073, 
1.1954881211204, 0.694670633111126, 1.03274740893889, 2.42996854423028, 
2.10287709536259, 1.15310473651356, 0.930494475702483, 0.800362608672448, 
0.670946160047344, 0.917149369794335, 0.969963623056172, 0.356546865546547, 
1.01767041791117, 0.437905647321569, 0.628994758168588, 0.475946636660009, 
0.41227308378791, 0.453016082808868, 0.504440080025434, 0.423426859723028, 
0.355655416919196, 0.549594966348079, 0.371544423161221, 0.396618119014455, 
0.426581724604903, 0.457564804982094, 0.406227408970275, 0.66592753876014, 
0.429242005148684, 0.308938181405404, 0.357491106253483, 0.400417939035907, 
0.452558426733727, 0.332516184043711, 0.336734593477769, 0.385984313372426, 
0.415257522282799, 0.416877219867739, 0.437870609858534, 0.504826034458151, 
0.3692029958207, 0.421086132923672, 0.398797799365072, 0.345820252410603, 
0.428961656779362, 0.343022005040261, 0.43859585449659, 0.351397709779422, 
0.441831474644956, 0.395322591846656, 0.42707677378149, 0.383038227789757, 
0.419372655758695, 1.20999057171145, 1.0324754309382, 1.15390937655487, 
1.27269225907284, 1.52681925668428, 1.24850012592141, 1.25692362442215, 
1.66191343132771, 1.52396351968135, 1.30216898419267, 0.979390794690607, 
0.84785205830123, 0.964426984707154, 0.977438888352537, 0.792711561574157, 
2.02183603756457, 0.813045009817121, 1.22303796362965, 1.29689832478866, 
1.47601961377895, 0.900469664727307, 0.982591604164768)), row.names = 400:585, class = "data.frame")

Any help would be appreciated. Thank you

Hi! Not sure how to do change 2, but to remove the dots in the legend you can add

+ guides(colour = guide_legend(override.aes = list(shape = NA)))

to your figure.

For question 2, consider what is written in this link:

1 Like

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.