2

我正在尝试通过压裂使用的水量来填充流域。我有数据集(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 ...
4

1 回答 1

1

您需要替换scale_color_gradientn(...)scale_fill_gradientn(...),因为您的审美是填充(多边形内部的绘制)而不是颜色(与多边形接壤的线)。

此外,作为一般规则,请勿dataframe$variable在通话中使用。ggplot就像上面提到的@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 会将其读取为文本列表。

将来,请确保包含可重现的数据片段以诊断您的问题。

于 2017-05-01T00:46:59.460 回答