繁体   English   中英

如何使用ggplot2以斜体写部分X标签字符串?

[英]How to write partial string of X labels in italics using ggplot2?

我有一个数据框:

                        ID   Strain value Type      Gene
182 VFG007613(gi:27366370) Strain-1     0    X      motY
183 VFG007614(gi:37679367) Strain-1     1    X      motY
184 VFG007619(gi:27364700) Strain-1     0    X      motX
185 VFG007620(gi:37681249) Strain-1     1    X      motX
186 VFG007622(gi:27364235) Strain-1     0    X       wza
187 VFG007623(gi:37678521) Strain-1     1    X       wza
188 VFG007627(gi:37678523) Strain-1     1    X       wzb
189 VFG007629(gi:27364230) Strain-1     1    X       wzc
190 VFG007630(gi:37678524) Strain-1     0    X       wzc
191 VFG007640(gi:37678525) Strain-1     0    X wbjD/wecB
192 VFG007653(gi:27364224) Strain-1     1    X      wbfY
193 VFG007654(gi:37678548) Strain-1     0    X      wbfY
194 VFG007656(gi:27364223) Strain-1     0    X wbfV/wcvB
195 VFG007657(gi:37678549) Strain-1     0    X wbfV/wcvB
196 VFG007668(gi:27367928) Strain-1     1    X     GeneA
197 VFG007669(gi:37676055) Strain-1     0    X     GeneA

基因的名称就像 wbfY,最后一个字母总是大写,所以我想用斜体的 Gene 列绘制axis.text.x,除了大写,比如: wbf Y, wbj D/ wec B, mot Y, mot X, gene G, anyname A。可以使用一些函数来绘制除大写 notitalic(:upper:) 之外的所有斜体文本( genename ): genename D。

或者有什么办法吗?

ggplot(mydf, aes(x=Gene, y=Strain, fill = Type, alpha = value)) + 
    scale_fill_manual(values = c("#336666", "#006699")) +
    geom_tile(colour = "grey79") +
    scale_alpha_identity(guide = "none") +
    coord_fixed(ratio = 2.4) +
    theme_bw() +
    theme(text = element_text(family = "Times New Roman"), # corresponde a todo en Time New Romans 
          legend.position="bottom", 
          legend.title = element_text(size = 14), # legend size title, corresponde a Genotype
          legend.text  = element_text(size = 14), # corresponde a vcgC and vcgE
          panel.grid.major = element_blank(),
          panel.grid.minor = element_blank(),
          axis.text.x = element_text(angle = 90, size = 18, hjust =1, vjust = 0.5, face = "italic"),
          axis.title.x = element_text(size = 15, face = "bold"),
          axis.text.y = element_text(angle = 0, hjust = 1, size = 12, face = "bold"),
          axis.title.y = element_text(size = 15, face = "bold")
    )

在此处输入图像描述

在这种情况下,所有axis.text.x都是斜体,但我想避免大写!

我怎样才能做到这一点?

非常感谢

你可以做的是拆分基因是部分,并使用glue使用ggtext中的element_rmarkdown使最后一个字母斜体,如下所示:

library(tidyverse)
library(stringi)
library(glue)
library(ggtext)
mydf %>%
  mutate(Gene_last_letter = stri_sub(Gene, -1, -1),
         Gene_first_letters = str_sub(Gene, 1, str_length(Gene)-1)) %>%
  mutate(Gene = glue("<i>{Gene_first_letters}</i>{Gene_last_letter}")) %>%
  ggplot(aes(x=Gene, y=Strain, fill = Type, alpha = value)) + 
  scale_fill_manual(values = c("#336666", "#006699")) +
  geom_tile(colour = "grey79") +
  scale_alpha_identity(guide = "none") +
  coord_fixed(ratio = 2.4) +
  theme_bw() +
  theme(text = element_text(family = "Times New Roman"), # corresponde a todo en Time New Romans 
        legend.position="bottom", 
        legend.title = element_text(size = 14), # legend size title, corresponde a Genotype
        legend.text  = element_text(size = 14), # corresponde a vcgC and vcgE
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        axis.text.x =  element_markdown(angle = 90, size = 18, hjust =1, vjust = 0.5), 
        axis.title.x = element_text(size = 15, face = "bold"),
        axis.text.y = element_text(angle = 0, hjust = 1, size = 12, face = "bold"),
        axis.title.y = element_text(size = 15, face = "bold")
  )

输出:

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM