Different Font Faces and Sizes Within Label Text Entries in Ggplot2

Different font faces and sizes within label text entries in ggplot2

I also think that I could not to make the graph by only using ggplot2 features.

I would use grid.text and grid.gedit.

require(ggplot2)
Treatment <- rep(c('T','C'), each=2)
Gender <- rep(c('Male','Female'), 2)
Response <- sample(1:100, 4)
test_df <- data.frame(Treatment, Gender, Response)

xbreaks <- levels(test_df$Gender)
xlabels <- paste(xbreaks,'\n',c('',''))

hist <- ggplot(test_df, aes(x=Gender, y=Response, fill=Treatment,
stat="identity"))
hist + geom_bar(position = "dodge") +
scale_y_continuous(limits = c(0, 100), name = "") +
scale_x_discrete(labels=xlabels, breaks = xbreaks) +
opts(axis.text.x = theme_text(face='bold', size=12))
grid.text(label="POP1", x = 0.29, y = 0.06)
grid.text(label="POP2", x = 0.645, y = 0.06)
grid.gedit("GRID.text", gp=gpar(fontsize=8))

Different font faces and sizes within label text entries in ggplot2

Please try to tune a code upon according to your environment (e.g. the position of sub-axis labels and the fontsize).

Different font sizes for different portions of text label in ggplot

You can do this with the ggtext package currently under development.

# this requires the current development versions of ggplot2 and ggtext
# remotes::install_github("tidyverse/ggplot2")
# remotes::install_github("clauswilke/ggtext")

library(tidyverse)
library(ggtext)

df <- tibble(reference = seq(0, 100, by = 5)) %>%
mutate(
overall_not_adj = 1 + .08/(-25) * (reference - 25),
event_free_not_adj = 1 + .1/(-25) * (reference - 25),
overall_adj = 1 + .12/(-25) * (reference - 25),
event_free_adj = 1 + .14/(-25) * (reference - 25)
) %>%
pivot_longer(-reference, names_to = "lymph_node_yield", values_to = "hazard_ratio")

ggplot(df, aes(reference, hazard_ratio, color = lymph_node_yield)) +
geom_line() +
scale_color_manual(
name = "Lymph node yield",
breaks = c(
"overall_not_adj", "overall_adj",
"event_free_not_adj", "event_free_adj"
),
labels = c(
"Overall survival<br><span style='font-size:7pt'>Not adjusted to metastatic burden</span>",
"Overall survival<br><span style='font-size:7pt'>Adjusted to metastatic burden</span>",
"Event-free survival<br><span style='font-size:7pt'>Not adjusted to metastatic burden</span>",
"Event-free survival<br><span style='font-size:7pt'>Adjusted to metastatic burden</span>"
),
values = c(
overall_not_adj = "#0072B2", overall_adj = "#009E73",
event_free_not_adj = "#CC79A7", event_free_adj = "#E69F00"
),
guide = guide_legend(title.position = "top", ncol = 2)
) +
theme_classic() +
theme(
legend.position = "top",
legend.title.align = 0.5,
legend.text = element_markdown(),
legend.key.height = grid::unit(20, "pt")
)

Sample Image

Created on 2019-12-03 by the reprex package (v0.3.0)

Mix different font sizes/faces in plot caption

You can plot first caption (bold line) using caption argument and second line using tag argument in labs function. Next you have to manually specify tag position using plot.tag.position.

library(ggplot2)
ggplot(mtcars, aes(cyl, mpg)) +
geom_point() +
labs(caption = "Source: BLS Quarterly Census of Employment and Wages",
tag = "Produced By: @NVlabormarket") +
theme(plot.caption = element_text(vjust = 4, size = 9, face = "bold"),
plot.tag = element_text(size = 9),
plot.tag.position = c(0.89, 0))

Sample Image

Change relative font size for chart label in ggplot 2 R

The actual size of the numbers that are printed is controlled by scale_size_continuous. The scale takes the argument range, which defines the sizes to be used for the smallest and largest object. By default, range = c(1,6). You can play with the two numbers until you get the desired result.

Default values:

ggplot(mtcars, aes(carb)) +
stat_count(aes(y = ..count..),
show.legend = F, geom = "text") +
element_text(aes(size = ..count.., label = ..count..)) +
scale_size_continuous(range = c(1, 6))

Sample Image

Enlarge small numbers, but keep maximum size the same:

ggplot(mtcars, aes(carb)) +
stat_count(aes(y = ..count..),
show.legend = F, geom = "text") +
element_text(aes(size = ..count.., label = ..count..)) +
scale_size_continuous(range = c(3, 6))

Sample Image

Enlarge all numbers:

ggplot(mtcars, aes(carb)) +
stat_count(aes(y = ..count..),
show.legend = F, geom = "text") +
element_text(aes(size = ..count.., label = ..count..)) +
scale_size_continuous(range = c(4, 12))

Sample Image

Ggplot2: manually assign the different sizes of the text and thickness of the line simultaneously?

Try with this using ggnewscale package and the function new_scale() where you define the geom you want to add, in this case size:

library(ggnewscale)
library(ggplot2)
#Inputs
lines.size = c(2, 0.7)
names(lines.size) = c("A", "B")
label.size = c(5, 2)
names(label.size) = c("A", "B")
#Plot
ggplot(df, aes(x = A, y = B, group = C, color = C)) +
geom_line(aes(size = C)) +
scale_size_manual(values = lines.size)+
new_scale("size")+
geom_text(aes(y = B + 0.1, label = round(B, 1), size = C)) +
scale_size_manual(values = label.size)

Output:

Sample Image

How to alternate between plain and solid text font within same label text

You can use ggtext for markdown style text in ggplot2, see the answer here. You need to add legend.text = ggtext::element_markdown() to your theme and then can use **text** for bold:

ggplot(p, aes(x=value, y=os.neck, color=name_new, fill=name_new)) + 

theme(axis.text.x = element_text(size=12, hjust=0),
legend.text = ggtext::element_markdown()) +

geom_point(size=2.2, shape=21, stroke=1, fill=alpha("white", .7)) +
geom_quantile(quantiles=.5, size=1.3) +
scale_color_manual(values = c("#2C77BF", "#E38072", "#E1B930"),
name="",
labels=c("**Lymph nodal yield**\nUICC Stage I and II\nn=292", "**Lymph nodal yield**\nUICC Stage III and IV\nn=138","**Lymph node density**\nUICC Stage III and IV\nn=138")) +
scale_fill_manual(values = c("#2C77BF", "#E38072","#E1B930"),
name="",
labels=c("**Lymph nodal yield**\nUICC Stage I and II\nn=292", "**Lymph nodal yield**\nUICC Stage III and IV\nn=138","**Lymph node density**\nUICC Stage III and IV\nn=138")) +
scale_x_continuous(breaks = seq(0,80,10),
name="") +
scale_y_continuous(breaks = seq(0,180,20),
name="Months to death")

Sample Image

Edit

To use linebreaks in markdown, use <br> instead of \n.



Related Topics



Leave a reply



Submit