如何在ggplot的每个轴标签内设置两种不同的字体大小? [英] How to set two different font sizes within each axis label in ggplot?

查看:167
本文介绍了如何在ggplot的每个轴标签内设置两种不同的字体大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找具有两种不同字体大小的x轴标签.我试过使用表达式使换行符变小后的文本,但无法成功.我注意到,除了使用表达式之外,是否还有其他方法可以做到这一点.

I am looking to have the x-axis labels with two different font sizes. I tried using the expression to make the text after the line breaks smaller, but couldn't succeed. I am note sure if there are alternative ways to do this apart from using expression.

数据是

cat                 stdev   mean_old    mean_new    N       star
CatA<100            0.0214  1.5954398   1.4514398   502010  0.01**
100<=CatB<500       0.0171  1.3915428   1.2825428   3012004 0.01***
500<=CatC<1000      0.014   1.1224943   1.0731943   120412  0.09**
1000<=CatD<5000     0.0187  0.8035697   0.7713697   3010405 0.05**
5000<=CatE<10000    0.00945 0.3998517   0.3814517   1205512 0.01**
10000<=CatF<50000   0.0106  0.2333042   0.2076042   1051250 0.01**

代码是:

if (!require("pacman")) install.packages("pacman")
pacman::p_load(ggplot2, extrafont, scales)
font_install("fontcm")
loadfonts()

Raw_data<-Book4

Raw_data$cat_np <- factor(Raw_data$cat, levels = c("CatA<100", "100<=CatB<500", "500<=CatC<1000", "1000<=CatD<5000", "5000<=CatE<10000",  "10000<=CatF<50000"))

Raw_data$cat_temp <- paste(Raw_data$cat_np, comma(Raw_data$N), sep="\nexpression([N=")

Raw_data$cat <- paste0(Raw_data$cat_temp, "])")
cols    <- c( "c1" = "#000000", "c2" = "#ce1256" )
shapes  <- c("s1" = 95, "s2" = 95)


Plot1<-ggplot(data=Raw_data)+
  geom_point(aes(x=fct_inorder(cat),y=mean_old, colour = "c1" ,  shape="s1"), stat="identity" , size=10)+
  geom_point(aes(x=fct_inorder(cat),y=mean_new, colour = "c2",  shape="s2"), stat="identity" , size=10)+
  geom_errorbar(aes(ymin = mean_new - stdev, ymax = mean_new + stdev, x=cat), alpha = 0.5, size=0.4, width=0.1)+
  #geom_text(aes(x=cat, y=p_mean, label=p_coefficient_star), vjust=-1, color='black', size=2)+
  geom_text(aes(x=fct_inorder(cat), y=0.001, label=star), position=position_dodge(width=1), color='black', size=2.5)+
  theme(panel.background = element_blank(),
        axis.text.x=element_text(size=8, color="black", angle=45, vjust=0.5),
        axis.text.y=element_text(size=8, color="black"),
        axis.title.y=element_text(size=8),
        axis.title.x=element_text(size=8),
        plot.title=element_text(size=8),
        panel.border = element_rect(fill = NA, colour = "black"),
        strip.background = element_rect(fill="snow1"),
        panel.grid.major.y = element_line(color = "gray80", size = 0.2),
        legend.title=element_text(size=5),
        legend.justification = c(0.5, 0.5), 
        legend.text=element_text(size=5),
        legend.position = "right",
        #legend.position      = c(0.95, 0.95),
        axis.ticks = element_line(size = 0.1))+
  scale_y_continuous(breaks=c(0.25,0.5,0.75,1.0,1.25,1.5,1.75))+
  scale_x_discrete(labels=Raw_data$cat)+
  ylab("Means")+
  xlab("Categories")+
  scale_color_manual(name = "Mean", 
                     breaks = c("c1", "c2"), 
                     values = cols,
                     labels = c("Before", "After"))+
  scale_shape_manual(name = "Mean", 
                     breaks = c("s1", "s2"),
                     values = shapes,
                     labels = c("Before", "After"))+
  guides(shape = guide_legend(override.aes = list(size = 5)))
Plot1


pdf(file.path(OutputPath, "output.pdf"),  family="CM Roman", width=4, height=6)
Plot1
dev.off()

输出:

感谢您的帮助.谢谢

推荐答案

一种解决方法:使用 plotmath scriptstyle()绘制小尺寸的第二行标签

A workaround: use scriptstyle() of plotmath to draw label of line two in small size

library(forcats)
if (!require("pacman")) install.packages("pacman")
pacman::p_load(ggplot2, extrafont, scales)
font_install("fontcm")
loadfonts()

cols    <- c( "c1" = "#000000", "c2" = "#ce1256" )
shapes  <- c("s1" = 95, "s2" = 95)


x_labels <- purrr::map2(Raw_data$cat, paste0("[N=", Raw_data$N, "]"), 
  ~ bquote(atop(.(.x), scriptstyle(.(.y))))
)
x_labels <- purrr::invoke(expression, x_labels)



Plot1<-ggplot(data=Raw_data)+
  geom_point(aes(x=fct_inorder(cat),y=mean_old, colour = "c1" ,  shape="s1"), 
stat="identity" , size=10)+
  geom_point(aes(x=fct_inorder(cat),y=mean_new, colour = "c2",  shape="s2"), 
stat="identity" , size=10)+
  geom_errorbar(aes(ymin = mean_new - stdev, ymax = mean_new + stdev, x=cat), 
alpha = 0.5, size=0.4, width=0.1)+
  geom_text(aes(x=fct_inorder(cat), y=0.001, label=star), position=position_dodge(width=1), color='black', size=2.5)+
  theme(panel.background = element_blank(),
    axis.text.x=element_text(size=8, color="black", angle=45, vjust=0.5),
    axis.text.y=element_text(size=8, color="black"),
    axis.title.y=element_text(size=8),
    axis.title.x=element_text(size=8),
    plot.title=element_text(size=8),
    panel.border = element_rect(fill = NA, colour = "black"),
    strip.background = element_rect(fill="snow1"),
    panel.grid.major.y = element_line(color = "gray80", size = 0.2),
    legend.title=element_text(size=5),
    legend.justification = c(0.5, 0.5), 
    legend.text=element_text(size=5),
    legend.position = "right",
    #legend.position      = c(0.95, 0.95),
    axis.ticks = element_line(size = 0.1))+
  scale_y_continuous(breaks=c(0.25,0.5,0.75,1.0,1.25,1.5,1.75))+
  scale_x_discrete(labels=x_labels)+
  ylab("Means")+
  xlab("Categories")+
  scale_color_manual(name = "Mean", 
    breaks = c("c1", "c2"), 
    values = cols,
    labels = c("Before", "After"))+
  scale_shape_manual(name = "Mean", 
    breaks = c("s1", "s2"),
    values = shapes,
    labels = c("Before", "After"))+
  guides(shape = guide_legend(override.aes = list(size = 5)))

Plot1

希望这会有所帮助.

这篇关于如何在ggplot的每个轴标签内设置两种不同的字体大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