主頁 > 企業開發 > 如何修復使用geom_ablines添加的分組散點圖的圖例

如何修復使用geom_ablines添加的分組散點圖的圖例

2022-01-21 00:19:08 企業開發

我想用兩個不同的組(基于大小)制作一個散點圖,每個組都用指定的顏色(藍色和紅色)繪制。此外,我想在散點圖中添加 4 個 geom_ablines,它們具有相同的藍色和紅色(2 條實線,1 條藍色虛線,2 條實線,1 條紅色虛線。

然后我想展示一個可以看到這 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"))

我得到的輸出圖是: 如何修復使用 geom_ablines 添加的分組散點圖的圖例

問題是散點是無色的,而不是基于大小分別為藍色和紅色。此外,在圖例中,虛線不是虛線,而是全部是實線。

如果有人可以幫助我,那將是一個很大的幫助。謝謝

使用 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")

uj5u.com熱心網友回復:

對于你的顏色概率,試試這個:

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 

uj5u.com熱心網友回復:

這里有幾個問題:

點顏色的第一個問題很簡單:在 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引數),如果可能,它將組合它們。

要從圖例中消除點顏色,請使用breaks引數 inscale_color_manual并僅提供線的名稱。它仍然會為 中列出的所有值著色values,但只有 中包含的值breaks會出現在圖上

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)) 
    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'))

如何修復使用 geom_ablines 添加的分組散點圖的圖例

一些進一步的閱讀:

洗掉某些因子水平的圖例條目

如何在 ggplot 中合并顏色、線條樣式和形狀圖例

轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/416769.html

標籤:

上一篇:將條形從低到高重新排序

