如何在ggplot facet wrap标签中使用不同的字体大小? [英] How to use different font sizes in ggplot facet wrap labels?

查看:1736
本文介绍了如何在ggplot facet wrap标签中使用不同的字体大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



例如:

我想在我的facet wrap的标签中创建两种不同大小的文字。

  • 种类X(大小14)

  • 总捕获量(n = 133)(大小12)


      test<  -  read.csv(paste0(path,Costello Artvgl2 for Stack.csv),sep =;,dec =,,header = T)

    str(test)


    test $ Wert < - factor(test $ Wert,levels = c(one,two ,三,四,五,六))


    test $ Sampling.site < - factor(test $ Sampling.site,levels = c 种X区T,种Y区T,种X区A,种Y区B,种X区B,种Y区C))
    $ b $ (物种X =总捕获量(n = 133),物种Y =总捕获量(n = 185),种类X = \\ n取样区域A(n = 57),物种Y \取样区域B(n = 12 2),
    物种X \采样区域B(n = 76),物种Y \采样区域C(n = 63))

    theme_new <函数(base_size = 12,base_family = base_family){
    theme_bw(base_size = base_size)%+替换%
    主题(
    axis.text.x = element_text(size = 8),
    axis.text.y = element_text(size = 8),
    axis.title.x = element_text(size = 12,vjust = 0.01),
    axis.title.y = element_text(size = 12,vjust = 0.9,angle = 90),

    plot.title = element_text(size = 10,face =bold),

    legend.key = element_rect color = NA,fill = NA,size = 0.5),
    legend.key.size = unit(1,lines),
    legend.text = element_text(size = 8),
    legend.title = element_blank(),

    strip.background = element_rect(fill = NA,color = NA),
    strip.text = element_text(size = 8,face =bold ,hjust = 0.5,vjust = 0.9),

    panel.backgroun d = element_rect(fill =white),
    panel.border = element_rect(fill = NA,color =black),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.margin = unit(1,lines)


    }

    ggplot(测试,aes(Fi,Pi),group = Wert)+
    geom_point(aes(color = factor(wert),shape = factor(wert)),size = 3)+
    scale_shape_manual(values = c(20,18,19,15,16,17))+
    scale_x_continuous(限制= c(0,1),break = c(0,0.2,0.4,0.6,0.8,1.0))+
    scale_colour_brewer(type =qual,palette =Paired)+
    scale_y_continuous(limits = c(0,100),breaks = c(0,20,40,60,80,100))+
    labs(x =发生频率,y =猎物特异性丰度[%])+
    facet_wrap(〜Sampling.site,scales =free,ncol = 2)+
    theme_new()

    有没有可能意识到这一点?

    解决方案

    下面的解决方案是一个黑客,因为它使用上标(或下标)为facet标签的第二行获取更小的字体大小。我不确定如何更好地控制标签大小,而不是直接操纵条形码库,尽管可能有一种方法可以编写 labeller 函数来执行它。



    例如,我们将使用内置的 mtcars 数据框。首先,我们将添加将用于刻面和标签的列。我们将面向 cyl ,但我们希望标签的第一行显示该面的柱面数,第二行显示数据点的数量那方面。为此,我们将在 mtcars 中创建两个新列,分别称为 Label1 Label2 我们将用它来创建facet标签的每一行。我们将通过这两列进行分析,以获取图中我们想要的标签。 ( Label3 类似于 Label2 ,但使用下标而不是上标;如果要更改距离从第二行的底部到情节面板的顶部。)



    Label1 Label2 是文本字符串,但它们是表达式的形式,因此我们可以在创建时使用 label_parsed 来获取更小的下标文本

     库(ggplot2)
    库(dplyr)
    库(网格)

    mtcars.new = mtcars%>%group_by(cyl)%>%
    summary(Label1 = paste0(bold(Cylinders:〜,unique(cyl),)),
    Label2 = paste0(bold(NULL [Count:〜,length(cyl),])),
    Label3 = paste0(bold(NULL ^ {Count:〜,length ))%}%
    full_join(mtcars)

    现在我们可以创建该情节。通过 Label1 Label2 为我们提供了两条线。因为我们为 Label2 创建了一个下标表达式,所以当我们使用 label_parsed 标记facet时,它会以更小的字体呈现。相对于 Label1 ,我更喜欢 Label2 有点大,但是没有办法用这个(哈克)方法。另外,尽管 element_text 有一个 lineheight 参数,但ggplot似乎并不尊重它。因此,我已手动重置条形标签的lineheight以减少两个标签之间的空间。

      p = ggplot(mtcars.new,aes(wt,mpg))+ 
    geom_point()+
    facet_grid(。〜Label1 + Label2,labeller = label_parsed)+
    theme_bw()+
    主题(strip.background = element_rect(fill = NA,color = NA),
    strip。 text = element_text(size = 12))

    g < - ggplotGrob(p)
    g $ heights [[3]] =单位(0.5,lines)

    grid.draw(g)


    I want to create two different sizes of text in the labels of my facet wrap.

    For example:

    • Species X (size 14)
    • Total catch (n=133) (size 12)

    test <- read.csv(paste0(path, "Costello Artvgl2 for Stack.csv"), sep = ";", dec = ",", header = T)
    
    str(test)
    
    
    test$Wert <- factor(test$Wert, levels = c("one","two","three","four","five","six")) 
    
    
    test$Sampling.site <- factor(test$Sampling.site, levels = c("Species X Area T","Species Y Area T","Species X Area A","Species Y Area B","Species X Area B","Species Y Area C"))
    
    
    levels(test$Sampling.site) <-  c("Species X\nTotal catch (n=133)", "Species Y\nTotal catch (n=185)", "Species X\nSampling area A (n=57)", "Species Y\nSampling area B (n=122)",
                                 "Species X\nSampling area B (n=76)",  "Species Y\nSampling area C (n=63)")
    
    theme_new <- function(base_size = 12, base_family = base_family){
    theme_bw(base_size = base_size) %+replace%
    theme(
      axis.text.x =       element_text(size = 8 ),
      axis.text.y =       element_text(size = 8 ),
      axis.title.x =        element_text(size = 12, vjust = 0.01),
      axis.title.y =        element_text(size = 12, vjust = 0.9, angle = 90),
    
      plot.title =        element_text(size = 10, face = "bold"),
    
      legend.key=         element_rect(colour= NA, fill =NA, size = 0.5),
      legend.key.size =   unit(1, "lines"),
      legend.text =       element_text(size = 8),
      legend.title =      element_blank(),
    
      strip.background =  element_rect(fill = NA, colour = NA), 
      strip.text =        element_text(size = 8, face = "bold",hjust = 0.5, vjust = 0.9),
    
      panel.background =  element_rect(fill = "white"), 
      panel.border =      element_rect(fill = NA, colour="black"), 
      panel.grid.major =  element_blank(),
      panel.grid.minor =  element_blank(),
      panel.margin =      unit(1, "lines")
    
    )
    }
    
    ggplot(test, aes(Fi, Pi),group=Wert)+
    geom_point(aes(colour = factor(Wert),shape = factor(Wert)),size=3)      +                         
      scale_shape_manual(values=c(20,18,19,15,16,17))+                    
      scale_x_continuous(limits=c(0, 1),breaks=c(0,0.2,0.4,0.6,0.8,1.0))+     
      scale_colour_brewer(type = "qual", palette = "Paired")+
      scale_y_continuous(limits=c(0, 100),breaks=c(0,20,40,60,80,100))+        
      labs(x = "Frequency of occurrence", y = "Prey-specific abundance [%]")+ 
      facet_wrap(~Sampling.site,scales = "free",ncol = 2) +
      theme_new()
    

    Is there a possibility to realize that?

    解决方案

    The solution below is a hack in that it uses a superscript (or subscript) to get a smaller font size for the second line of the facet label. I'm not sure how to get finer control of the label size without resorting to direct manipulation of the strip grobs, though there might be a way to write a labeller function to do it.

    We'll use the built-in mtcars data frame for the example. First, we'll add columns that will be used both for facetting and labeling. We'll facet by cyl, but we want the first line of the label to display the number of cylinders for that facet and the second line to display the number of data points in that facet. To do this, we'll create two new columns in mtcars, called Label1 and Label2 that we'll use to create each line of the facet label. We'll facet by these two columns to get the labels we want in the graph. (Label3 is similar to Label2, but uses subscript instead of superscript; this only matters if you want to change the distance from the bottom of the second line to the top of the plot panel.)

    Label1 and Label2 are text strings, but they're in the form of expressions so that we can use label_parsed to get smaller subscripted text when we create the plot.

    library(ggplot2)
    library(dplyr)
    library(grid)
    
    mtcars.new = mtcars %>% group_by(cyl) %>% 
      summarise(Label1=paste0("bold(Cylinders:~", unique(cyl),")"),
                Label2=paste0("bold(NULL[Count:~", length(cyl),"])"),
                Label3=paste0("bold(NULL^{Count:~", length(cyl),"})")) %>%
      full_join(mtcars)
    

    Now we can create the plot. Facetting by Label1 and Label2 gives us two lines. Because we created a subscripted expression for Label2, this gets rendered in a smaller font when we use label_parsed to label the facets. I'd prefer the Label2 to be a bit larger, relative to Label1, but there's no way to control that with this (hacky) method. Also, although element_text has a lineheight argument, ggplot doesn't seem to be respecting it. As a result, I've manually reset the lineheight for the strip labels to reduce the space between the two labels.

    p = ggplot(mtcars.new, aes(wt, mpg)) +
      geom_point() +
      facet_grid(. ~ Label1 + Label2, labeller=label_parsed) +
      theme_bw() +
      theme(strip.background=element_rect(fill=NA, color=NA),
            strip.text=element_text(size=12))
    
    g <- ggplotGrob(p)
    g$heights[[3]] = unit(0.5,"lines")
    
    grid.draw(g)
    

    这篇关于如何在ggplot facet wrap标签中使用不同的字体大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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