我试图通过压裂所用的水填充流域。我有数据集(well_watershed_ok(和变量(H2O_BBL_T(。每个流域都有一个价值。这是我正在使用的代码。
ggplot() +
geom_polygon(data = well_watershed_ok, aes(x = long, y = lat),
fill = well_watershed_ok$H2O_BBL_T) +
labs(x = "", y = "", title = "Number of Barrells of Water used per Day") +
scale_color_gradientn("Water BBL/Day", colors = c( "#660000", "#f9f3c2")) +
theme(legend.position = "bottom", axis.ticks.y = element_blank(),
axis.text.y = element_blank(), axis.ticks.x = element_blank(),
axis.text.x = element_blank(),
plot.title = element_text(lineheight = .8, face="bold", vjust = 1, size = 12)) +
coord_equal(ratio=1)
Regions defined for each Polygons
Error: Aesthetics must be either length 1 or the same as the data (222074): fill
因为我遇到了这个错误,所以我检查了我的长度,但它们相等,但后来我检查了计算机是否认识到它们是否相等,这是错误的。我该怎么办?
identical(well_watershed_ok,well_watershed_ok$H2O_BBL_T)
[1] FALSE
length(well_watershed_ok)
[1] 79
length(well_watershed_ok$H2O_BBL_T)
[1] 79
这是我的数据的样子
data.frame': 79 obs. of 37 variables:
$ FID_1 : Factor w/ 79 levels "0","1","10","11",..: 1 2 13 24 35 46 57 68
78 79 ...
$ HUC_8 : Factor w/ 68 levels "11040001","11040002",..: 1 2 3 3 3 3 4 5 6
7 ...
$ ACRES : num 26176 420687 188128 1281 657 ...
$ HU_8_NAME : Factor w/ 68 levels "11040001 - CIMARRON HEADWATERS",..: 1 2 3 3
3 3 4 5 6 7 ...
$ STATES : Factor w/ 8 levels "AR,OK","AR,OK,TX",..: 5 7 3 3 3 3 3 7 7 7 ...
$ SQ_MILES : num 40.9 657.32 293.95 2 1.03 ...
$ Count_ : Factor w/ 40 levels "0","1","10","12",..: 1 14 17 2 1 34 1 20 26
16 ...
$ Sum_Lat_Y : num 0 73.8 775.6 37 0 ...
$ Sum_Long_X: num 0 -204 -2111 -101 0 ...
$ Sum_DF_Ele: num 0 0 0 0 0 ...
$ Sum_GF_Ele: num 0 7691 54745 3223 0 ...
$ Sum_Total_: num 0 9389 144678 6720 0 ...
$ Sum_OilBBL: num 0 0 0 0 0 0 0 181 166 698 ...
$ Sum_Oil_Gr: num 0 0 0 0 0 ...
$ Sum_GasMCF: num 0 0 0 0 0 ...
$ Sum_GasOil: num 0 0 0 0 0 ...
$ Sum_WaterB: num 0 0 0 0 0 ...
$ H2O_BBL_T : num 0 642 0 0 0 ...
$ FID_12 : Factor w/ 79 levels "0","1","10","11",..: 1 2 13 24 35 46 57 68
78 79 ...
$ FID_12_13 : Factor w/ 79 levels "0","1","10","11",..: 1 2 13 24 35 46 57 68
78 79 ...
$ HUC_89 : Factor w/ 68 levels "11040001","11040002",..: 1 2 3 3 3 3 4 5 6
7 ...
$ ACRES_1 : num 26176 420687 188128 1281 657 ...
$ HU_8_NAM_1: Factor w/ 68 levels "11040001 - CIMARRON HEADWATERS",..: 1 2 3 3
3 3 4 5 6 7 ...
$ STATES_1 : Factor w/ 8 levels "AR,OK","AR,OK,TX",..: 5 7 3 3 3 3 3 7 7 7
...
$ SQ_MILES_1: num 40.9 657.32 293.95 2 1.03 ...
$ Count1 : Factor w/ 39 levels "0","1","107",..: 1 24 2 1 1 14 1 31 5 22
...
$ Sum_DF_E_1: num 0 0 0 0 0 ...
$ Sum_GF_E_1: num 0 12299 2499 0 0 ...
$ Sum_Total1: num 0 20844 6950 0 0 ...
$ Sum_OilB_1: num 0 20 0 0 0 ...
$ Sum_Oil__1: num 0 0 0 0 0 ...
$ Sum_GasM_1: num 0 20 0 0 0 ...
$ Sum_GasO_1: num 0 1000 0 0 0 ...
$ Sum_Wate_1: num 0 642 0 0 0 ...
您需要用 scale_fill_gradientn(...)
替换 scale_color_gradientn(...)
,因为您的美学已填充(在多边形内部的绘制(不彩色(与多边形接壤的线(。
另外,请勿在ggplot
中使用dataframe$variable
作为一般规则。就像上面提到的@mikeck和@marcelo一样,您要指定所有与数据一起在aes(...)
语句中变化的美学,您可以在其中只编写未引用的列名:
ggplot(well_watershed_ok, aes(x=long, y=lat, fill=H2O_BBL_T) +
geom_polygon() +
scale_fill_gradientn(name = "Water BBL/Day", colors = c("#660000", "#f9f3c2"))
最后,您需要确保well_watershed_ok$H2O_BBL_T
是数字向量。尝试str(well_watershed_ok)
。如果说该列是一个因素,那么R将您的数据解释为类别,而不是连续变量。一个共同的原因是,当您有一个具有.
的数字列表以表示缺失值;r将以文字列表读取。
将来,请确保包含数据的可重复段来诊断您的问题。