问题描述
下面的代码可以正常工作,并且没有错误,我正在尝试解决以下问题。
首先,我试图将每个图形的组名称更改为例如“
我尝试了此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)
解决方法
对于第一个问题,请查看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())