Add dynamic subtitle using ggplot
You should use function bquote()
instead of expression()
to use titles that are stored as variables. And variable names should be placed inside .()
plot.title = 'TITLE'
plot.subtitle = 'SUBTITLE'
ggplot(mtcars,aes(disp,mpg))+geom_point()+
ggtitle(bquote(atop(.(plot.title), atop(italic(.(plot.subtitle)), ""))))
UPDATE - ggplot2 version 2.2.1
The latest ggplot2 version now can produce subtitles directly, so you don't have to use bquote()
and expression()
. The result is atchieved with argument subtitle =
of function labs()
.
ggplot(mtcars,aes(disp,mpg))+geom_point()+
labs(title = plot.title,subtitle = plot.subtitle) +
theme(plot.subtitle = element_text(face = "italic"))
Create a dynamic title + subtitle using ggplot2
Use bquote
to evaluate the bits you need evaluated. Put the objects you want evaluated in .()
g + ggtitle(bquote(atop(.(title), atop(italic("Location"), ""))))
How to add a ggplot2 subtitle with different size and colour?
The latest ggplot2 builds (i.e., 2.1.0.9000 or newer) have subtitles and below-plot captions as built-in functionality. That means you can do this:
library(ggplot2) # 2.1.0.9000+
secu <- seq(1, 16, by=2)
melt.d <- data.frame(y=secu, x=LETTERS[1:8])
m <- ggplot(melt.d, aes(x=x, y=y))
m <- m + geom_bar(fill="darkblue", stat="identity")
m <- m + labs(x="Weather stations",
y="Accumulated Rainfall [mm]",
title="Rainfall",
subtitle="Location")
m <- m + theme(axis.text.x=element_text(angle=-45, hjust=0, vjust=1))
m <- m + theme(plot.title=element_text(size=25, hjust=0.5, face="bold", colour="maroon", vjust=-1))
m <- m + theme(plot.subtitle=element_text(size=18, hjust=0.5, face="italic", color="black"))
m
Adding dynamic chart titles in ggplot2
It seems like you want a function where you can input a disease and have the plot created.
disease_plot <- function(disease_of_interest) {
DATA %>%
filter(DISEASE == disease_of_interest) %>%
ggplot(aes(x = AREA, y = rate)) +
geom_point() +
geom_hline(aes(yintercept = rate[AREA == "A"]),
linetype = "dashed", color = "red") +
# labs(x = x_label, y = y_label) +
ggtitle(disease_of_interest)
}
disease_plot("Marco Polio")
disease_plot("Chicky Pox")
disease_plot("Mumps")
Or to have them all created at once...
map(unique(DATA$DISEASE), disease_plot)
Adaptively start a newline for ggplot text (title, subtitle, caption, etc.) using R
Imprecise but simple option. This just wraps based on count of characters, regardless of font size, specific letter widths, etc.
text <- stringr::str_wrap(glue("The top {n1} commodities that price rose most are: {s1}; \\
the top {n2} commodities that fell most are: {s2}."), 80)More control option. This uses
ggtext::geom_textbox
to define a textbox with specific width, reflecting font and specific characters. Could also include further options if you want to color or bold certain text within the title.ggplot() +
ggtext::geom_textbox(data = tibble(label = text),
width = unit(5, "inch"), box.colour = NA,
aes(label = label, x = 1, y = 1)) +
theme_void() -> t
t / p + plot_layout(heights = c(1,5))
ggplot2 line between title and subtitle
One possible solution is to manually trace a segment using geom_segment
while setting clip = "off"
on coord_cartesian
:
df <- data.frame(x = 1:10,
y = 2:11)
ggplot(df, aes(x = x, y = y))+
geom_point()+
labs(title = "Main Title", subtitle = "Subtitle")+
coord_cartesian(clip ="off")+
geom_segment(x =0.5, xend = 2.5, y = 12.1, yend = 12.1)
You will have to adapt the position based on data you are going to plot but it is one possible solution
How to use max of a variable in title/subtitle of a plot with glue in r?
Try to first save the data in a separate tibble
x
and then to callmax(x$Cases_Count)
.
fn_daily_cases_plot <- function(country_selected = India) {
x <- fn_daily_cases({{country_selected}})
ggplot(x, aes(Date, y = daily_cases)) +
geom_line(col = "midnightblue") +
labs(title = glue("{quo_name(enquo(country_selected))} Daily Cases") ,
subtitle = glue("Total cases so far: {max(x$Cases_Count)}" )) +
theme_light()
}
Anyways, I don't know why you prefer glue
over the base alternative paste
(see also this question).
Related Topics
Three-Way Color Gradient Fill in R
How to Fill Nas with Locf by Factors in Data Frame, Split by Country
Texture in Barplot for 7 Bars in R
Wrap Long Text in Kable Table Column
How to Split a Data Frame by Rows, and Then Process the Blocks
R - Run Source() in Background
Combining Elements of List of Lists by Index
Ctree() - How to Get the List of Splitting Conditions for Each Terminal Node
Row-By-Row Operations and Updates in Data.Table
Dynamically Add Function to R6 Class Instance
Saving Plot as PDF and Simultaneously Display It in the Window (X11)
Polygons Nicely Cropping Ggplot2/Ggmap at Different Zoom Levels
Differencebetween a List and a Pairlist in R
How to Generate Bin Frequency Table in R
Convert a Matrix with Dimnames into a Long Format Data.Frame
Find Overlapping Dates for Each Id and Create a New Row for the Overlap