R ggplot2

时间:2017-04-30 21:43:25

标签: r ggplot2 aesthetics

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

1 个答案:

答案 0 :(得分:1)

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

此外,请勿在{{1​​}}次通话中使用dataframe$variable作为一般规则。就像上面提到的@mikeck和@marcelo一样,你想要指定你的所有美学和数据一起变成ggplot语句,你可以在那里写下不带引号的列名:

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。如果它说该列是一个因子,那么R将您的数据解释为类别,而不是连续变量。一个常见原因是当您有一个数字列表str(well_watershed_ok)来表示缺失值时; R会将其读作文本列表。

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