2015-09-27 43 views
4

這是問題:我使用ggplot來製作一個圖表,其中x軸的範圍是-90到90度。我成功地使用了一個函數來爲創建的刻度標籤by scale_x_continuous添加度數符號。但是,當我使用theme()調整字體面時,刻度標籤不會變爲粗體。但是,改變字體大小是可行的。這裏是一個重複的例子:ggplot scale_x_continuous with symbol:make bold

# function to add degree symbols 
add_degree <- function(x) { 
    parse(text = paste(x, "*degree",sep = "")) 
} 

y<- c(0.00552243346007605, 0.00553155893536122, 0.005693536121673, 
     0.0054722433460076, 0.00562661596958175, 0.00546768060836502, 
     0.00561292775665399, 0.00550950570342205, 0.0056851711026616, 
     0.00551558935361217, 0.0055041825095057, 0.00556501901140684, 
     0.00552699619771863, 0.00552623574144487, 0.0055680608365019, 
     0.00567148288973384, 0.00550342205323194, 0.00553764258555133, 
     0.00538098859315589, 0.00573307984790875, 0.00564486692015209, 
     0.00561444866920152, 0.00556349809885932, 0.00571254752851711, 
     0.00544030418250951, 0.00557946768060837, 0.00546083650190114, 
     0.00549049429657795, 0.00557414448669202, 0.00553916349809886, 
     0.00551787072243346, 0.00557794676806084, 0.0055041825095057, 
     0.00552699619771863, 0.0056509505703422, 0.00544790874524715, 
     0.00555555555555556) 

x <- seq(-90,90,by = 5) 
dat <- data.frame(x,y) 
ggplot(dat,aes(x = x, y = y))+ 
    geom_line()+ 
    scale_x_continuous(limits = c(-90,90), breaks = seq(-90,90, by = 15), labels = add_degree)+ 
    theme_bw() + 
    theme(

    # Adjust axis label and title font sizes below 
    axis.title = element_text(size=12, face= "bold"), 
    plot.title = element_text(size=12, face = "bold"), 
    # Adjust size of tick labels 
    axis.text.x = element_text(size=12, face = "bold"), 
    axis.text.y = element_text(size=12, face = "bold"), 
    panel.grid.major = element_blank(), 
    panel.grid.minor = element_blank(), 
    panel.border = element_blank(), 
    panel.background = element_blank(), 
    axis.line = element_line(colour = "black") 
) 

我試圖調整上述parse功能,但至今沒有運氣。我已經看了看下面的解決方案,但一直沒能得到它在ggplot工作:How to make beta italic and bold in axis label and P italic and bold in text

這裏是我的sessionInfo()輸出:

R version 3.2.2 (2015-08-14) 
Platform: x86_64-pc-linux-gnu (64-bit) 
Running under: Ubuntu 14.04.3 LTS 

locale: 
[1] LC_CTYPE=en_US.UTF-8  LC_NUMERIC=C    LC_TIME=en_US.UTF-8  LC_COLLATE=en_US.UTF-8  LC_MONETARY=en_US.UTF-8 
[6] LC_MESSAGES=en_US.UTF-8 LC_PAPER=en_US.UTF-8  LC_NAME=C     LC_ADDRESS=C    LC_TELEPHONE=C    
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C  

attached base packages: 
[1] stats  graphics grDevices utils  datasets methods base  

other attached packages: 
[1] reshape2_1.4.1 ggplot2_1.0.1 moments_0.14 

loaded via a namespace (and not attached): 
[1] Rcpp_0.12.0  digest_0.6.8  MASS_7.3-44  grid_3.2.2  plyr_1.8.3  gtable_0.1.2  magrittr_1.5  scales_0.2.5  stringi_0.5-5 
[10] proto_0.3-10  labeling_0.3  tools_3.2.2  stringr_1.0.0 munsell_0.4.2 colorspace_1.2-6 

附:我相信這是一個非常類似(如果不是相同的)問題,以下未回答的問題:making y-axis labels bold in ggplot (x-axis is set bold but y-axis label doesn't change)

任何幫助將不勝感激!

+0

你'add_degree'功能輸出打破了大膽的。雖然我找不出解決方案。 – Jason

回答

3

我讀了評論here即提到,標籤取一個字符值。你的函數的輸出是一個表達式。更改add_degree()下面的輸出字符值,而不是一種表達,似乎爲我工作:

add_degree <- function(x) { 
    paste(x, "º",sep = "") 
} 

這裏是我的sessionInfo()

R version 3.0.3 (2014-03-06) 
Platform: x86_64-apple-darwin10.8.0 (64-bit) 

locale: 
[1] en_CA.UTF-8/en_CA.UTF-8/en_CA.UTF-8/C/en_CA.UTF-8/en_CA.UTF-8 

attached base packages: 
[1] stats  graphics grDevices utils  datasets methods 
[7] base  

other attached packages: 
[1] ggplot2_1.0.0 

loaded via a namespace (and not attached): 
[1] colorspace_1.2-4 digest_0.6.4  grid_3.0.3  
[4] gtable_0.1.2  labeling_0.2  MASS_7.3-31  
[7] munsell_0.4.2 plyr_1.8.1  proto_0.3-10  
[10] Rcpp_0.11.1  reshape2_1.4  scales_0.2.4  
[13] stringr_0.6.2 tools_3.0.3 
4

我正在重複使用從similar question取得的功能,這隻需要稍微調整。

make_labels <- function(value) { 
    x <- as.character(value) 
    do.call(expression, lapply(x, function(y) bquote(bold(.(y))^degree))) 
} 

現在,在您的通話ggplot代替add_degree使用labels = make_labels,你會得到

enter image description here

+1

謝謝! +1,因爲它的工作原理,它教我更多關於如何使用'bquote'。我接受了@ tsurudak的回答,因爲它允許我分別控制'element_text()'中的'face'。此外,我調整了一下代碼,以便度符號不是指數,而是在數字旁邊:'do.call(expression,lapply(x,function(y)bquote(bold(。(y)) *度)))' – MSJ