ggplot图例的形状和填充不正确

时间:2017-04-06 22:17:09

标签: r ggplot2 legend

我试图制作一个图表,显示不同喂养水平和温度下的累积死亡率。我设法使图表看起来正确,但图例不匹配。我确信我的代码过于复杂,但这是我找到的唯一方法来实现正确的视觉效果。我希望不同的温度可以用不同的形状来表示,我希望不同的喂料水平用实心或空心填料表示(但形状与相应的温度相同)。喂食水平仅适用于2度和5度。

如下图所示,图表上的进料水平确实显示实心和空心点,但在图例中它没有。我还想了下面所示的所有要点'温度'在传说中要坚实。

这是我的代码:

ggplot(ac_tank_cumulative_mort_summary, aes(Day, mean, shape = factor(Target_Temp),fill=factor(Feeding))) +
    geom_point(stat = "identity",size=3.5,color="black") +
    geom_line() +
    scale_y_continuous(limits = c(0,100)) +
    scale_shape_manual(name="Temperature (ºC)",labels=c("0 ºC","2 ºC","5 ºC","7 ºC","9 ºC"),values=c(21,22,23,24,25)) +
    scale_fill_manual(name="Feeding",labels=c("High Food","Low Food"),values=c("black","white")) +
    xlab("Day of Experiment") +
    ylab("Cumulative Mortality (%)") +
    ggtitle("Cumulative Percent Mortality \n of Later Stage Arctic Cod") +
    theme_bw() +
    theme(axis.text = element_text(size = 16, color='black'), axis.title = element_text(size = 16, face = "bold"), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), legend.text = element_text(size = 16), legend.title = element_text(size = 16, face = "bold"), plot.title = element_text(size = 18, face = "bold",hjust=0.5))

它生成一个如下所示的图形:

click here for graph

可重现的例子:

