ggplot中文本标签的不同部分的字体大小不同

时间:2019-12-03 09:31:28

标签: r ggplot2

提供了一个ubuntu,请滚动到底部

我遵循了这些instructions,但是我无法使其与脚本一起使用。他们似乎使用的是labs(),而不是我下面使用的方法。

我制作了这个情节:

enter image description here

但是,我希望文本的不同部分具有不同的字体大小。我在Photoshop中编辑了以下内容,以描述我要寻找的结果。

enter image description here

我使用了以下脚本:

Data sample

数据样本

ggplot(as.data.frame(out), aes(x = n.fjernet)) + theme +
  geom_ribbon(aes(fill = model, ymin = lower, ymax = upper), alpha = .1) +
  geom_line(aes(y = yhat, col = model),size=1) +

  ggtitle("Lymph node yield") + 

  geom_segment(aes(x = 0, y = 1, xend = 100, yend = 1), lty="dashed", size=0.5) +
  geom_segment(aes(x = 25, y = 1, xend = 25, yend = 0.5), lty="dashed", size=0.5, col="black") +

  geom_point(mapping = aes(x = 25, y = 1), size=2, shape=16, col="black", alpha=0.5) +

  scale_fill_manual(values = c("#DAE5F2", "#F9E7E5","#E4F2F3","#FAF1D9"), name = "",
                    labels = c("Overall survival\nNot adjusted to metastatic burden", "Event-free survival\nNot adjusted to metastatic burden","Overall survival\nAdjusted to metastatic burden", "Event-free survival\nAdjusted to metastatic burden")) +
  scale_colour_manual(values = c("#2C77BF", "#E38072","#6DBCC3","#E1B930"), name = "",
                      labels = c("Overall survival\nNot adjusted to metastatic burden", "Event-free survival\nNot adjusted to metastatic burden","Overall survival\nAdjusted to metastatic burden", "Event-free survival\nAdjusted to metastatic burden")) +

  scale_x_continuous(name="", breaks=seq(0,100,by=25), limits=c(0,100), label=c("0","25\nas reference","50", "75", "100")) +
  scale_y_continuous(name="Hazard ratio", breaks = seq(0.5,1.2,by=.1)) +coord_cartesian(ylim=c(0.5,1.25)) +

  theme(axis.text.x = element_text(color = "grey20", size =11), 
        axis.title.x = element_text(color = "grey20", size = 14, face="bold", margin=ggplot2::margin(t=12)),
        axis.text.y = element_text(color = "grey20", size = 11), 
        axis.title.y = element_text(color = "grey20", size = 14, face="bold", margin=ggplot2::margin(r=12)),
        legend.key = element_rect(fill = "white"),
        plot.title = element_text(color = "grey20", size = 18,face="bold",hjust = 0.5),
        plot.subtitle = element_text(hjust = 0.5),
        legend.text=element_text(size=12), legend.title=element_text(size=14), legend.position="top") +
        guides(fill=guide_legend(nrow=2,byrow=TRUE))

1 个答案:

答案 0 :(得分:2)

您可以使用当前正在开发的ggtext软件包进行此操作。

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

reprex package(v0.3.0)于2019-12-03创建