下一篇:ggplot到底在用` `運算子做什么?

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • IEEE1588PTP在數字化變電站時鐘同步方面的應用

    IEEE1588ptp在數字化變電站時鐘同步方面的應用 京準電子科技官微——ahjzsz 一、電力系統時間同步基本概況 隨著對IEC 61850標準研究的不斷深入,國內外學者提出基于IEC61850通信標準體系建設數字化變電站的發展思路。數字化變電站與常規變電站的顯著區別在于程序層傳統的電流/電壓互 ......

    uj5u.com 2020-09-10 03:51:52 more
  • HTTP request smuggling CL.TE

    CL.TE 簡介 前端通過Content-Length處理請求,通過反向代理或者負載均衡將請求轉發到后端,后端Transfer-Encoding優先級較高,以TE處理請求造成安全問題。 檢測 發送如下資料包 POST / HTTP/1.1 Host: ac391f7e1e9af821806e890 ......

    uj5u.com 2020-09-10 03:52:11 more
  • 網路滲透資料大全單——漏洞庫篇

    網路滲透資料大全單——漏洞庫篇漏洞庫 NVD ——美國國家漏洞庫 →http://nvd.nist.gov/。 CERT ——美國國家應急回應中心 →https://www.us-cert.gov/ OSVDB ——開源漏洞庫 →http://osvdb.org Bugtraq ——賽門鐵克 →ht ......

    uj5u.com 2020-09-10 03:52:15 more
  • 京準講述NTP時鐘服務器應用及原理

    京準講述NTP時鐘服務器應用及原理京準講述NTP時鐘服務器應用及原理 安徽京準電子科技官微——ahjzsz 北斗授時原理 授時是指接識訓通過某種方式獲得本地時間與北斗標準時間的鐘差,然后調整本地時鐘使時差控制在一定的精度范圍內。 衛星導航系統通常由三部分組成:導航授時衛星、地面檢測校正維護系統和用戶 ......

    uj5u.com 2020-09-10 03:52:25 more
  • 利用北斗衛星系統設計NTP網路時間服務器

    利用北斗衛星系統設計NTP網路時間服務器 利用北斗衛星系統設計NTP網路時間服務器 安徽京準電子科技官微——ahjzsz 概述 NTP網路時間服務器是一款支持NTP和SNTP網路時間同步協議,高精度、大容量、高品質的高科技時鐘產品。 NTP網路時間服務器設備采用冗余架構設計,高精度時鐘直接來源于北斗 ......

    uj5u.com 2020-09-10 03:52:35 more
  • 詳細解讀電力系統各種對時方式

    詳細解讀電力系統各種對時方式 詳細解讀電力系統各種對時方式 安徽京準電子科技官微——ahjzsz,更多資料請添加VX 衛星同步時鐘是我京準公司開發研制的應用衛星授時時技術的標準時間顯示和發送的裝置,該裝置以M國全球定位系統(GLOBAL POSITIONING SYSTEM,縮寫為GPS)或者我國北 ......

    uj5u.com 2020-09-10 03:52:45 more
  • 如何保證外包團隊接入企業內網安全

    不管企業規模的大小,只要企業想省錢,那么企業的某些服務就一定會采用外包的形式,然而看似美好又經濟的策略,其實也有不好的一面。下面我通過安全的角度來聊聊使用外包團的安全隱患問題。 先看看什么服務會使用外包的,最常見的就是話務/客服這種需要大量重復性、無技術性的服務,或者是一些銷售外包、特殊的職能外包等 ......

    uj5u.com 2020-09-10 03:52:57 more
  • PHP漏洞之【整型數字型SQL注入】

    0x01 什么是SQL注入 SQL是一種注入攻擊,通過前端帶入后端資料庫進行惡意的SQL陳述句查詢。 0x02 SQL整型注入原理 SQL注入一般發生在動態網站URL地址里,當然也會發生在其它地發,如登錄框等等也會存在注入,只要是和資料庫打交道的地方都有可能存在。 如這里http://192.168. ......

    uj5u.com 2020-09-10 03:55:40 more
  • [GXYCTF2019]禁止套娃

    git泄露獲取原始碼 使用GET傳參,引數為exp 經過三層過濾執行 第一層過濾偽協議,第二層過濾帶引數的函式,第三層過濾一些函式 preg_replace('/[a-z,_]+\((?R)?\)/', NULL, $_GET['exp'] (?R)參考當前正則運算式,相當于匹配函式里的引數 因此傳遞 ......

    uj5u.com 2020-09-10 03:56:07 more
  • 等保2.0實施流程

    流程 結論 ......

    uj5u.com 2020-09-10 03:56:16 more
最新发布
  • 使用Django Rest framework搭建Blog

    在前面的Blog例子中我們使用的是GraphQL, 雖然GraphQL的使用處于上升趨勢,但是Rest API還是使用的更廣泛一些. 所以還是決定回到傳統的rest api framework上來, Django rest framework的官網上給了一個很好用的QuickStart, 我參考Qu ......

    uj5u.com 2023-04-20 08:17:54 more
  • 記錄-new Date() 我忍你很久了!

    這里給大家分享我在網上總結出來的一些知識,希望對大家有所幫助 大家平時在開發的時候有沒被new Date()折磨過?就是它的諸多怪異的設定讓你每每用的時候,都可能不小心踩坑。造成程式意外出錯,卻一下子找不到問題出處,那叫一個煩透了…… 下面,我就列舉它的“四宗罪”及應用思考 可惡的四宗罪 1. Sa ......

    uj5u.com 2023-04-20 08:17:47 more
  • 使用Vue.js實作文字跑馬燈效果

    實作文字跑馬燈效果,首先用到 substring()截取 和 setInterval計時器 clearInterval()清除計時器 效果如下: 實作代碼如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta ......

    uj5u.com 2023-04-20 08:12:31 more
  • JavaScript 運算子

    JavaScript 運算子/運算子 在 JavaScript 中,有一些運算子可以使代碼更簡潔、易讀和高效。以下是一些常見的運算子: 1、可選鏈運算子(optional chaining operator) ?.是可選鏈運算子(optional chaining operator)。?. 可選鏈操 ......

    uj5u.com 2023-04-20 08:02:25 more
  • CSS—相對單位rem

    一、概述 rem是一個相對長度單位,它的單位長度取決于根標簽html的字體尺寸。rem即root em的意思,中文翻譯為根em。瀏覽器的文本尺寸一般默認為16px,即默認情況下: 1rem = 16px rem布局原理:根據CSS媒體查詢功能,更改根標簽的字體尺寸,實作rem單位隨螢屏尺寸的變化,如 ......

    uj5u.com 2023-04-20 08:02:21 more
  • 我的第一個NPM包:panghu-planebattle-esm(胖虎飛機大戰)使用說明

    好家伙,我的包終于開發完啦 歡迎使用胖虎的飛機大戰包!! 為你的主頁添加色彩 這是一個有趣的網頁小游戲包,使用canvas和js開發 使用ES6模塊化開發 效果圖如下: (覺得圖片太sb的可以自己改) 代碼已開源!! Git: https://gitee.com/tang-and-han-dynas ......

    uj5u.com 2023-04-20 08:01:50 more
  • 如何在 vue3 中使用 jsx/tsx?

    我們都知道,通常情況下我們使用 vue 大多都是用的 SFC(Signle File Component)單檔案組件模式,即一個組件就是一個檔案,但其實 Vue 也是支持使用 JSX 來撰寫組件的。這里不討論 SFC 和 JSX 的好壞,這個仁者見仁智者見智。本篇文章旨在帶領大家快速了解和使用 Vu ......

    uj5u.com 2023-04-20 08:01:37 more
  • 【Vue2.x原始碼系列06】計算屬性computed原理

    本章目標:計算屬性是如何實作的?計算屬性快取原理以及洋蔥模型的應用?在初始化Vue實體時,我們會給每個計算屬性都創建一個對應watcher,我們稱之為計算屬性watcher ......

    uj5u.com 2023-04-20 08:01:31 more
  • http1.1與http2.0

    一、http是什么 通俗來講,http就是計算機通過網路進行通信的規則,是一個基于請求與回應,無狀態的,應用層協議。常用于TCP/IP協議傳輸資料。目前任何終端之間任何一種通信方式都必須按Http協議進行,否則無法連接。tcp(三次握手,四次揮手)。 請求與回應:客戶端請求、服務端回應資料。 無狀態 ......

    uj5u.com 2023-04-20 08:01:10 more
  • http1.1與http2.0

    一、http是什么 通俗來講,http就是計算機通過網路進行通信的規則,是一個基于請求與回應,無狀態的,應用層協議。常用于TCP/IP協議傳輸資料。目前任何終端之間任何一種通信方式都必須按Http協議進行,否則無法連接。tcp(三次握手,四次揮手)。 請求與回應:客戶端請求、服務端回應資料。 無狀態 ......

    uj5u.com 2023-04-20 08:00:32 more