簡體   English   中英

更改 face_wrap() ggplot2 中的刻面標簽

[英]Changing facet labels in face_wrap() ggplot2

所以下面的代碼可以正常工作,沒有錯誤,我正在嘗試解決以下問題。

首先,我試圖更改每個圖表的組名,例如,“< 1500 美元”是指收入 1500 美元或以下的工人組等......

我嘗試了這個解決方案:更改基礎因子級別名稱,但我不斷收到此錯誤:

"錯誤:意外的 ',' in ""< 1500 Dollars",""

outflows <- Wage_Outflows
levels(outflows$wage_group)
"< 1500",     "1501 ~ 2999", "3000", 
levels(outflows$wage_group) <- c("< 1500 Dollars", "1501 ~ 2999 Dollars", "3000 Dollars")
text.on.each.panel <-"Dollars"

p1 = ggplot(Wage_Outflows[Wage_Outflows$wage_group=="< 1500",], aes(x = year, y = labor)) +
       geom_point() +
       scale_y_continuous(breaks=seq(4000000, 6500000, by = 400000)) +
                             facet_wrap(~ wage_group) + theme(axis.title.x = element_blank())

p2 = ggplot(Wage_Outflows[Wage_Outflows$wage_group=="1501 ~ 2999",], aes(x = year, y = labor)) +
       geom_point() +
       scale_y_continuous(breaks=seq(800000, 1100000, by = 20000)) +
       facet_wrap(~ wage_group) + theme(axis.title.x = element_blank())


p3 = ggplot(Wage_Outflows[Wage_Outflows$wage_group=="3000",], aes(x = year, y = labor)) +
       geom_point() +
       scale_y_continuous(breaks=seq(50000, 120000, by = 5000)) +
       facet_wrap(~ wage_group) + theme(axis.title.x = element_blank())


grid.arrange(p1, p2,p3, ncol=1)

在此處輸入圖片說明

關於第一個問題有一個看看labeller的參數facet_wrap功能。

對於您的第二個問題, labs功能可能是解決方案。

p1 = ggplot(Wage_Outflows[Wage_Outflows$wage_group=="< 1500",], 
     aes(x = year, y = labor)) +
     geom_point() +
     scale_y_continuous(breaks=seq(4000000, 6500000, by = 400000)) +
     labs(y = "Number of workers") +
     facet_wrap(~ wage_group, labeller = labeller(wage_group = c(`< 1500` = "< 1500 
     dollars"))) +
     theme(axis.title.x = element_blank())

也許你可以像這樣縮短你的代碼:

# Example dataset: 

df <- data.frame(wage_group = rep(c("A","B","C"), each = 10),
                 year = 2001:2010,
                 labor = seq(5000,34000, 1000))


ggplot(df , aes(x = factor(year), y = labor)) +
  geom_point() +
  labs(y = "# of workers") +
  facet_wrap(~wage_group, ncol = 1, scales = "free",
             labeller = labeller(wage_group = c(`A` = "less than 1500 dollars", 
             `B` = "1500-2999 dollars", `C` = "more than 3000 dollars"))) +
  theme(axis.title.x = element_blank())

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM