Quantcast
Channel: r software hub
Viewing all articles
Browse latest Browse all 1015

ggplot2で字幕 [Subtitles in ggplot2]

$
0
0

By hrbrmstr

2300lawyers0116-2

Subtitles aren’t always necessary for plots, but I began to use them enough that I whipped up a function for ggplot2 that does a decent job adding a subtitle to a finished plot object. More than a few folks have tried their hand at this in the past and this is just my incremental contribution until there’s proper support in ggplot2 (someone’s bound to add it via PR at some point).

We’ll nigh fully recreate the following plot from this NYTimes article:

Here’s a stab at that w/o the subtitle:

library(ggplot2)
library(scales)

data.frame(
yrs=c("1789-90", "1849-50", "1909-10", "1965-66", "2016-16"),
pct=c(0.526, 0.795, 0.713, 0.575, 0.365),
xtralabs=c("", "Highest:n", "", "", "Lowest:n")
) -> hill_lawyers

gg ggplot(hill_lawyers, aes(yrs, pct))
gg gg + geom_bar(stat="identity", width=0.65)
gg gg + geom_label(aes(label=sprintf("%s%s", xtralabs, percent(pct))),
vjust=-0.4, family=c(rep("FranklinGothic-Book", 4),"FranklinGothic-Heavy"),
lineheight=0.9, size=4, label.size=0)
gg gg + scale_x_discrete()
gg gg + scale_y_continuous(expand=c(0,0), limits=c(0.0, 1.0), labels=percent)
gg gg + labs(x=NULL, y=NULL, title="Fewer and fewer lawyers on the Hill")
gg gg + theme_minimal(base_family="FranklinGothic-Book")
gg gg + theme(axis.line=element_line(color="#2b2b2b", size=0.5))
gg gg + theme(axis.line.y=element_blank())
gg gg + theme(axis.text.x=element_text(family=c(rep("FranklinGothic-Book", 4),
"FranklinGothic-Heavy")))
gg gg + theme(panel.grid.major.x=element_blank())
gg gg + theme(panel.grid.major.y=element_line(color="#b2b2b2", size=0.1))
gg gg + theme(panel.grid.minor.y=element_blank())
gg gg + theme(plot.title=element_text(hjust=0,
...read more

Source:: r-bloggers.com


Viewing all articles
Browse latest Browse all 1015

Trending Articles