我正在制作一个情节,以展示迈阿密十大监狱预订,以便更加熟悉图形库。我遇到了两个关键问题:
所以我想问题是...... 如何让我的轴在图形的其余部分上强制执行空间,以及如何使我的标题居中。这是迄今为止的代码:
以下是代码:
p = d %>%
ggplot(aes(x = factor(bookMonth), y = chargeObs, fill = factor(Charge1))) +
geom_bar(position = "stack", stat = "identity") +
facet_wrap(~ bookYear) +
ggtitle("Top 10 Miami Jail Bookings") +
labs(y="Bookings (thousands)", x="Date") +
scale_fill_brewer(palette="Spectral", name = "Charge") +
theme(plot.title = element_text(size = rel(2)),
panel.background = element_rect(fill = "white"),
panel.grid.major = element_line(colour = "#eaeded"),
strip.background = element_rect(fill = "#eaeded", size = 1),
axis.title.y = element_text(size = rel(1.25), angle = 90),
axis.title.x = element_text(size = rel(1.25), angle = 0)
)
ggplotly(p)
这是操纵代码:
library(plotly)
library(plyr)
library(dplyr)
library(rjson)
library(lubridate)
#Read Data in.
correctionsInit = read.csv("C:\\data\\jailbookings.csv")
correctionsInit = correctionsInit %>% tbl_df()
corrections = correctionsInit
#Number of Charges we want to display
#ie top 10.
numCharges = 10
#Questions
corrections$BookDate = as.Date(corrections$BookDate, format = "%m/%d/%Y")
#
#Rework some of the labelling.
#
ChargeLUT = c("BENCH WARRANT" = "Misc. Warrants",
"PROBATION WARRANT" = "Misc. Warrants",
"OUT-OF-CNTY/WARRANT" = "Misc. Warrants",
"ALIAS CAPIAS" = "Misc. Warrants",
"ASSAULT/AGGRAVATED" = "Assault",
"DOMESTIC VIOL WARRNT" = "Misc. Warrants",
"GRAND THEFT/ARM/CONS" = "Grand Theft",
"GRAND THEFT 2ND DEG" = "Grand Theft",
"GRAND THEFT 3RD DEG" = "Grand Theft",
"COKE/SELL/DEL/W/INT" = "Drug Traffic",
"CONT SUB/SELL/DEL" = "Drug Traffic",
"DWLS/HABITUAL" = "Drivers Lisence",
"DWLS/KNOWINGLY" = "Drivers Lisence",
"CANN/TRF/25-2000LBS" = "Drug Traffic",
"CANN/POSN/-20/ATTEMP" = "Drug Possession",
"DWLS/3RD & SUBS OFFN" = "Drivers Lisence",
"NO VALID DRIVERS LIC" = "Drivers Lisence",
"CANNABIS/POSN/0-20" = "Drug Possession",
"COCAINE/POSSESSION" = "Drug Possession",
"ALC BEV/DRK IN PUBLC" = "Disorderly in Public",
"DISORDERLY CONDUCT" = "Disorderly in Public",
"PETIT THEFT" = "Theft",
"PETIT THEFT 1D" = "Theft",
"ASSAULT/AGG/FIREARM" = "Armed Assault",
"ASSAULT/AGG/DWEAP" = "Armed Assault",
"NO VALID DRIVERS LIC" = "Drivers Lisence",
"GRD THEFT/3D/VEHICLE" = "Grand Theft",
"CONT SUBS/POSS" = "Drug Possession",
"ALCOHOL/PUB/MIA BCH" = "Disorderly in Public",
"DL/EXPIRED 6 MTHS+" = "Drivers Lisence",
"ALC/OPN CNTR/POSN" = "Drug Possession",
"INDECENT EXPOSURE" = "Disorderly in Public",
"DISORDERLY INTOX" = "Disorderly in Public",
"RESIST OFF W/O VIOL" = "Resist Officer",
"BATTERY/AGGRAVATED" = "Battery",
"LOITERING OR PROWL" = "Loitering",
"ARREST WARRANT" = "Misc. Warrants",
"ROBBERY/STRONGARM" = "Theft",
"RETAIL THEFT/300>" = "Theft",
"BATTERY" = "Battery",
"BATT/DOM/STRANGLE" = "Battery",
"DRUG PARAPHERNA/POSN" = "Drug Possession",
"TRES PROP/AFTER WARN" = "Misdemeanor",
"BURGLARY/UNOCC CONVY" = "Theft",
"CANN/SELL/DEL/PSNW/I" = "Drug Traffic",
"BATTERY/LEO/COR/FIRE" = "Battery",
"FUG WARR/OUT O STATE" = "Misc. Warrants",
"TRESPASS/STRUC/UNOCC" = "Misdemeanor",
"DIS COND/ESTAB" = "Disorderly in Public",
"RECKLESS DRIVING" = "Reckless Driving",
"RBRY/ARM/FA/DW - PBL" = "Theft",
"BURGLARY/OCC/DWELL" = "Theft",
"BATTERY/AGG/DWEAP" = "Armed Assault",
"DUI/DAM/PROP/PERSON" = "DUI",
"ALCOHOL/CURB DRNKNG" = "Disorderly in Public",
"DUI/BREATH LEVEL" = "DUI",
"BATTERY/FELONY" = "Battery",
"ASSAULT" = "Assault",
"PROBATION VIOLATION" = "Probation",
"CRIM MIS/1000+" = "Misdemeanor",
"CRIM MIS/200-1000" = "Misdemeanor",
"BURGLARY/UNOCC/DWELL" = "Theft",
"WRIT/BODILY ATTACH" = "Misc. Warrants",
"PANHANDLING/BEG/SOL" = "Misdemeanor",
"CONCEALED F/A /CARRY" = "Weapons",
"CANNABIS/POSN/20+" = "Drug Possession",
"BURGLARY/UNOCC/PREJU" = "Theft",
"PROST/COMMIT/ENGAGE" = "Prostitution",
"HOLD FOR AGENCY" = "Misc. Warrants",
"BATTERY/AGG/PREGNANT" = "Battery",
"LSA/PROP DAMAGE" = "Reckless Driving",
"PANHANDLE/AGGRESV" = "Misdemeanor",
"ALCOHOL/CONSUM/STORE" = "Disorderly in Public",
"CRIM MIS/0-200" = "Misdemeanor",
"FA/WEAP/POSN/FEL/DEL" = "Weapons",
"BURGLARY/UNOCC/STRUC" = "Theft",
"UTTERING/CHECK" = "Forgery",
"DOM VIOL/INJUNC VIOL" = "Assault",
"GT 3RD/VEHICLE/CONS" = "Grand Theft",
"FLEE/ELUDE PO" = "Fleeing",
"BURG/ASLT/BATT - PBL" = "Theft",
"BATTERY/ELDERLY" = "Battery",
"PARK/ENT AFT HRS" = "Misdemeanor",
"BATTERY/AGG/HRM/WEAP" = "Armed Assault",
"STOLEN PROP/DEAL IN" = "Theft",
"TRESP/STRUC/OCC/WARN" = "Misdemeanor",
"HER/SELL/DEL W/INT" = "Drug Traffic",
"BUSINESS W/O LIC" = "Bussiness Offense",
"PETIT THEFT 2> CONV" = "Theft",
"HOLD/CORRECTIONS" = "Misc. Warrants",
"PANHANDLE/AGGR/OBSTR" = "Misdemeanor",
"BATTERY/ATTEMPT" = "Assault",
"ORGANIZED FRD/0-20K" = "Business Offense",
"RESIST OFF W/VIOL" = "Resist Officer",
"FLS NAME/ID/AFT ARRE" = "Other",
"RETAIL THFT/PRI CON" = "Theft",
"BOATING UNDER INFLU" = "DUI",
"ATT FEL MUR/DLY WEA" = "Armed Assault"
)
#Rename for real.
corrections$Charge1 = corrections$Charge1 %>%
revalue(ChargeLUT)
#What are the top issues
#aside from warrants.
topCharges = corrections %>%
filter(Charge1 != "") %>%
filter(Charge1 != "Misc. Warrants") %>%
group_by(Charge1) %>%
summarise(
totalCharge1 = n()
) %>%
top_n(numCharges, totalCharge1)
#Get data together and in good display format
d = corrections %>%
mutate(
bookYear = year(BookDate),
bookMonth = ordered(months(BookDate, abbreviate = TRUE), month.abb)
) %>%
filter(Charge1 != "") %>%
filter(Charge1 != "Misc. Warrants") %>%
group_by(bookYear, bookMonth, Charge1) %>%
summarise(
chargeObs = n() / 1000
) %>%
mutate(
total = sum(chargeObs)
) %>%
filter(Charge1 %in% topCharges$Charge1)
答案 0 :(得分:2)
不一定是答案,但是Github issue正在跟踪此问题并寻求解决方案。不幸的是,目前还没有解决方法。
答案 1 :(得分:2)
我遇到了类似的问题并手动调整了plot.ly图形对象的边距:
plotly_object$x$layout$margin$l <- plotly_object$x$layout$margin$l + 30
plotly_object$x$layout$margin$b <- plotly_object$x$layout$margin$b + 30