我想使用 R 中的 ggmap 删除情节中的美学图例
I want to remove the aesthetics legend in my plot using ggmap in R
我对 ggplot2 和 ggmap 比较陌生。我在处理重复的图例时遇到了麻烦。我想删除的图例是在我相信的代码的 'Fill' 部分创建的。
## Kootenai map
Koot_base = get_map(location = c(-116,49), zoom = 7,
maptype = 'roadmap')
KootBase = ggmap(Koot_base)
## Coloring map for data
Fill = stat_bin2d(aes(x = YearPercDat[,2], y = YearPercDat[,1],
colour = YearPercInd, fill = YearPercInd), size=.9,
bins = 100, alpha = .5, data = YearPercFort)
## Outlining subbasin
Outline = geom_polygon(aes(x = kootenai.coord[,1], y = kootenai.coord[,2]),
data = kootenai.coord, colour = 'black', fill = NA, alpha = 0.4,
size = 0.75)
## Titles
Axes = labs(x = "Longitude", y = "Latitude")
Main = ggtitle(paste("Kootenai Subbasin ",year,sep=''))
## Scale
Scale = scale_fill_discrete(name = "Percent of Year Snow Covered",
labels = c("Greater than 0","0"))
ggmap(Koot_base) + Fill + Outline + Axes + Main + Scale
下面是我的数据集的前 100 行。
> dput(Dat2)
structure(list(V1 = c(50.078331, 50.140011, 50.051201, 50.071758,
50.0923, 50.11282, 50.133331, 50.15382, 50.17429, 50.003502,
50.024059, 50.044601, 50.065121, 50.085629, 50.106121, 50.126591,
50.147041, 49.976372, 49.99691, 50.017429, 50.037941, 50.05843,
50.078899, 50.09935, 50.119781, 50.140202, 50.160599, 50.201351,
49.94923, 49.969761, 49.990261, 50.01075, 50.031219, 50.05167,
50.072102, 50.092522, 50.112919, 50.133301, 50.15366, 50.174011,
50.19434, 50.214649, 49.881031, 49.901569, 49.922089, 49.9426,
49.963081, 49.983551, 50.004002, 50.024429, 50.044849, 50.06525,
50.085629, 50.105991, 50.126339, 50.14666, 50.166969, 50.187271,
49.812832, 49.833389, 49.85392, 49.874439, 49.894939, 49.915428,
49.935902, 49.956348, 49.97678, 49.997189, 50.01759, 50.037971,
50.058331, 50.078678, 50.098999, 50.119308, 50.139599, 50.159882,
49.785751, 49.80629, 49.826801, 49.847301, 49.86779, 49.888248,
49.908699, 49.929131, 49.949551, 49.96994, 49.990318, 50.010681,
50.031029, 50.05135, 50.071659, 50.091949, 50.112228, 50.132481,
49.676418, 49.738129, 49.758659, 49.779179, 49.799679, 49.82016
), V2 = c(-117.192703, -117.066025, -117.160736, -117.118576,
-117.076355, -117.034103, -116.991791, -116.949432, -116.907043,
-117.170914, -117.128815, -117.08667, -117.044464, -117.002213,
-116.95993, -116.917587, -116.875198, -117.139038, -117.096954,
-117.05481, -117.012619, -116.970383, -116.928116, -116.885773,
-116.843414, -116.800995, -116.758514, -116.673447, -117.107193,
-117.065125, -117.022995, -116.98082, -116.938599, -116.896347,
-116.854019, -116.811661, -116.769257, -116.726807, -116.684296,
-116.641754, -116.599167, -116.556519, -117.159393, -117.117432,
-117.075409, -117.03334, -116.991226, -116.949066, -116.906876,
-116.864609, -116.822311, -116.779968, -116.737579, -116.695145,
-116.652649, -116.610107, -116.567535, -116.524918, -117.211411,
-117.16954, -117.127625, -117.085663, -117.043655, -117.001617,
-116.959503, -116.917374, -116.875168, -116.832947, -116.790649,
-116.748322, -116.705933, -116.663513, -116.621033, -116.578522,
-116.53595, -116.493347, -117.179657, -117.137802, -117.095901,
-117.053955, -117.011963, -116.969925, -116.927841, -116.885712,
-116.843536, -116.801315, -116.759033, -116.716705, -116.674347,
-116.631943, -116.589478, -116.546967, -116.504425, -116.461823,
-117.31485, -117.189743, -117.147949, -117.10611, -117.064224,
-117.022293), V3 = c(48.3425414364641, 48.3425414364641, 48.3425414364641,
70.1657458563536, 72.6519337016575, 70.1657458563536, 70.1657458563536,
0, 24.8618784530387, 70.1657458563536, 46.4088397790055, 37.8453038674033,
70.1657458563536, 68.232044198895, 58.0110497237569, 0, 24.8618784530387,
70.1657458563536, 69.6132596685083, 26.2430939226519, 70.1657458563536,
67.6795580110497, 0, 24.8618784530387, 47.5138121546961, 79.8342541436464,
79.8342541436464, 75.1381215469613, 61.878453038674, 59.3922651933702,
32.3204419889503, 59.3922651933702, 59.3922651933702, 0, 24.8618784530387,
50.5524861878453, 56.9060773480663, 51.9337016574586, 75.1381215469613,
77.9005524861878, 54.9723756906077, 98.3425414364641, 61.878453038674,
61.6022099447514, 80.3867403314917, 71.5469613259668, 61.0497237569061,
48.3425414364641, 0, 0, 25.414364640884, 25.414364640884, 25.414364640884,
57.182320441989, 45.8563535911602, 46.6850828729282, 46.6850828729282,
79.8342541436464, 61.6022099447514, 70.1657458563536, 61.6022099447514,
46.4088397790055, 37.8453038674033, 32.3204419889503, 32.3204419889503,
58.0110497237569, 0, 24.8618784530387, 58.0110497237569, 56.9060773480663,
56.9060773480663, 37.292817679558, 79.8342541436464, 58.5635359116022,
72.9281767955801, 43.9226519337017, 59.9447513812155, 39.5027624309392,
61.878453038674, 70.1657458563536, 70.1657458563536, 71.5469613259668,
26.2430939226519, 0, 24.8618784530387, 26.5193370165746, 70.1657458563536,
70.1657458563536, 43.3701657458564, 57.182320441989, 51.9337016574586,
72.0994475138122, 53.8674033149171, 72.9281767955801, 27.3480662983425,
51.1049723756906, 61.878453038674, 61.6022099447514, 72.9281767955801,
72.3756906077348), V4 = c(0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0)), .Names = c("V1", "V2", "V3", "V4"), row.names = c(NA,
100L), class = "data.frame")
我得到的输出
可能最简单的方法是在调用中省略 colour
参数。对于 bin2d,颜色就是轮廓 - 当您已经使用填充时不需要轮廓:
library(ggmaps)
ggmap(Koot_base) +
stat_bin2d(data = YearPercDat, aes(x = V2, y = V1, fill = as.factor(V4))) +
scale_fill_discrete(name = "Percent of Year Snow Covered", labels = c("Greater than 0","0"))
如果出于某种原因想要保留颜色,可以将 colour
指南设置为 false:
+ guides(colour = FALSE)
注意,您通过按列子集调用 aes
的方式并不是最好的。最好使用名称 - 即 V4
V2
等,以便正确执行绘图功能执行的任何子集等。
我对 ggplot2 和 ggmap 比较陌生。我在处理重复的图例时遇到了麻烦。我想删除的图例是在我相信的代码的 'Fill' 部分创建的。
## Kootenai map
Koot_base = get_map(location = c(-116,49), zoom = 7,
maptype = 'roadmap')
KootBase = ggmap(Koot_base)
## Coloring map for data
Fill = stat_bin2d(aes(x = YearPercDat[,2], y = YearPercDat[,1],
colour = YearPercInd, fill = YearPercInd), size=.9,
bins = 100, alpha = .5, data = YearPercFort)
## Outlining subbasin
Outline = geom_polygon(aes(x = kootenai.coord[,1], y = kootenai.coord[,2]),
data = kootenai.coord, colour = 'black', fill = NA, alpha = 0.4,
size = 0.75)
## Titles
Axes = labs(x = "Longitude", y = "Latitude")
Main = ggtitle(paste("Kootenai Subbasin ",year,sep=''))
## Scale
Scale = scale_fill_discrete(name = "Percent of Year Snow Covered",
labels = c("Greater than 0","0"))
ggmap(Koot_base) + Fill + Outline + Axes + Main + Scale
下面是我的数据集的前 100 行。
> dput(Dat2)
structure(list(V1 = c(50.078331, 50.140011, 50.051201, 50.071758,
50.0923, 50.11282, 50.133331, 50.15382, 50.17429, 50.003502,
50.024059, 50.044601, 50.065121, 50.085629, 50.106121, 50.126591,
50.147041, 49.976372, 49.99691, 50.017429, 50.037941, 50.05843,
50.078899, 50.09935, 50.119781, 50.140202, 50.160599, 50.201351,
49.94923, 49.969761, 49.990261, 50.01075, 50.031219, 50.05167,
50.072102, 50.092522, 50.112919, 50.133301, 50.15366, 50.174011,
50.19434, 50.214649, 49.881031, 49.901569, 49.922089, 49.9426,
49.963081, 49.983551, 50.004002, 50.024429, 50.044849, 50.06525,
50.085629, 50.105991, 50.126339, 50.14666, 50.166969, 50.187271,
49.812832, 49.833389, 49.85392, 49.874439, 49.894939, 49.915428,
49.935902, 49.956348, 49.97678, 49.997189, 50.01759, 50.037971,
50.058331, 50.078678, 50.098999, 50.119308, 50.139599, 50.159882,
49.785751, 49.80629, 49.826801, 49.847301, 49.86779, 49.888248,
49.908699, 49.929131, 49.949551, 49.96994, 49.990318, 50.010681,
50.031029, 50.05135, 50.071659, 50.091949, 50.112228, 50.132481,
49.676418, 49.738129, 49.758659, 49.779179, 49.799679, 49.82016
), V2 = c(-117.192703, -117.066025, -117.160736, -117.118576,
-117.076355, -117.034103, -116.991791, -116.949432, -116.907043,
-117.170914, -117.128815, -117.08667, -117.044464, -117.002213,
-116.95993, -116.917587, -116.875198, -117.139038, -117.096954,
-117.05481, -117.012619, -116.970383, -116.928116, -116.885773,
-116.843414, -116.800995, -116.758514, -116.673447, -117.107193,
-117.065125, -117.022995, -116.98082, -116.938599, -116.896347,
-116.854019, -116.811661, -116.769257, -116.726807, -116.684296,
-116.641754, -116.599167, -116.556519, -117.159393, -117.117432,
-117.075409, -117.03334, -116.991226, -116.949066, -116.906876,
-116.864609, -116.822311, -116.779968, -116.737579, -116.695145,
-116.652649, -116.610107, -116.567535, -116.524918, -117.211411,
-117.16954, -117.127625, -117.085663, -117.043655, -117.001617,
-116.959503, -116.917374, -116.875168, -116.832947, -116.790649,
-116.748322, -116.705933, -116.663513, -116.621033, -116.578522,
-116.53595, -116.493347, -117.179657, -117.137802, -117.095901,
-117.053955, -117.011963, -116.969925, -116.927841, -116.885712,
-116.843536, -116.801315, -116.759033, -116.716705, -116.674347,
-116.631943, -116.589478, -116.546967, -116.504425, -116.461823,
-117.31485, -117.189743, -117.147949, -117.10611, -117.064224,
-117.022293), V3 = c(48.3425414364641, 48.3425414364641, 48.3425414364641,
70.1657458563536, 72.6519337016575, 70.1657458563536, 70.1657458563536,
0, 24.8618784530387, 70.1657458563536, 46.4088397790055, 37.8453038674033,
70.1657458563536, 68.232044198895, 58.0110497237569, 0, 24.8618784530387,
70.1657458563536, 69.6132596685083, 26.2430939226519, 70.1657458563536,
67.6795580110497, 0, 24.8618784530387, 47.5138121546961, 79.8342541436464,
79.8342541436464, 75.1381215469613, 61.878453038674, 59.3922651933702,
32.3204419889503, 59.3922651933702, 59.3922651933702, 0, 24.8618784530387,
50.5524861878453, 56.9060773480663, 51.9337016574586, 75.1381215469613,
77.9005524861878, 54.9723756906077, 98.3425414364641, 61.878453038674,
61.6022099447514, 80.3867403314917, 71.5469613259668, 61.0497237569061,
48.3425414364641, 0, 0, 25.414364640884, 25.414364640884, 25.414364640884,
57.182320441989, 45.8563535911602, 46.6850828729282, 46.6850828729282,
79.8342541436464, 61.6022099447514, 70.1657458563536, 61.6022099447514,
46.4088397790055, 37.8453038674033, 32.3204419889503, 32.3204419889503,
58.0110497237569, 0, 24.8618784530387, 58.0110497237569, 56.9060773480663,
56.9060773480663, 37.292817679558, 79.8342541436464, 58.5635359116022,
72.9281767955801, 43.9226519337017, 59.9447513812155, 39.5027624309392,
61.878453038674, 70.1657458563536, 70.1657458563536, 71.5469613259668,
26.2430939226519, 0, 24.8618784530387, 26.5193370165746, 70.1657458563536,
70.1657458563536, 43.3701657458564, 57.182320441989, 51.9337016574586,
72.0994475138122, 53.8674033149171, 72.9281767955801, 27.3480662983425,
51.1049723756906, 61.878453038674, 61.6022099447514, 72.9281767955801,
72.3756906077348), V4 = c(0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0)), .Names = c("V1", "V2", "V3", "V4"), row.names = c(NA,
100L), class = "data.frame")
我得到的输出
可能最简单的方法是在调用中省略 colour
参数。对于 bin2d,颜色就是轮廓 - 当您已经使用填充时不需要轮廓:
library(ggmaps)
ggmap(Koot_base) +
stat_bin2d(data = YearPercDat, aes(x = V2, y = V1, fill = as.factor(V4))) +
scale_fill_discrete(name = "Percent of Year Snow Covered", labels = c("Greater than 0","0"))
如果出于某种原因想要保留颜色,可以将 colour
指南设置为 false:
+ guides(colour = FALSE)
注意,您通过按列子集调用 aes
的方式并不是最好的。最好使用名称 - 即 V4
V2
等,以便正确执行绘图功能执行的任何子集等。