如何修复添加了 geom_ablines 的分组散点图的图例
How to fix the legend for a grouped scatter plot added with geom_ablines
我想用两个不同的组(基于大小)制作一个散点图,每个组都用指定的颜色(蓝色和红色)绘制。此外,我想向具有相同蓝色和红色的散点图添加 4 geom_ablines(2 条线 一条实线和一条虚线 ,蓝色和 2 条线 一条实线和一条虚线,红色。
然后我想显示一个图例,其中可以看到这 4 条线。即蓝色虚线、红色虚线、蓝色实心和红色实心。
我一直在使用 scale_color 手册,但它并没有帮助我解决问题。
我的代码如下:
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"))
我得到的输出图是:
问题是散点是无色的,而不是根据大小分别为蓝色和红色。而且图例中虚线不是虚线,都是实线
如果有人能帮我解决这个问题,那将是一个很大的帮助。谢谢
使用 dput 的 MRE 是:
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")
对于你的颜色概率,试试这个:
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
这里有几个问题:
关于点颜色的第一个问题很简单:在 ggplot2 中,每种美学只能有一个比例尺——如果您添加另一个比例尺,它将取代第一个。您为第一个块中的点设置颜色,但随后在末尾替换该比例,使点没有颜色。
解决这个问题的简单方法是将 2 个尺度组合成一个 scale_color_manual
,其中包括线条和点的颜色:
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'))
你更大的问题是合并线的图例并隐藏点的图例。为此,您需要使用手动定义的线型摆脱单独的线,并将线型移动到 aes
.
最简单的方法是用所有的线信息制作一个data.frame(在下面的代码中称为lines
),然后使用比例尺为每种类型设置颜色、大小和线型行(名称中的 name
变量)。通过将比例命名为相同(使用 name
参数),如果可能,它将合并它们。
要消除图例中的点颜色,请在 scale_color_manual
中使用 breaks
参数并仅提供线条的名称。它仍然会为 values
中列出的所有值着色,但只有 breaks
中包含的值会出现在图
中
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'))
进一步阅读:
Remove legend entries for some factors levels
我想用两个不同的组(基于大小)制作一个散点图,每个组都用指定的颜色(蓝色和红色)绘制。此外,我想向具有相同蓝色和红色的散点图添加 4 geom_ablines(2 条线 一条实线和一条虚线 ,蓝色和 2 条线 一条实线和一条虚线,红色。
然后我想显示一个图例,其中可以看到这 4 条线。即蓝色虚线、红色虚线、蓝色实心和红色实心。
我一直在使用 scale_color 手册,但它并没有帮助我解决问题。
我的代码如下:
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"))
我得到的输出图是:
问题是散点是无色的,而不是根据大小分别为蓝色和红色。而且图例中虚线不是虚线,都是实线
如果有人能帮我解决这个问题,那将是一个很大的帮助。谢谢
使用 dput 的 MRE 是:
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")
对于你的颜色概率,试试这个:
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
这里有几个问题:
关于点颜色的第一个问题很简单:在 ggplot2 中,每种美学只能有一个比例尺——如果您添加另一个比例尺,它将取代第一个。您为第一个块中的点设置颜色,但随后在末尾替换该比例,使点没有颜色。
解决这个问题的简单方法是将 2 个尺度组合成一个 scale_color_manual
,其中包括线条和点的颜色:
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'))
你更大的问题是合并线的图例并隐藏点的图例。为此,您需要使用手动定义的线型摆脱单独的线,并将线型移动到 aes
.
最简单的方法是用所有的线信息制作一个data.frame(在下面的代码中称为lines
),然后使用比例尺为每种类型设置颜色、大小和线型行(名称中的 name
变量)。通过将比例命名为相同(使用 name
参数),如果可能,它将合并它们。
要消除图例中的点颜色,请在 scale_color_manual
中使用 breaks
参数并仅提供线条的名称。它仍然会为 values
中列出的所有值着色,但只有 breaks
中包含的值会出现在图
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'))
进一步阅读:
Remove legend entries for some factors levels