简体   繁体   English

如何在 R 中绘制组直方图并添加趋势线?

[英]how to draw group histogram graph and add trend line in R?

example graph示例图

As picture shows above, I have data like如上图所示,我有类似的数据

year  0-19years_old   20-44years_old  45-64years_old  above_65years old
2000   20000             34000            29000           16700
2005   19800             33000            28500           17000

I would like to draw the each age group separately histogram graph in r, and the x present year, y present count.我想在 r 中分别绘制每个年龄组的直方图,以及 x 当前年份,y 当前计数。 It will be great if use different color for each histogram and give note in the side to say which color present what age group.如果为每个直方图使用不同的颜色并在旁边注明哪种颜色代表哪个年龄段,那就太好了。 Also draw another linear graph shows how each age group change for two years, here should be 4 lines in different color but in one graph.还要绘制另一个线性图,显示每个年龄组在两年内的变化情况,这里应该是 4 条不同颜色的线,但在一个图中。

Thanks for all help!感谢大家的帮助!

Something like this:像这样的东西:

library(ggplot2)
library(tidyr)

dat <- tibble::tribble(
  ~year,  ~`0-19`,   ~`20-44`,  ~`45-64`, ~`65+`,
  2000,   20000,             34000,            29000,           16700,
  2005,   19800,             33000,            28500,           17000
) |> 
  pivot_longer(
    cols = -year,
    names_to = "age_group",
    values_to = "count"
  )
dat
#> # A tibble: 8 x 3
#>    year age_group count
#>   <dbl> <chr>     <dbl>
#> 1  2000 0-19      20000
#> 2  2000 20-44     34000
#> 3  2000 45-64     29000
#> 4  2000 65+       16700
#> 5  2005 0-19      19800
#> 6  2005 20-44     33000
#> 7  2005 45-64     28500
#> 8  2005 65+       17000

# Grouped bar chart
ggplot(dat) +
  aes(x = year, y = count, fill = age_group) +
  geom_col(position = "dodge")

# Line chart
ggplot(dat) +
  aes(x = year, y = count, color = age_group) +
  geom_line()

Created on 2021-05-30 by the reprex package (v2.0.0)代表 package (v2.0.0) 于 2021 年 5 月 30 日创建

The key ideas are:关键思想是:

  1. Organize your data with the variables in columns.使用列中的变量组织数据。
  2. Map age group to fill and/or color in the plot Map 年龄组在 plot 中fill和/或color
  3. Set position = "dodge" in geom_col() for the grouped bar chartgeom_col()中为分组条形图设置position = "dodge"

What you need is a bar chart not a histogram.您需要的是条形图而不是直方图。

  1. Bring your data into long format with pivot_longer使用pivot_longer将您的数据转换为长格式
  2. use geom_bar() for the bar chart对条形图使用geom_bar()
  3. geom_line() for line chart geom_line()用于折线图
  4. library(cowplot) to plot both plots side by side库(cowplot)到 plot 两个图并排
library(tidyverse)
library(cowplot)

df1 <- df %>% 
  pivot_longer(
    cols = ends_with("old"),
    names_to = "names",
    values_to = "values"
  ) 

a <- ggplot(df1, aes(x=factor(year), y=values, fill=names)) +
  geom_bar(stat="identity", position = "dodge") + 
  geom_text(aes(label=values), vjust=1.6, color="black",
            position = position_dodge(0.9), size=3.5)+
  scale_fill_brewer(palette = "Set1")

b <- ggplot(df1, aes(x=factor(year), y=values, color=names, group=names)) +
  geom_point() + 
  geom_line() +
  scale_color_brewer(palette = "Set1")

plot_grid(a, b, labels = "AUTO")

在此处输入图像描述

data:数据:

df <- tribble(
~year,  ~`0-19years_old`,   ~`20-44years_old`,  ~`45-64years_old`,  ~`above_65years_old`, 
2000,   20000,             34000,            29000,           16700,
2005,   19800,            33000,           28500,           17000)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM