微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

更改face_wrapggplot2中的构面标签

如何解决更改face_wrapggplot2中的构面标签

下面的代码可以正常工作,并且没有错误,我正在尝试解决以下问题。

首先,我试图将每个图形的组名称更改为例如“

我尝试了此solution:更改了底层因子级别名称,但我不断收到此错误

错误:”“

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",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",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)

enter image description here

解决方法

对于第一个问题,请查看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())

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。