structure(list(Target_Temp = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 5L,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 
31.02208526, 37.00051361, 39.6671802766667, 43.1015237133333, 
46.0402328333333, 49.4934086633333, 52.9560006833333, 54.0859441866667, 
55.7620270533333, 57.4569423066667, 57.4569423066667, 58.6369757233333, 
60.40180446, 60.9865997833333, 60.9865997833333, 61.58183788, 
62.77231407, 0, 5.483691307, 9.37154714766667, 18.2054598866667, 
19.8012953533333, 23.7363121066667, 26.4029787733333, 31.31040864, 
34.08436863, 36.8583286166667, 38.5098588766667, 39.0474932833333, 
40.1227621, 40.66039651, 42.91086732, 43.52815127, 43.52815127, 
43.52815127, 43.52815127, 44.1454352233333, 44.1454352233333, 
0, 5.32153032133333, 12.76963777, 24.6092796066667, 32.63939764, 
41.1558811566667, 43.8225478233333, 47.6157916166667, 49.2030932033333, 
52.46723647, 53.0227920266667, 53.57834758, 53.57834758, 54.09116809, 
54.6759634133333, 58.1083346633333, 58.6931299833333, 59.2779253066667, 
60.90874968, 61.4643052366667, 62.0019396466667, 0, 5.84092792033333, 
18.993371995, 28.6523059933333, 32.47031207, 32.9397956366667, 
34.27312897, 37.6423639866667, 39.56172328, 43.4211543733333, 
44.4015465333333, 46.8111019033333, 49.2206572766667, 49.6901408466667, 
50.1803369233333, 53.25726, 56.33418308, 58.2949673933333, 61.3040171633333, 
64.8485118866667, 0, 13.1614526916667, 22.6657863833333, 31.59793698, 
38.60881636, 41.6744537733333, 42.0077871066667, 42.0077871066667, 
45.0654117, 47.1327193933333, 47.6455399066667, 48.6711809333333, 
49.6534850133333, 54.2688696266667, 55.3529521233333, 57.4086130966667, 
59.4730877466667, 60.9999279933333, 61.54637608, 61.54637608, 
0, 11.9041826816667, 20.52782238, 29.3914919133333, 31.8773415066667, 
32.9526103233333, 34.2859436566667, 34.9395384266667, 34.9395384266667, 
35.5931332, 36.8751844833333, 40.96136817, 43.3312574, 45.71371576, 
51.05476461, 65.1266928133333, 73.21981707, 77.7511211166667, 
79.56133673, 80.8065805933333, 0, 40, 87.5, 90, 90, 92.5, 92.5, 
92.5, 92.5, 92.5, 92.5, 92.5, 92.5, 92.5, 92.5, 92.5), sd = c(0, 
1.97372461784689, 5.80473942192512, 12.9273738611295, 18.7980078654077, 
26.2827168030405, 24.7758104083834, 25.1241212927305, 27.2873150523553, 
27.4420059335036, 27.4630003540068, 26.461746133237, 25.0082449931503, 
23.6407105265119, 23.6407105265119, 22.6249375824511, 21.0314195173936, 
20.4868619144685, 20.4868619144685, 20.0127776006859, 19.1960498751, 
0, 0.77902264946986, 1.23794275560703, 3.34864008798032, 3.2335928525848, 
5.36060756499091, 5.77844904674583, 4.44080298306709, 4.09533840068484, 
3.9968905491584, 4.06201390276555, 3.24181683679238, 1.93161303094676, 
1.71565205207569, 2.64834297675624, 2.66829911911964, 2.66829911911964, 
2.66829911911964, 2.66829911911964, 3.08417845208611, 3.08417845208611, 
0, 0.910533229473384, 2.72480540999204, 6.65751125731073, 8.33148895279841, 
9.0007033472894, 9.57771603598199, 11.7084213217991, 12.9037012862438, 
14.5648056008555, 15.1859834728865, 15.8412902763179, 15.8412902763179, 
14.9818268027929, 15.7149156535738, 18.1464512942252, 18.9773239417533, 
19.8251322063165, 18.2151433915608, 18.2852656545651, 17.440878798362, 
0, 3.00352826170525, 9.15977839384524, 9.07674417328714, 7.17760460594045, 
6.49258187655286, 6.47302852615445, 5.18715642772437, 4.46042901997611, 
3.92629364971166, 4.75362293709948, 4.62667657495029, 5.55770752177903, 
5.01285084668542, 4.53061089193868, 7.36285237297975, 9.36455421131727, 
6.89234052824489, 6.90699192099218, 6.4916961180775, 0, 4.40845912544074, 
6.13942103207389, 6.68730640525036, 9.00051763429896, 11.6269296244466, 
11.4835770151044, 11.4835770151044, 12.6472892332494, 13.3450448267859, 
12.8661920432309, 12.0486504655982, 11.2113054845842, 10.9471632374873, 
11.8734160758113, 10.9393380542906, 10.181245628949, 9.03442659821049, 
9.64562925636332, 9.64562925636332, 0, 8.39261284702131, 9.64654877195206, 
12.3498744833651, 13.5156278842202, 13.1057317249229, 13.3307327422633, 
14.420694327421, 14.420694327421, 15.5166855751566, 14.066097040009, 
12.5037531860345, 12.3444832323343, 14.1519399447546, 10.2648576647302, 
5.60999878946208, 2.3909883546916, 5.27289282704079, 5.52418422785351, 
6.39683309409102, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA), se = c(0, 1.13953043942009, 3.3513678678241, 
7.46362277863804, 10.8530349013219, 15.1743336212701, 14.3043208086713, 
14.5054181915108, 15.7543386909395, 15.8436495128116, 15.8557706471406, 
15.2776962532519, 14.4385169787554, 13.6489705863157, 13.6489705863157, 
13.0625138036266, 12.1424957198072, 11.8280952411691, 11.8280952411691, 
11.5543825349881, 11.0828445627664, 0, 0.449768936376239, 0.714726583191068, 
1.93333825621461, 1.86691570388948, 3.09494822066744, 3.33618911263724, 
2.56389879769188, 2.36444472805801, 2.30760583447808, 2.34520482021369, 
1.87166382338554, 1.1152173033873, 0.990532174101631, 1.52902153053667, 
1.54054321470217, 1.54054321470217, 1.54054321470217, 1.54054321470217, 
1.78065125954076, 1.78065125954076, 0, 0.525696605142557, 1.57316713694826, 
3.84371591654131, 4.81018738964856, 5.1965585004535, 5.529696931596, 
6.75986020192626, 7.4499554111554, 8.40899443434848, 8.76763164598026, 
9.14597320534316, 9.14597320534316, 8.64976173754481, 9.07301078288308, 
10.4768585395573, 10.95656308627, 11.4460454160367, 10.5165179404452, 
10.557003047867, 10.0694960691379, 0, 1.73408785041418, 5.28840052140387, 
5.2404606918127, 4.14399195137642, 3.74849389416348, 3.7372047620474, 
2.99480615987537, 2.57522989538443, 2.26684669557854, 2.74450548236037, 
2.67121296600089, 3.20874393377633, 2.89417078574127, 2.61574941805425, 
4.25094479954333, 5.40662789474487, 3.97929465932875, 3.98775364487541, 
3.74798250126929, 0, 2.54522506278467, 3.54459638553631, 3.8609181532248, 
5.19645127900848, 6.71281094852307, 6.63004628093031, 6.63004628093031, 
7.30191584333562, 7.70476522309248, 7.42829943960477, 6.95629158968482, 
6.47285023949184, 6.32034764202606, 6.85511996757007, 6.31582977040099, 
5.87814490455939, 5.21602862845074, 5.56890664766469, 5.56890664766469, 
0, 4.84547728643206, 5.56943753023738, 7.13020335742892, 7.80325139722136, 
7.56659773931127, 7.69650213724068, 8.32579175183782, 8.32579175183782, 
8.95856259374746, 8.12106491249661, 7.21904526783764, 7.12709071719501, 
8.17062633665946, 5.92641833592515, 3.23893431124941, 1.38043777021046, 
3.04430609310005, 3.18938925100431, 3.69321330883456, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)), .Names = c("Target_Temp", 
"Feeding", "Day", "N", "mean", "sd", "se"), row.names = c(NA, 
-139L), class = "data.frame")

1 个答案:

答案 0 :(得分:2)

正如aosmith在评论中指出的那样,你可以添加这一行,以确保在图例中使用带填充的形状:

> library(readr) 
> parse_date("31 DICIEMBRE 2011","%d %B %Y",locale=locale("es"))
[1] "2011-12-31"

enter image description here