0
我正在與西雅圖的犯罪數據。以下是一個示例數據集。我有兩個問題。如何迭代製作圖形?
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
我當前已經創造了「summarized.offense.description」我很感興趣的一個新的過濾數據集,如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:我沒有爲每個我感興趣的summange.offense.description創建一個新的過濾數據集。如何在我的彙總數據集中爲前四個summange.offense.description創建並保存一個新地圖(在這種情況下,它會是CAR PROWL,BURGLARY,FRAUD,OTHER PROPERTY)?
問題2:如何製作排名前四的summary.offense.description的方面圖?
任何洞察到我的問題將不勝感激。
謝謝!
是的。感謝您的幫助! – RunAmuck