協助解決2個問題 感謝

54 views
Skip to first unread message

Chung-Cheng Yang

unread,
Jul 30, 2021, 10:30:00 AM7/30/21
to R軟體使用者論壇
Q1 再跑相關性的圖時,中文字體跑不出來(我是用mac)
以下是我的code,還有圖,我試過很多方法都沒跳出中文字

```{r 相關性2, fig.cap = "薪資待遇、學歷、工作年資及年齡的相關性2。", warning=FALSE, cache=TRUE, message=FALSE}
GGally::ggpairs(data = constract.df)
GGally::ggcorr(data = constract.df, 
               method = c("complete", "pearson")) + theme(text = element_text(family = "黑體-繁 中黑", size = 14))
Rplot03.png

Q2 要怎麼讓底下x軸數值不要重疊,讓寬距變大一點?
以下是我的code與圖,再請大家幫忙
```{r 工作年資與學歷, fig.cap = "工作年資與學歷的差異。", warning=FALSE, cache=TRUE, message=FALSE}
ggplot(contract_staff, aes(x = 工作年資區間, fill = 學歷)) + 
  geom_bar(position = "dodge") + facet_wrap(~契約人力)+ theme(strip.text.x = element_text(size = 12)) + labs(y = "次數", size = 10) + geom_text(stat = "count", aes(label = ( ..count..)), vjust = 1, color = I("black"), size = 4)

謝謝
工作年資與學歷-1.png

WEPA ^_^

unread,
Jul 31, 2021, 4:38:49 AM7/31/21
to R軟體使用者論壇
Hi Terry,
 
For Q1, 參考以下之內容與附圖中文字型顯示正常.
如果 R-4.1.0 Patched 有錯誤, 再改至R-4.0.5.
近日 4.1.1 將可以下載, 亦可在下載測試.

# R 4.0.5 for Mac 10.15.6

# 1.使用 Mac 內建字型 par(family = "xxx"),中文字型顯示正常.
par(family="宋體-繁 標準體")
pairs(iris[-5], main="iris資料集", pch=".", col=iris$Species)
sessionInfo()

mac_par.png

# 2.ggplot2繪圖 par方法無效時,使用 showtext套件,中文字型顯示正常.
library(showtext)
font_install(source_han_serif())
font_families()
showtext_auto()

library(ggplot2)
ggplot(iris, aes(Petal.Width, Petal.Length, color=Species)) +
  geom_point(size=2) +
  ggtitle("ggplot2-使用內建群組顏色")

mac_ggplot2.png

# 3.GGally繪圖,使用 showtext套件,中文字型顯示正常.
library(GGally)
mydf <- iris[-5]
names(mydf) <- c(paste0("變數", 1:4))
ggpairs(mydf, upper = list(continuous = wrap("cor")))

mac_GGally.png
# end

terry...@gmail.com 在 2021年7月30日 星期五下午10:30:00 [UTC+8] 的信中寫道:

WEPA ^_^

unread,
Jul 31, 2021, 5:37:09 AM7/31/21
to R軟體使用者論壇
For Q2: 如果使用 RStudio 應該可以直接將圖型拖曳放大, X軸文字比較不會重疊, 還是使用時有其他限制?

Q2-x軸數值不要重疊.png

WEPA ^_^ 在 2021年7月31日 星期六下午4:38:49 [UTC+8] 的信中寫道:
Reply all
Reply to author
Forward
0 new messages