如何迭代制作图表?

时间:2017-07-20 02:22:36

标签: r ggplot2 ggmap

我正在处理西雅图犯罪数据。下面是一个示例数据集。我有两个问题。

library(ggplot2)
library(ggmap)

 SPD_2015 <-   structure(list(summarized.offense.description = c("OTHER PROPERTY", 
    "CAR PROWL", "ASSAULT", "SHOPLIFTING", "VEHICLE THEFT", "OTHER PROPERTY", 
    "OTHER PROPERTY", "PROSTITUTION", "CAR PROWL", "PROPERTY DAMAGE", 
    "CAR PROWL", "ASSAULT", "FRAUD", "SHOPLIFTING", "ROBBERY", "WARRANT ARREST", 
    "VEHICLE THEFT", "CAR PROWL", "PROPERTY DAMAGE", "ASSAULT", "VEHICLE THEFT", 
    "OTHER PROPERTY", "CAR PROWL", "FRAUD", "CAR PROWL", "CAR PROWL", 
    "CAR PROWL", "THREATS", "CAR PROWL", "DISTURBANCE", "CAR PROWL", 
    "CAR PROWL", "EMBEZZLE", "THREATS", "CAR PROWL", "PROPERTY DAMAGE", 
    "STOLEN PROPERTY", "ASSAULT", "LOST PROPERTY", "BURGLARY-SECURE PARKING-RES", 
    "THREATS", "PROPERTY DAMAGE", "FRAUD", "CAR PROWL", "BURGLARY", 
    "ASSAULT", "THEFT OF SERVICES", "OTHER PROPERTY", "DISTURBANCE", 
    "BIKE THEFT", "BURGLARY", "CAR PROWL", "FRAUD", "CAR PROWL", 
    "VEHICLE THEFT", "DISTURBANCE", "BURGLARY", "BURGLARY", "BURGLARY", 
    "OTHER PROPERTY", "CAR PROWL", "CAR PROWL", "BURGLARY", "BURGLARY", 
    "OTHER PROPERTY", "FRAUD", "CAR PROWL", "BURGLARY", "NARCOTICS", 
    "THREATS", "PROPERTY DAMAGE", "TRESPASS", "ASSAULT", "FRAUD", 
    "CAR PROWL", "BURGLARY", "CAR PROWL", "BURGLARY-SECURE PARKING-RES", 
    "FRAUD", "CAR PROWL", "FRAUD", "THREATS", "CAR PROWL", "BURGLARY", 
    "TRESPASS", "TRESPASS", "OTHER PROPERTY", "STOLEN PROPERTY", 
    "STOLEN PROPERTY", "WARRANT ARREST", "WARRANT ARREST", "FRAUD", 
    "CAR PROWL", "OTHER PROPERTY", "PROPERTY DAMAGE", "BURGLARY", 
    "FRAUD", "OTHER PROPERTY", "FRAUD", "CAR PROWL"), longitude = c(-122.300109863, 
    -122.385444641, -122.269958496, -122.341133118, -122.311935425, 
    -122.256233215, -122.344665527, -122.302001953, -122.344993591, 
    -122.311782837, -122.325790405, -122.337394714, -122.317298889, 
    -122.365219116, -122.33140564, -122.343269348, -122.300140381, 
    -122.280647278, -122.349700928, -122.340240479, -122.354415894, 
    -122.345626831, -122.317359924, -122.378921509, -122.390213013, 
    -122.354415894, -122.337089539, -122.280601501, -122.359313965, 
    -122.337791443, -122.330421448, -122.343261719, -122.396110535, 
    -122.311546326, -122.316917419, -122.262084961, -122.340454102, 
    -122.320770264, -122.315254211, -122.344444275, -122.304519653, 
    -122.319442749, -122.36756134, -122.330039978, -122.337348938, 
    -122.330810547, -122.303710938, -122.327880859, -122.382667542, 
    -122.322769165, -122.313537598, -122.301094055, -122.4034729, 
    -122.333267212, -122.32888031, -122.382377625, -122.310951233, 
    -122.318778992, -122.326576233, -122.354827881, -122.382377625, 
    -122.378768921, -122.315391541, -122.311248779, -122.311393738, 
    -122.32408905, -122.367424011, -122.338768005, -122.297531128, 
    -122.374198914, -122.348678589, -122.326385498, -122.33303833, 
    -122.381492615, -122.338088989, -122.282745361, -122.316902161, 
    -122.355461121, -122.389198303, -122.32635498, -122.404212952, 
    -122.313087463, -122.343833923, -122.304168701, -122.3854599, 
    -122.296226501, -122.318733215, -122.332801819, -122.316726685, 
    -122.323440552, -122.332260132, -122.290527344, -122.337585449, 
    -122.344940186, -122.31678009, -122.376319885, -122.31816864, 
    -122.335906982, -122.355148315, -122.355621338), latitude = c(47.595077515, 
    47.556591034, 47.670768738, 47.610042572, 47.664890289, 47.497062683, 
    47.702514648, 47.583400726, 47.725036621, 47.526573181, 47.700252533, 
    47.612663269, 47.564403534, 47.521022797, 47.602767944, 47.608207703, 
    47.610794067, 47.535404205, 47.57101059, 47.612014771, 47.634437561, 
    47.660072327, 47.669715881, 47.680427551, 47.521442413, 47.66809082, 
    47.607299805, 47.724998474, 47.687664032, 47.60974884, 47.620243073, 
    47.61145401, 47.549030304, 47.60710907, 47.619354248, 47.509685516, 
    47.686210632, 47.613517761, 47.664012909, 47.608901978, 47.589576721, 
    47.717647552, 47.642562866, 47.606300354, 47.52047348, 47.600463867, 
    47.609523773, 47.623706818, 47.665958405, 47.649650574, 47.593112946, 
    47.602165222, 47.573997498, 47.58398056, 47.630302429, 47.591312408, 
    47.595115662, 47.660381317, 47.626041412, 47.549259186, 47.591312408, 
    47.567428589, 47.662197113, 47.629676819, 47.62008667, 47.602870941, 
    47.673809052, 47.606601715, 47.610782623, 47.56407547, 47.613479614, 
    47.607337952, 47.604553223, 47.666133881, 47.712303162, 47.727027893, 
    47.618183136, 47.705989838, 47.652839661, 47.600868225, 47.665912628, 
    47.66399765, 47.688751221, 47.691646576, 47.561988831, 47.707542419, 
    47.670059204, 47.611839294, 47.624809265, 47.604129791, 47.605373383, 
    47.632568359, 47.726856232, 47.71957016, 47.605884552, 47.551052094, 
    47.615837097, 47.600463867, 47.632316589, 47.635715485)), .Names = c("summarized.offense.description", 
    "longitude", "latitude"), row.names = c(NA, -100L), class = c("tbl_df", 
    "tbl", "data.frame"))

