3

我有一个热图,其中一个标签是用“,”分隔的两个单词的刺痛。我想将标签的后缀斜体,同时保持前缀字体不变。我意识到有一些问题解决了类似的问题,如果这是一个重复的问题,我深表歉意,但我无法将这些问题的任何解决方案应用于我的特定问题。

我有以下代码生成我的情节:

ggplot(mockdata, aes(variable, Measurement)) + 
    geom_tile(aes(fill = mockdata$plotval), colour = "dark red")  + facet_grid(category~type, scales='free', space='free') + scale_fill_gradient2(limits=c(-20, 20),high = "firebrick3", low = "dodgerblue4") + 
    theme_minimal() + theme(axis.text.x=element_text(size=28, angle=90), axis.text.y=element_text(size=28)) + 
    labs(title="", x="", y="", fill="") + theme(strip.text.x=element_blank(),strip.text.y=element_text(size=20, angle=0))

在此处输入图像描述

我希望前缀(这里是数字 23:42 但实际上是一个字符串)保持不变,同时以斜体设置后缀。我怎么能做到这一点?(我应该注意,我的数据框中也有只有前缀或只有后缀的列,所以像 expression(paste(column1, italics(column2), sep="")) 之类的东西可以工作,虽然它对我没有用所以远的)。

这是可重现的数据:

dput(mockdata)
structure(list(Measurement = structure(c(42L, 41L, 40L, 39L, 
38L, 37L, 36L, 35L, 34L, 33L), .Label = c("1, italic_suffix", 
"2, italic_suffix", "3, italic_suffix", "4, italic_suffix", "5, italic_suffix", 
"6, italic_suffix", "7, italic_suffix", "8, italic_suffix", "9, italic_suffix", 
"10, italic_suffix", "11, italic_suffix", "12, italic_suffix", 
"13, italic_suffix", "14, italic_suffix", "15, italic_suffix", 
"16, italic_suffix", "17, italic_suffix", "18, italic_suffix", 
"19, italic_suffix", "20, italic_suffix", "21, italic_suffix", 
"22, italic_suffix", "23, italic_suffix", "24, italic_suffix", 
"25, italic_suffix", "26, italic_suffix", "27, italic_suffix", 
"28, italic_suffix", "29, italic_suffix", "30, italic_suffix", 
"31, italic_suffix", "32, italic_suffix", "33, italic_suffix", 
"34, italic_suffix", "35, italic_suffix", "36, italic_suffix", 
"37, italic_suffix", "38, italic_suffix", "39, italic_suffix", 
"40, italic_suffix", "41, italic_suffix", "42, italic_suffix"
), class = c("ordered", "factor")), category = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L), .Label = c("x1", "x2", "x3", 
"x4", "x5", "x6", "x7", "x8", "x9"), class = "factor"), variable = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("A", "B", "C", 
"a", "b", "c", "d", "e", "f"), class = "factor"), Pval = c(2.47889266743109e-11, 
3.57296480818891e-12, 2.95428165629922e-21, 6.55646318564946e-12, 
0.00140846156326513, 0.00504059407383829, 0.239272686561618, 
0.146388841964746, 0.0193041667726786, 34.6978854862654), effect = c(1.40367296123877, 
0.267565311381035, 0.157909806505032, 0.117848801449174, 0.139575361152878, 
0.153551445281832, 0.254959981281264, 0.0635385821595322, 0.0832431045850506, 
0.010863571087271), direction = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 
1), plotval = c(10.6057422771658, 11.4469712613471, 20.5295481021367, 
11.1833303729987, 2.85125500035703, 2.29751827550182, 0.62110687410944, 
0.83449202476544, 1.71434893912345, -1.54030300938373), type = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Individual", 
"Composite"), class = "factor"), `NA` = list(c("rs188468174", 
" RUNX3"), c("rs35668054", " CYP26B1"), c("rs968567", " FADS2"
), c("rs9276244", " HLA-DQA2"), c("rs10065637", " ANKRD55"), 
    c("rs1071888", " ASCC2"), c("rs142973694", " MICA"), c("*rs3815768", 
    " ELL2"), c("rs3184504", " SH2B3"), c("rs2926468", " FCGR3B"
    )), `NA` = c(" RUNX3", " CYP26B1", " FADS2", " HLA-DQA2", 
" ANKRD55", " ASCC2", " MICA", " ELL2", " SH2B3", " FCGR3B")), .Names = c("Measurement", 
"category", "variable", "Pval", "effect", "direction", "plotval", 
"type", NA, NA), row.names = c(NA, 10L), class = "data.frame")
4

1 回答 1

1

下面介绍如何使用自定义标签。想法来自Haboryme 链接的问题。请注意,我将代码最小化了一点,因为theme这不是演示目的所必需的。

library(ggplot2)
labs <- sapply(strsplit(as.character(mockdata$Measurement), ", "), 
               FUN = function(x) {
                 x1 <- x[1]; x2 <- x[2]
                 parse(text = paste("plain('", x1, ",') ~ ", "italic('", x2, "')", sep = ""))
               })

ggplot(mockdata, aes(variable, Measurement)) + 
  geom_tile(aes(fill = mockdata$plotval), colour = "dark red")  + 
  facet_grid(category~type, scales='free', space='free') + 
  scale_fill_gradient2(limits=c(-20, 20),high = "firebrick3", low = "dodgerblue4") + 
  theme_minimal() + 
  scale_y_discrete(labels = labs, breaks = mockdata$Measurement) +
  labs(title="", x="", y="", fill="")

在此处输入图像描述

于 2016-11-08T15:23:20.717 回答