在R中用斜体标记后缀

时间:2016-11-08 13:07:43

标签: r ggplot2 substring italic

我有一个热图,其中一个标签是用“,”分隔的两个单词的刺。我想保留标签的后缀,同时保留前缀的未更改字体。我意识到有一些问题可以解决类似的问题,如果这是一个重复的问题我会道歉但我无法将这些问题的任何解决方案应用于我的特定问题。

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

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

enter image description here

我希望前缀(这里是一个数字23:42,但实际上是一个字符串)保持不变,同时以斜体设置后缀。我怎么能做到这一点? (我应该注意,我的数据框中只有前缀或后缀的列,所以像表达式(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")

1 个答案:

答案 0 :(得分:1)

以下是如何使用自定义标签的方法。由Haboryme联系的想法from question。请注意,由于演示目的不需要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="")

enter image description here