2

I'm trying to fill watersheds by amount of water used by fracking. I have the dataset(well_watershed_ok) and the variable(H2O_BBL_T). There is one value for each watershed. This was the code I was using.

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

Because I got this error I check my lengths and they are equal but then I checked to see if the computer recognized that they were equal and it was false. What do I do?

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

Here is what my data looks like

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 ...
AU95
  • 21
  • 1
  • 3
  • Does it work if you put the `fill` specification inside the `aes` call? Try `ggplot(well_watershed_ok, aes(x=long, y=lat, fill=H2O_BBL_T) +geom_polygon()`. Your may also need to specify the `group` aesthetic. – mikeck Apr 30 '17 at 21:59
  • No, I get the same error. What do you mean by specify the group aesthetic? Sorry, I am new to R – AU95 Apr 30 '17 at 22:24
  • It should be `geom_polygon(data = well_watershed_ok, aes(x = long, y = lat, fill = well_watershed_ok$H2O_BBL_T))` since you are using fill as part of mapping. – Marcelo Apr 30 '17 at 22:34
  • > 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"))+ Regions defined for each Polygons Error: Aesthetics must be either length 1 or the same as the data (222074): x, y, fill – AU95 Apr 30 '17 at 22:39
  • I still get the same error – AU95 Apr 30 '17 at 22:39
  • 1
    Please put an example of your data, e.g. `head (well_watershed_ok)` or `str(well_watershed_ok)`. Hard to tell what the problem is without understanding the data format. – mikeck May 01 '17 at 14:33

1 Answers1

1

You need to replace scale_color_gradientn(...) with scale_fill_gradientn(...), since your aesthetic is fill (the painted inside of the polygon) not colour (the line bordering the polygon).

Also, don't use dataframe$variable in ggplot calls as a general rule. Like @mikeck and @marcelo mentioned above, you want to specify all your aesthetics that vary along with the data to be inside an aes(...) statement, where you can just write the unquoted column name:

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"))

Finally, you need to make sure that well_watershed_ok$H2O_BBL_T is a numeric vector. Try str(well_watershed_ok). If it says that that column is a factor, then R has interpreted your data as being categories, not a continuous variable. A common cause is when you have a list of numbers with . to represent missing values; R will read that as a list of text instead.

In the future, make sure to include a reproducible snippet of your data for diagnosing your problem.

Brian
  • 7,900
  • 1
  • 27
  • 41
  • Thank you for your help, When I don't use dataframe$variable, then it does not recognize the variable '> ggplot(well_watershed_ok, aes(x=long, y=lat,fill=H2O_BBL_T))+geom_polygon()+labs(x="", y="", title="Number of Barrells of Water used per Day")+scale_fill_gradientn("Water BBL/Day", colors=c( "#660000", "#f9f3c2"))' Regions defined for each Polygons Error in eval(expr, envir, enclos) : object 'H2O_BBL_T' not found – AU95 May 01 '17 at 03:55
  • Thank you for your help, When I don't use dataframe$variable, then it does not recognize the variable `> ggplot(well_watershed_ok, aes(x=long, y=lat,fill=H2O_BBL_T))+geom_polygon()+labs(x="", y="", title="Number of Barrells of Water used per Day")+scale_fill_gradientn("Water BBL/Day", colors=c( "#660000", "#f9f3c2"))` Regions defined for each Polygons Error in eval(expr, envir, enclos) : object 'H2O_BBL_T' not found, also I did the str test and the variables are numbers – AU95 May 01 '17 at 04:01
  • It sounds like the error is somewhere upstream in your code then. You should provide a minimal reproducible example, see: http://stackoverflow.com/help/mcve and http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Brian May 01 '17 at 12:20