How to fix the legend for a grouped scatter plot added with geom_ablines
I want to make a scatter plot with two different groups (based on Size) each plotted with specified colors (Blue and Red). In addition, I want to add 4 geom_ablines to the scatter plot with the same Blue and Red colors (2 lines one solid and one dashed with the Blue color and 2 lines one solid and one dashed with the Red color.
Then I want to show a legend where those 4 lines are visible. i.e. Dashed Blue, Dashed Red, Solid Blue, and Solid Red.
I have been using the scale_color manual but it is not helping me solve my issue.
The code I have is as follows:
cols <- c("Blue", "Red")
###Plotting data
fig <- ggplot(scalingdata, aes(x=Re, y=Sw, color=Size)) +
geom_point(size=3) +
scale_color_manual(values=cols) +
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())
# legend.position ="none") # If no legend to be shown
# Converting scale with better numbering style format
fig <- fig + 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()
# ###Scaling fit by Vladimir Nikora___Dashed for Fishes and Solid line for Larva
fig <- fig + geom_abline(aes(intercept=1, slope = 13/15, color="Sw ~ Re^13/15"), size=1, linetype="solid") #Undulating High Re
fig <- fig + geom_abline(aes(intercept = 0.79, slope = 9/10, color="Sw ~ Re^9/10"), size=1.25, linetype="dashed") #Motionless High Re
fig <- fig + geom_abline(aes(intercept = 1.35, slope = 2/3, color="Sw ~ Re^2/3"), size=1, linetype="solid") #Undulating Low Re
fig <- fig + geom_abline(aes(intercept = 1.1, slope = 3/4, color="Sw ~ Re^3/4"), size=1.25, linetype="dashed") #Motionless Low Re
fig <- fig + scale_color_manual(values=c("Sw ~ Re^13/15"= "Blue" ,"Sw ~ Re^9/10"="Blue",
"Sw ~ Re^2/3" = "Red", "Sw ~ Re^3/4" = "Red"))
The output plot I am getting is:
The issue is that the scatter points are colorless instead of being BLUE and RED respectively based on Size. Moreover, in the legend, the dashed lines are not dashed but all of them are solid.
If someone can help me with this, it would be a great help. Thank you
THE MRE using dput is:
structure(list(Sw = c(118612, 119946, 122211, 122442, 124753,
126631, 128301, 128781, 135193, 137232, 137489, 137744, 138263,
141664, 144336, 150394, 152376, 152664, 156417, 158480, 160867,
162985, 170000, 172000, 172000, 175000, 183000, 185000, 185000,
193000, 198000, 202000, 206000, 209000, 213000, 213000, 214000,
221000, 221000, 221000, 221000, 232000, 236000, 236000, 240000,
240000, 241000, 248000, 251000, 256000, 258000, 260000, 271000,
272000, 274000, 292000, 297000, 299000, 308000, 315000, 327000,
327000, 329000, 342000, 359000, 366000, 368000, 376000, 377000,
387000, 394000, 396000, 407000, 416000, 420000, 428000, 431000,
456000, 456000, 463000, 473000, 490000, 491000, 498000, 512000,
518000, 524000, 528000, 574000, 592000, 609000, 611000, 631000,
634000, 646000, 669000, 672000, 677000, 703000, 751000, 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(77600, 36434, 49900,
70000, 81300, 54900, 83300, 73400, 49100, 70000, 78800, 53200,
86600, 79400, 71700, 50700, 58000, 97400, 64200, 92200, 79400,
70000, 69500, 95900, 54800, 74500, 128000, 61200, 94400, 121000,
108000, 139000, 102000, 156000, 46100, 125000, 83200, 113000,
123000, 94400, 146000, 88600, 82600, 155000, 102000, 155000,
125000, 169000, 122000, 81900, 114000, 159000, 133000, 155000,
114000, 169000, 144000, 209000, 156000, 189000, 137000, 168000,
186000, 187000, 184000, 186000, 246000, 152000, 231000, 332000,
202000, 222000, 239000, 214000, 302000, 219000, 307000, 260000,
312000, 241000, 348000, 235000, 196000, 324000, 244000, 324000,
357000, 250000, 357000, 380000, 337000, 3e+05, 351000, 414000,
389000, 293000, 417000, 335000, 374000, 512000, 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",
"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", "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", "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",
"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", "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", "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", "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.243268582034501, 0.523959877133545, 0.389788362056428,
0.278388628178544, 0.244219067477153, 0.367102048635318, 0.245134340978561,
0.279238212500755, 0.438219631492664, 0.31201571537706, 0.277690390877163,
0.412078707080919, 0.254101483129468, 0.283960618825043, 0.320386808203692,
0.472108334099284, 0.418126498248202, 0.249457632595693, 0.387764494072905,
0.273566338387061, 0.322452372293089, 0.370568682018262, 0.38929895045003,
0.285449282566378, 0.499536244491161, 0.373853018937899, 0.227541300611154,
0.481104503595853, 0.311902495975278, 0.253858117251408, 0.291783380018674,
0.231289376443841, 0.321429819699716, 0.213226316167493, 0.73535626042963,
0.271199388846448, 0.409363920393332, 0.311266904187258, 0.28596065181431,
0.372597035732629, 0.240912055980549, 0.416747860646413, 0.454727345483648,
0.242325669270641, 0.374481343339475, 0.246432884004042, 0.306850012732366,
0.233552269751365, 0.327440961481463, 0.497479489076128, 0.360191923659416,
0.260252757478063, 0.324292396384391, 0.279290601871247, 0.382529407297209,
0.274988962771768, 0.328256302521008, 0.227690032080688, 0.314228255404726,
0.265257618198795, 0.37987988825258, 0.309783004110735, 0.281515343185172,
0.291074135050228, 0.310524407391415, 0.313175123421803, 0.23808488657843,
0.393698149116106, 0.259745771586872, 0.185520538993253, 0.310430202684134,
0.283897342720872, 0.271028913657513, 0.309384586459903, 0.221340793993696,
0.311041809833107, 0.223438501844327, 0.279132632073809, 0.232610526728174,
0.305761686039938, 0.216322161048327, 0.331854211704024, 0.398698440415127,
0.244626470116666, 0.333963689797827, 0.254450826346251, 0.233605028464149,
0.336134453781513, 0.255895584615308, 0.247946068379505, 0.287611672670147,
0.324144809438927, 0.286115482193914, 0.243729463678313, 0.264302963531755,
0.363393883771318, 0.256479308531784, 0.321634759738359, 0.299159527690512,
0.233447427266361, 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 = 300:585, class = "data.frame")
There are a few issues here:
Your first problem with the point colors is simple: in ggplot2 you can only have a single scale for each aesthetic – if you add another one, it will replace the first. You set the colors for the points in the first block, but then replace that scale at the end, leaving the points without color.
The simple way to get around that would be to combine the 2 scales into a single scale_color_manual
which includes colors for the lines and points:
scale_color_manual(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'))
Your bigger problem is combining the legend for the lines and hiding the legend for the points. To do that, you need to get rid of the separate lines, with manually defined linetypes, and move the linetype into the aes
.
The easiest way is to make a data.frame with all the line information (called lines
in the code below), then use scales to set the color, size, and linetype for each kind of line (the name
variable in names). By naming the scales the same (with the name
argument), it will combine them if possible.
To eliminate the point colors from the legend, use the breaks
argument in scale_color_manual
and provide the names of the lines only. It will still color all the values listed in values
, but only the ones included in breaks
will appear on the plot
fig <- ggplot(scalingdata, aes(x=Re, y=Sw, color=Size)) +
geom_point(size=3, show.legend = FALSE) +
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))+
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'))
Some further reading:
Remove legend entries for some factors levels
How to merge color, line style and shape legends in ggplot
For you colour prob, try this :
fig <- ggplot(scalingdata, aes(x=Re, y=Sw, colour=Size)) +
geom_point( size=3) +
scale_color_manual(values=cols) +
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())
# legend.position ="none") # If no legend to be shown
# Converting scale with better numbering style format
fig <- fig + 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()
cols2 <- c("Blue", "Red", "Blue", "Red", "Red", "Blue")
# ###Scaling fit by Vladimir Nikora___Dashed for Fishes and Solid line for Larva
fig <- fig + geom_abline(aes(intercept=1, slope = 13/15, color="Sw ~ Re^13/15"), size=1, linetype="solid") #Undulating High Re
fig <- fig + geom_abline(aes(intercept = 0.79, slope = 9/10, color="Sw ~ Re^9/10"), size=1.25, linetype="dashed") #Motionless High Re
fig <- fig + geom_abline(aes(intercept = 1.35, slope = 2/3, color="Sw ~ Re^2/3"), size=1, linetype="solid") #Undulating Low Re
fig <- fig + geom_abline(aes(intercept = 1.1, slope = 3/4, color="Sw ~ Re^3/4"), size=1.25, linetype="dashed") #Motionless Low Re
fig <- fig + scale_color_manual(values=cols2)
fig