如何在R的ggplot2中將條形圖的X軸標籤轉換為斜體?


顯然,與其他任何統計分析工具一樣,R中軸標籤的預設字型不是斜體,但我們可以使用ggplot2來實現。為此,我們可以使用ggplot2包的theme函式,其中我們可以使用axis.text.x引數更改軸標籤的字型。

示例

考慮以下資料框

線上演示

> x<-c("A","B","C","D")
> y<-c(24,23,25,27)
> df<-data.frame(x,y)
> df

輸出

  x  y
1 A 24
2 B 23
3 C 25
4 D 27

載入ggplot2包並建立條形圖

示例

> library(ggplot2)
> ggplot(df,aes(x,y))+geom_bar(stat="identity")

輸出

建立帶有斜體X軸標籤的條形圖

示例

> ggplot(df,aes(x,y))+geom_bar(stat="identity")+theme(axis.text.x=element_text(face=c("italic","italic","italic","italic")))
Warning message:
Vectorized input to `element_text()` is not officially supported.
Results may be unexpected or may change in future versions of ggplot2.

此警告訊息不會影響圖表,但它有助於我們理解ggplot2的未來版本可能不支援element_text內的向量化輸入。

輸出

更新於:2020年11月19日

837 次瀏覽

啟動您的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.