我总结了我的数据,看看我在做什么:

group_by(SPD_2015, summarized.offense.description) %>%
  summarize(count = n()) %>%
  arrange(desc(count))

# A tibble: 21 × 2
   summarized.offense.description count
                            <chr> <int>
1                       CAR PROWL    24
2                        BURGLARY    11
3                           FRAUD    11
4                  OTHER PROPERTY    10
5                         ASSAULT     6
6                 PROPERTY DAMAGE     6
7                         THREATS     5
8                   VEHICLE THEFT     4
9                     DISTURBANCE     3
10                STOLEN PROPERTY     3
# ... with 11 more rows

我目前正在创建一个新的过滤数据集&#34; summa.offense.description&#34;我感兴趣的,比如CAR PROWL:

car.prowl <- SPD_2015 %>%
  filter(summarized.offense.description == "CAR PROWL")

然后映射所述数据:

ggmap(seattle.map) +
  geom_point(data = car.prowl, aes(car.prowl$longitude, car.prowl$latitude),
             alpha = 0.2, color = "tomato4", size = 0.7) +
  theme(axis.text = element_blank()) +
  theme(axis.title = element_blank())

问题1:不是为我感兴趣的每个summa.offense.description创建一个新的过滤数据集。如何在我的汇总数据集中为前四个summa.offense.description创建和保存新地图(在这种情况下,它将是CAR PROWL,BURGLARY,欺诈,其他财产)?

问题2:如何制作前四名summa.offense.description的方面图?

我将非常感谢您对我的问题的任何见解 谢谢!

1 个答案:

答案 0 :(得分:2)

对于第一个请求,我会执行以下操作:

# Get Seattle Map
seattle.map <- qmap("seattle", zoom = 11, source="stamen", maptype="toner",darken = c(.3,"#BBBBBB"))

# Get ordered summarized data
ordered <- group_by(SPD_2015, summarized.offense.description) %>%
  summarize(count = n()) %>%
  arrange(desc(count))

# Make the ordered summarized data a dataframe and then use only the first column
# to be a character vector with the top 4
ordered <- as.data.frame(ordered[1:4,1])
ordered <- ordered$summarized.offense.description

# Make a list to comprehend the 4 ggmap plots
ggmap_list <- list()

# Plot the 4 ggmaps
for(i in 1:4){

  subst <- SPD_2015[SPD_2015$summarized.offense.description == ordered[i],]

  ggmap_list[[i]] <- seattle.map +
    geom_point(data = subst, aes(longitude, latitude),
               alpha = 0.2, color = "red", size = 0.7) +
    theme(axis.text = element_blank()) +
    theme(axis.title = element_blank())
}

关于第二个请求,我会尝试:

# Make a subset of the data with the top 4
subst <- SPD_2015[SPD_2015$summarized.offense.description %in% ordered,]


# Plot all 4 ggmaps on the same plotting region
seattle.map +
  geom_point(data = subst, aes(longitude, latitude),
             alpha = 0.2, color = "red", size = 0.7) +
  facet_wrap(~summarized.offense.description) +
  theme(axis.text = element_blank()) +
  theme(axis.title = element_blank())

facetted ggmap

请注意,我更改了颜色以获得更好的可视化效果,并且我尝试重现西雅图地图

这是你想要的吗?