rickie
rickie

Reputation: 21

ggplot separated by biotopes

I am quite new to R, so hopefully this will be easy:

I have a dataset of different biotopes (arable land, water bodies, settlement etc.) which I want to plot by means of their Area and Perimeter on the x and y axis. Colors should indicate the different biotopes. I succeeded by

qplot(Data$Perimeter_meter, Data$Area_meter, color= Data$biotopes)

As there are two biotopes of huge sizes, the main biotopes are not very well visible/presented in this plot (very small and skewed in the left corner). I tried to eliminate them by creating a subset:

ggplot(subset(Data[which(Data$Perimeter_meter <= 90000 &       
                         Data$Area_meter < 5000),]),
     aes(x = Data$Perimeter_meter, y = Data$Area_meter, 
        colour = Data$biotopes))

The dput(head(Data)) is:

    new("SpatialPolygonsDataFrame"
    , data = structure(list(OBJECTID = c(1, 2, 3, 4, 5, 6), HG_STR = structure(c(6L, 
6L, 3L, 3L, 3L, 8L), .Label = c("02", "04", "05", "07", "08", 
"09", "10", "12"), class = "factor"), HG_NUM = c(9, 9, 5, 5, 
5, 12), HGUG2_STR = structure(c(25L, 25L, 10L, 10L, 10L, 32L), .Label = c("0210", 
"0212", "0215", "0221", "0412", "0510", "0511", "0513", "0514", 
"0515", "0710", "0711", "0717", "0810", "0828", "0830", "0831", 
"0832", "0835", "0836", "0848", "0850", "0851", "0868", "0913", 
"0914", "1010", "1011", "1021", "1023", "1212", "1214"), class = "factor"), 

PATCHID = c(4, 1364, 1444, 1549, 1573, 1580), Shape_Leng = c(2515.41907685, 
69731.2831506, 13900.6309242, 873.269546963, 1865.70715484, 
915.779630011), Shape_Area = c(95171.5598722, 6997023.54296, 
900797.40161, 23226.8911895, 59557.1082323, 30295.3228437
), biotopes = c("arable land", "arable land", "grassland", 
"grassland", "grassland", "settlement, traffic areas"), Area_meter = c(95171.56, 
6997023.54, 900797.4, 23226.89, 59557.11, 30295.32), Perimeter_meter = c(
    92290.15, 
        94507.9, 20812.99, 1344.65, 2405.33, 8100.22)), .Names = c("OBJECTID", 
    "HG_STR", "HG_NUM", "

HGUG2_STR", "PATCHID", "Shape_Leng", "Shape_Area", 
    "biotopes", "Area_meter", "Perimeter_meter"), row.names = c("0", 
    "1", "2", "3", "4", "5"), class = "data.frame")
        , polygons = list(<S4 object of class structure("Polygons", package = "sp")>, 
        <S4 object of class structure("Polygons", package = "sp")>, 
        <S4 object of class structure("Polygons", package = "sp")>, 
        <S4 object of class structure("Polygons", package = "sp")>, 
        <S4 object of class structure("Polygons", package = "sp")>, 
        <S4 object of class structure("Polygons", package = "sp")>)
        , plotOrder = c(2L, 3L, 1L, 5L, 6L, 4L)
        , bbox = structure(c(3423870, 5865918, 3428870, 5868918), .Dim = c(2L, 
    2L), .Dimnames = list(c("x", "y"), c("min", "max")))
        , proj4string = new("CRS"
        , projargs = "+proj=tmerc +lat_0=0 +lon_0=15 +k=0.9996 +x_0=3500000 +y_0=0 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0"
    )
    )

error still occurs: "Regions defined for each Polygons Fehler in eval(expr, envir, enclos) : Objekt 'Perimeter_meter' not found

Upvotes: 1

Views: 65

Answers (1)

Spacedman
Spacedman

Reputation: 94182

get the columbus spatial polygons data frame:

require(spdep)
example(columbus)

then do a scatter plot of perim, area coloured by Y, on a subset:

ggplot(subset(data.frame(columbus),AREA>0.1), aes(x=PERIMETER, y=AREA,colour=Y))  + geom_point()

Note: 1. conversion to data.frame (ggplot doesn't like sp-class objects here, so strip the spatial off and make it a plain data frame). 2. No need to say columbus$AREA because ggplot uses non-standard evaluation and just gets the name from the data frame; 3. use of ggplot(data, aes=...) + geom_point() to draw a scatter plot.

Upvotes: 2

Related Questions