如何在ggplot中的每个轴标签中设置两种不同的字体大小?

时间:2018-01-30 02:16:35

标签: r ggplot2 figure

我希望x轴标签有两种不同的字体。我尝试使用表达式在换行后使文本变小,但无法成功。我注意到除了使用表达式之外还有其他方法可以做到这一点。

数据是

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

输出:

output.png

感谢任何帮助。感谢

1 个答案:

答案 0 :(得分:0)

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

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

希望这有帮助。