为ggplot2中的两个变量合并图例中的颜色和形状

时间:2018-10-21 14:30:48

标签: r ggplot2

这是我的数据:

delta <- structure(list(rcp = c("rcp45", "rcp45", "rcp45", "rcp45", "rcp45", 
"rcp45", "rcp45", "rcp45", "rcp45", "rcp45", "rcp45", "rcp45", 
"rcp45", "rcp45", "rcp45", "rcp45", "rcp45", "rcp45", "rcp45", 
"rcp45", "rcp45", "rcp45", "rcp45", "rcp45", "rcp45", "rcp45", 
"rcp45", "rcp45", "rcp45", "rcp45", "rcp45", "rcp45", "rcp45", 
"rcp45", "rcp45", "rcp45", "rcp45", "rcp45", "rcp45", "rcp45", 
"rcp45", "rcp45", "rcp45", "rcp45", "rcp45", "rcp45", "rcp45", 
"rcp45", "rcp45", "rcp45", "rcp45", "rcp45", "rcp45", "rcp45", 
"rcp45", "rcp45", "rcp45", "rcp45", "rcp45", "rcp45", "rcp45", 
"rcp45", "rcp45", "rcp45", "rcp45", "rcp45", "rcp45", "rcp45", 
"rcp85", "rcp85", "rcp85", "rcp85", "rcp85", "rcp85", "rcp85", 
"rcp85", "rcp85", "rcp85", "rcp85", "rcp85", "rcp85", "rcp85", 
"rcp85", "rcp85", "rcp85", "rcp85", "rcp85", "rcp85", "rcp85", 
"rcp85", "rcp85", "rcp85", "rcp85", "rcp85", "rcp85", "rcp85", 
"rcp85", "rcp85", "rcp85", "rcp85", "rcp85", "rcp85", "rcp85", 
"rcp85", "rcp85", "rcp85", "rcp85", "rcp85", "rcp85", "rcp85", 
"rcp85", "rcp85", "rcp85", "rcp85", "rcp85", "rcp85", "rcp85", 
"rcp85", "rcp85", "rcp85", "rcp85", "rcp85", "rcp85", "rcp85", 
"rcp85", "rcp85", "rcp85", "rcp85", "rcp85", "rcp85", "rcp85", 
"rcp85", "rcp85", "rcp85", "rcp85", "rcp85"), gcm = c("ACCESS1-0", 
"ACCESS1-3", "bcc-csm1-1", "bcc-csm1-1-m", "CanESM2", "CCSM4", 
"CESM1-BGC", "CESM1-CAM5", "CMCC-CM", "CMCC-CMS", "CNRM-CM5", 
"CSIRO-Mk3-6-0", "FGOALS-g2", "FIO-ESM", "GFDL-CM3", "GFDL-ESM2G", 
"GFDL-ESM2M", "GISS-E2-H", "GISS-E2-R", "HadGEM2-AO", "HadGEM2-CC", 
"HadGEM2-ES", "inmcm4", "IPSL-CM5A-LR", "IPSL-CM5A-MR", "IPSL-CM5B-LR", 
"MIROC-ESM", "MIROC-ESM-CHEM", "MIROC5", "MPI-ESM-LR", "MPI-ESM-MR", 
"MRI-CGCM3", "NorESM1-M", "NorESM1-ME", "ACCESS1-0", "ACCESS1-3", 
"bcc-csm1-1", "bcc-csm1-1-m", "CanESM2", "CCSM4", "CESM1-BGC", 
"CESM1-CAM5", "CMCC-CM", "CMCC-CMS", "CNRM-CM5", "CSIRO-Mk3-6-0", 
"FGOALS-g2", "FIO-ESM", "GFDL-CM3", "GFDL-ESM2G", "GFDL-ESM2M", 
"GISS-E2-H", "GISS-E2-R", "HadGEM2-AO", "HadGEM2-CC", "HadGEM2-ES", 
"inmcm4", "IPSL-CM5A-LR", "IPSL-CM5A-MR", "IPSL-CM5B-LR", "MIROC-ESM", 
"MIROC-ESM-CHEM", "MIROC5", "MPI-ESM-LR", "MPI-ESM-MR", "MRI-CGCM3", 
"NorESM1-M", "NorESM1-ME", "ACCESS1-0", "ACCESS1-3", "bcc-csm1-1", 
"bcc-csm1-1-m", "CanESM2", "CCSM4", "CESM1-BGC", "CESM1-CAM5", 
"CMCC-CM", "CMCC-CMS", "CNRM-CM5", "CSIRO-Mk3-6-0", "FGOALS-g2", 
"FIO-ESM", "GFDL-CM3", "GFDL-ESM2G", "GFDL-ESM2M", "GISS-E2-H", 
"GISS-E2-R", "HadGEM2-AO", "HadGEM2-CC", "HadGEM2-ES", "inmcm4", 
"IPSL-CM5A-LR", "IPSL-CM5A-MR", "IPSL-CM5B-LR", "MIROC-ESM", 
"MIROC-ESM-CHEM", "MIROC5", "MPI-ESM-LR", "MPI-ESM-MR", "MRI-CGCM3", 
"NorESM1-M", "NorESM1-ME", "ACCESS1-0", "ACCESS1-3", "bcc-csm1-1", 
"bcc-csm1-1-m", "CanESM2", "CCSM4", "CESM1-BGC", "CESM1-CAM5", 
"CMCC-CM", "CMCC-CMS", "CNRM-CM5", "CSIRO-Mk3-6-0", "FGOALS-g2", 
"FIO-ESM", "GFDL-CM3", "GFDL-ESM2G", "GFDL-ESM2M", "GISS-E2-H", 
"GISS-E2-R", "HadGEM2-AO", "HadGEM2-CC", "HadGEM2-ES", "inmcm4", 
"IPSL-CM5A-LR", "IPSL-CM5A-MR", "IPSL-CM5B-LR", "MIROC-ESM", 
"MIROC-ESM-CHEM", "MIROC5", "MPI-ESM-LR", "MPI-ESM-MR", "MRI-CGCM3", 
"NorESM1-M", "NorESM1-ME"), future = c("Near", "Near", "Near", 
"Near", "Near", "Near", "Near", "Near", "Near", "Near", "Near", 
"Near", "Near", "Near", "Near", "Near", "Near", "Near", "Near", 
"Near", "Near", "Near", "Near", "Near", "Near", "Near", "Near", 
"Near", "Near", "Near", "Near", "Near", "Near", "Near", "Far", 
"Far", "Far", "Far", "Far", "Far", "Far", "Far", "Far", "Far", 
"Far", "Far", "Far", "Far", "Far", "Far", "Far", "Far", "Far", 
"Far", "Far", "Far", "Far", "Far", "Far", "Far", "Far", "Far", 
"Far", "Far", "Far", "Far", "Far", "Far", "Near", "Near", "Near", 
"Near", "Near", "Near", "Near", "Near", "Near", "Near", "Near", 
"Near", "Near", "Near", "Near", "Near", "Near", "Near", "Near", 
"Near", "Near", "Near", "Near", "Near", "Near", "Near", "Near", 
"Near", "Near", "Near", "Near", "Near", "Near", "Near", "Far", 
"Far", "Far", "Far", "Far", "Far", "Far", "Far", "Far", "Far", 
"Far", "Far", "Far", "Far", "Far", "Far", "Far", "Far", "Far", 
"Far", "Far", "Far", "Far", "Far", "Far", "Far", "Far", "Far", 
"Far", "Far", "Far", "Far", "Far", "Far"), dP = c(-3.25601208225227, 
4.25422575313546, 3.47856932609794, 6.07325308463769, 6.13007803837502, 
3.7621513993393, 13.713339640492, 10.2070499979329, 0.577718791474743, 
-1.09176058823822, 8.96908501620543, 7.59563902682608, -1.09812004690998, 
-3.71905411491341, 5.24520341268675, 9.16074352957066, 6.15524950613622, 
0.807207207207211, 1.34566862910008, -4.51506437950621, -4.08101204233061, 
-5.39728674911793, -2.69215440031884, 1.15328520828994, 8.40475894717955, 
-4.07115502025864, 6.08013359849063, 7.057408574626, 2.01259854997822, 
-1.90455419455918, -0.902599845908517, 8.59364585647634, -0.164483012875046, 
2.82756313256128, 0.271076172404446, 9.65568966235642, 15.8954542366997, 
3.56600434296903, 20.642681394342, 9.85687780515799, 14.330867807689, 
23.8083068053264, -6.07905899497749, -4.29246854228087, 21.4528795811518, 
12.3292738184407, 5.01202364452658, -9.56785087232639, 38.1477228727452, 
12.0792602972542, 12.590308517961, 14.0396396396396, 12.6072329688814, 
-0.455401787611376, -1.24072497263106, -2.20760639134636, 2.4530188977207, 
3.8655276800706, 18.7349040067354, 8.45867791530222, 12.7983019733823, 
14.9721511171711, 12.5, -3.69382922660589, -0.862733152962687, 
4.44493816188995, 7.86531299037679, 8.55302678904106, -6.42992680943345, 
3.39032486562313, 8.2779232737345, -1.03490692741217, 9.54729294486627, 
4.00816195557822, 9.2332501935151, 11.8442069973804, 2.4744506280926, 
-2.61853507206316, 12.8153826975816, 14.145421255583, 3.61420093109208, 
-6.14250934985085, 5.13571972175119, 4.99119267500688, 9.6092180443554, 
-3.35135135135136, 5.4247266610597, 7.4904032749267, -5.6934138858479, 
-8.46111781142083, -1.11671767722722, 4.7687914404422, 6.38413868300829, 
-2.77330669182809, 8.93503645913007, 4.87194659188845, 2.79604611544708, 
-1.08736291126619, -2.8690580709896, 7.28578834333112, 2.46270773070161, 
1.86983400375641, -6.96898114084344, 19.4502701426409, 24.4669690634505, 
7.83554456564033, 28.658774829192, 8.87077863575217, 17.918637653737, 
23.3422657861999, -4.51877214309878, -7.51356000457387, 31.4454001495886, 
22.5381677624583, 14.9164267860739, -14.6229709355072, 45.9891584442634, 
32.6658900463158, 25.2656791256512, 6.81801801801802, 14.6173254835997, 
7.30838420896072, -6.61179364500128, -9.24692680515084, 6.47169853886704, 
-1.97933646268675, 26.8794942928519, 6.50959279199276, 18.1148335118046, 
20.2061975019616, 19.3187670852977, -3.67290984190286, -4.62677608356775, 
13.2044730800563, 11.812905299378, 10.2593738831563), dT = c(1.74, 
1.116, 1.286, 1.314, 1.952, 1.418, 1.376, 1.826, 1.657, 1.99, 
1.769, 1.703, 1.547, 1.183, 3.343, 1.46, 1.105, 1.274, 1.295, 
1.797, 1.149, 1.427, 0.785, 1.956, 2.267, 1.011, 1.972, 1.808, 
1.713, 1.769, 1.864, 1.202, 1.486, 1.273, 3.366, 2.504, 2.273, 
2.284, 3.671, 2.461, 2.344, 3.256, 3.594, 4.136, 3.901, 3.174, 
2.833, 1.701, 5.998, 2.184, 2.082, 2.407, 2.315, 3.354, 2.656, 
3.135, 1.881, 3.931, 4.02, 2.117, 4.002, 4.151, 3.641, 3.283, 
3.018, 2.318, 2.447, 2.742, 1.713, 1.268, 1.574, 1.499, 1.879, 
1.66, 1.658, 1.928, 1.781, 2.569, 2.036, 1.49, 1.75, 1.272, 3.008, 
1.635, 1.481, 1.511, 1.447, 1.143, 1.343, 1.427, 1.069, 2.352, 
2.337, 1.366, 2.156, 2.417, 1.759, 2.158, 2.205, 1.319, 1.446, 
1.522, 4.822, 3.837, 3.997, 3.815, 5.742, 4.306, 4.197, 5.109, 
6.036, 6.457, 5.376, 4.614, 4.244, 3.638, 8.075, 3.981, 3.69, 
4.115, 3.78, 4.29, 4.737, 4.604, 3.605, 6.276, 6.486, 3.897, 
6.876, 7.334, 4.911, 5.451, 5.488, 4.125, 4.141, 4.202)), row.names = c(NA, 
-136L), class = "data.frame")

