首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何添加更多的x轴标签,使人口金字塔与全球图形?R

如何添加更多的x轴标签,使人口金字塔与全球图形?R
EN

Stack Overflow用户
提问于 2020-04-04 01:15:02
回答 1查看 308关注 0票数 1
代码语言:javascript
复制
> gender %>% arrange(desc(age))
# A tibble: 18 x 3
   age   sex      pop
   <chr> <chr>  <dbl>
 1 62+   Female    51
 2 62+   Male     167
 3 55-61 Female    98
 4 55-61 Male     283
 5 5-12  Female    93
 6 5-12  Male      87
 7 45-54 Female   160
 8 45-54 Male     346
 9 35-44 Female   257
10 35-44 Male     315
11 25-34 Female   207
12 25-34 Male     285
13 18-24 Female   103
14 18-24 Male      72
15 13-17 Female    37
16 13-17 Male      34
17 0-4   Female    63
18 0-4   Male     105

我不明白为什么"5-12“没有被正确地订购。

这是我的密码

代码语言:javascript
复制
ggplot(data = gender, 
       mapping = aes(x = age, y = ifelse(test = sex == "Male", yes = -pop, no = pop), 
                     fill = sex, )) +
  geom_col(col = "black") +
  coord_flip() +
  scale_y_continuous(labels = abs, limits = max(gender$pop) * c(-1,1)) +
  labs(y = "Population") + 
  labs(title="Age Distribution by Male & Female Genders",
       #subtitle="Point-In-Time Count 2011-2020",
       x="Age Range", 
       y="Individuals Counted",
       fill = "Gender") + 
  scale_fill_brewer(palette = "Set1") 

目前我的x轴只有3个标签c(200,0,200).

我如何做c(300,200,100,0,100,200,300)

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-04 01:27:03

因为在代码中使用coord_flip,所以"x轴“实际上是"y轴”。要添加更多的中断,只需将值传递到scale_y_continuousscale_y_continuous参数,如:

代码语言:javascript
复制
ggplot(data = df, 
       mapping = aes(x = age, y = ifelse(sex == "Male", yes = -pop, no = pop), 
                     fill = sex)) +
  geom_col(col = "black") +
  coord_flip() +
  scale_y_continuous(labels = abs, limits = max(df$pop) * c(-1,1),
                     breaks = seq(-300,300,by = 100)) +
  labs(y = "Population") + 
  labs(title="Age Distribution by Male & Female Genders",
       #subtitle="Point-In-Time Count 2011-2020",
       x="Age Range", 
       y="Individuals Counted",
       fill = "Gender") + 
  scale_fill_brewer(palette = "Set1")+
  geom_text(aes(x = age, y = ifelse(sex == "Male", yes = -pop, no = pop),label = pop,
                hjust =  ifelse(sex == "Male", yes =0, no = 1)))

它能回答你的问题吗?

可复制示例

代码语言:javascript
复制
structure(list(age = c("62+", "62+", "55-61", "55-61", "5-12", 
"5-12", "45-54", "45-54", "35-44", "35-44", "25-34", "25-34", 
"18-24", "18-24", "13-17", "13-17", "0-4", "0-4"), sex = c("Female", 
"Male", "Female", "Male", "Female", "Male", "Female", "Male", 
"Female", "Male", "Female", "Male", "Female", "Male", "Female", 
"Male", "Female", "Male"), pop = c(51L, 167L, 98L, 283L, 93L, 
87L, 160L, 346L, 257L, 315L, 207L, 285L, 103L, 72L, 37L, 34L, 
63L, 105L)), row.names = c(NA, -18L), .internal.selfref = <pointer: 0x55a536b290c0>, class = c("tbl_df", 
"tbl", "data.frame"))
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61022635

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档