2016-01-21 71 views
1

我正在尋找一種方法在ggplot2中獲得真正的減號。ggplot2中X軸可變標籤上的真減號

生成數據:

score <- c(rnorm(8, 20, 3), rnorm(8, 35, 5)) 
gene <- factor((rep(c(1,2), each = 8))) 
df <- data.frame(gene, score) 

現在有包括減號

require(plyr) 

df$gene <- mapvalues(df$gene, from = c(1,2), to = c("Gene -", "Gene +")) 

現在圖表數據幀中GGPLOT2

cellMeansDF <- aggregate(score ~ gene, df, mean) 

require(ggplot2) 

plot1 <- ggplot(cellMeansDF, aes(gene, score)) + 
     geom_bar(stat = "identity", position = "dodge", size = 0.1) + 
     xlab("") + 
     theme(axis.text.x = element_text(face = "plain", color = "black", size = 15)) 

plot1 

現在注意減號名字命名的因素不夠寬。這真是一個破折號。我如何讓ggplot2顯示一個真正的減號?即使是短跑也可以。

回答

3

您可以使用Unicode 破折號字符,而不是一個連字符:

"Gene \u2013" 

其中給出:

"Gene –" 

而標準連字符看起來是這樣的:

"Gene -" 

因此,將連字符更改爲代碼中的短劃線:

df$gene <- mapvalues(df$gene, from = c(1,2), to = c("Gene \u2013", "Gene +")) 

得到以下情節:

enter image description here

+0

是的。正是我需要的,非常整齊。 – llewmills

0

這個怎麼樣?您當然可以根據自己的喜好選擇或放棄粗體。

score <- c(rnorm(8, 20, 3), rnorm(8, 35, 5)) 
gene <- factor((rep(c(1,2), each = 8))) 
df <- data.frame(gene, score) 


require(plyr) 

df$gene <- mapvalues(df$gene, from = c(1,2), to = c("Gene -", "Gene +")) 

Now graph the dataframe in ggplot2 

cellMeansDF <- aggregate(score ~ gene, df, mean) 

require(ggplot2) 

plot1 <- ggplot(cellMeansDF, aes(gene, score)) + 
    geom_bar(stat = "identity", position = "dodge", size = 0.1) + 
    xlab("") + 
    theme(axis.text.x = element_text(family="mono", face="bold", color = "black", size = 15)) 

plot1 

Plot

你可以在這裏找到字體的完整列表: http://www.cookbook-r.com/Graphs/Fonts/

的字體和其他element_text選擇將決定這個字符顯示。這裏是你的字體選擇的圖形總結:

Fonts

+0

非常感謝@ Hack-R的字體類型的範例,但我不認爲在這種情況下更改字體會做到這一點。特別是因爲它會涉及橫座標標籤與其他標籤不同。 – llewmills