这是我要绘制的代码:

ggplot()+geom_point(data = delta, aes(x = dP/100, y = dT, shape = interaction(rcp, future),
                                      color = interaction(rcp, future)  )) +
  xlab("Percentage change in precipitation")+
  ylab("Change in temperature in degree Celsius")+
  scale_x_continuous(breaks = seq(-0.3,0.5, 0.1), label = scales::percent, limits = c(-0.3,0.5))+
  scale_y_continuous(breaks = seq(0, 9, 1), limits = c(0,9)) 

它给出以下结果。

enter image description here

有4种颜色和4种形状。我希望仅以这种方式用2种颜色和2种形状来表示它:

1. red circle   : rcp45.Far
2. red triangle : rcp45.Near
3. blue circle  : rcp85.Far
4. blue triangle: rcp85.Near

为了解决我的问题,我在这里提到了很多问题,但是这里提出的问题在颜色和形状上都有相同的变量。我想为不同的变量设置不同的形状和颜色。

1 个答案:

答案 0 :(得分:3)

只需手动设置它们:

scale_color_manual(values = c("rcp45.Far" = "red", "rcp85.Far" = "cyan",
                              "rcp45.Near" = "red", "rcp85.Near" = "cyan")) + 
scale_shape_manual(values = c("rcp45.Far" = 1, "rcp85.Far" = 1, 
                              "rcp45.Near" = 2, "rcp85.Near" = 2))

您可以通过更改值来选择自己的颜色和形状,希望对您有所帮助