2

Question: is it possible to have both plain and solid text font within the same label text specified in scale_fill_manual and scale_color_manual?

I have

enter image description here

Written with

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

  theme(axis.text.x = element_text(size=12, hjust=0)) +

  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")

I would like the Lymph nodal yield and Lymph node density to appear bold in the text generated from scale_fill/color_manual(labels=()), whereas the remaining text should stay plain

Expected output:

enter image description here

My data p

p <- structure(list(os.neck = c(11.5, 11.5, 74.38, 74.38, 17.02, 7.89, 
96.03, 40.48, 17.74, 14.65, 14.65, 62.46, 12.55, 9.92, 26.05, 
45.47, 45.47, 17.38, 39.72, 51.45, 8.61, 8.61, 76.98, 67.09, 
94.79, 72.15, 93.93, 17.05, 12.48, 12.48, 91.6, 15.87, 15.87, 
11.04, 67.22, 67.02, 8.94, 8.94, 6.6, 6.6, 5.09, 10.68, 10.68, 
17.15, 17.15, 0.07, 5.19, 5.19, 40.77, 0.2, 0.2, 170.88, 5.55, 
5.55, 1.61, 1.61, 38.28, 38.28, 10.58, 10.58, 32.99, 32.99, 110.98, 
103.69, 122.32, 14.78, 42.74, 4.04, 4.04, 8.28, 84.96, 11.7, 
11.7, 49.97, 49.97, 120.48, 52.6, 52.6, 71.26, 16.3, 100.14, 
55.03, 6.51, 6.51, 89.89, 51.71, 51.71, 24.97, 55.66, 21.91, 
81.48, 30.92, 1.58, 1.58, 7.52, 7.52, 30.75, 30.75, 3.45, 19.22
), value = c(22.2, 18, 9.1, 11, 14, 15, 9, 6, 3, 25, 16, 4, 6, 
10, 13, 12.1, 33, 16, 6, 9, 4.3, 23, 9, 8, 13, 5, 30, 25, 33.3, 
3, 9, 33.3, 9, 12, 7, 38, 60, 5, 71.4, 7, 15, 25, 4, 16.7, 6, 
15, 11.1, 9, 8, 42.9, 7, 4, 16.7, 6, 10, 10, 62.5, 8, 25, 4, 
11.1, 9, 10, 14, 14, 3, 4, 50, 6, 6, 20, 33.3, 3, 7.7, 26, 13, 
7.7, 13, 13, 13, 3, 7, 16.7, 6, 5, 20, 10, 15, 29, 7, 6, 11, 
11.8, 17, 71.4, 14, 33.3, 18, 22, 9), name_new = structure(c(3L, 
2L, 3L, 2L, 1L, 1L, 1L, 1L, 1L, 3L, 2L, 1L, 1L, 1L, 1L, 3L, 2L, 
1L, 1L, 1L, 3L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 2L, 1L, 3L, 2L, 
1L, 1L, 1L, 3L, 2L, 3L, 2L, 1L, 3L, 2L, 3L, 2L, 1L, 3L, 2L, 1L, 
3L, 2L, 1L, 3L, 2L, 3L, 2L, 3L, 2L, 3L, 2L, 3L, 2L, 1L, 1L, 1L, 
1L, 1L, 3L, 2L, 1L, 1L, 3L, 2L, 3L, 2L, 1L, 3L, 2L, 1L, 1L, 1L, 
1L, 3L, 2L, 1L, 3L, 2L, 1L, 1L, 1L, 1L, 1L, 3L, 2L, 3L, 2L, 3L, 
2L, 1L, 1L), .Label = c("n_fjernet0", "n_fjernet1", "lnd1"), class = "factor")), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -100L))
cmirian
  • 2,572
  • 3
  • 19
  • 59

2 Answers2

6

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")

enter image description here

Edit

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

starja
  • 9,887
  • 1
  • 13
  • 28
2

Labels in ggplot also allow mathematical annotation via expressions

You can pick the following features to answer your question :
- paste
- bold
- atop

expression(paste(atop(bold("Lymph nodal yield"),"UICC Stage I and II\nn=292")))
ggplot(p, aes(x=value, y=os.neck, color=name_new, fill=name_new)) + 

  theme(axis.text.x = element_text(size=12, hjust=0)) +

  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(expression(paste(atop(bold("Lymph nodal yield"),"UICC Stage I and II\nn=292"))), expression(paste(atop(bold("Lymph nodal yield"),"nUICC Stage III and IV\nn=138"))),expression(paste(atop(bold("Lymph node density"),"\nUICC Stage III and IV\nn=138"))))) + 
  scale_fill_manual(values = c("#2C77BF", "#E38072","#E1B930"),
                    name="") +
  scale_x_continuous(breaks = seq(0,80,10),
                     name="") +
  scale_y_continuous(breaks = seq(0,180,20),
                     name="Months to death")
Waldi
  • 39,242
  • 6
  • 30
  • 78