如何从列表制作数据图形

时间:2019-03-07 02:25:58

标签: r

我有一个名称列表,我想为其制作35种不同的数据图形。我为第一个(参考)图提供了正确的代码,但是现在我想为列表中的35个不同元素重现相同的数据图形。我不确定该怎么做...我也是数据科学专业的学生的入门,所以我们仅在使用mapping()函数以及如何定义新函数。这是35个元素的列表(本例中为babynames数据集的名称)。

actual_names <- c("Jessie", "Marion", "Jackie", "Alva", "Ollie",
                  "Jody", "Cleo", "Kerry", "Frankie", "Guadalupe", 
                  "Carey", "Tommie", "Angel", "Hollis", "Sammie",
                  "Jamie", "Kris", "Robbie", "Tracy", "Merrill", 
                  "Noel", "Rene", "Johnnie", "Ariel", "Jan", 
                  "Devon", "Cruz", "Michel", "Gale", "Robin", 
                  "Dorian", "Casey", "Dana", "Kim", "Shannon")

这是用于数据整理的代码,然后将其用于制作数据图形

casey_sex<- babynames %>%
  filter(name == "Casey") %>%
  filter(year >= 1930, year <= 2012) %>%
  group_by(sex)

casey_sextotal <- babynames %>%
  filter(name == "Casey") %>%
  filter(year >= 1930, year <= 2012) %>%
  group_by(year) %>%
  summarise(total = sum(n))

casey <- casey_sextotal %>%
  full_join(casey_sex, by = "year") %>%
  mutate(perc = 100 * (n / total)) %>%
  filter(sex == "F")

casey
casey_plot <- ggplot(casey, aes(x = year))

casey_plot <- casey_plot +
  theme(
    panel.background = element_rect(fill = "aliceblue") ,
    panel.grid.major = element_line(color = "aliceblue"),
    panel.grid.minor = element_blank(),
    plot.background = element_blank()) +
  geom_area(stat = "identity", aes(y = perc), fill ="darkmagenta", col = "darkmagenta") +
  geom_line(aes(y = perc), size = 0.5) +
  ylab(NULL) +
  xlab(NULL) +
  scale_x_continuous(expand = c(0, 0), breaks = seq(1940, 2000, 20), lim = c(1930, 2012), labels = c("1940" = "1940","1960" = "'60", "1980" = "'80", "2000" = "2000")) +
  scale_y_continuous(expand = c(0, 0), breaks = seq(0, 100, 50), lim = c(0,100), labels = c("0" = "0%","50" = "50%", "100" = "100%")) +
  theme(axis.ticks.y = element_blank())

casey_plot <- casey_plot +
  ggtitle("Casey") +
  theme(plot.title = element_text(family = "Century Gothic", size = 40, colour = 'Black', face = "bold"),
        axis.text = element_text(family = "Century Gothic", size = 30, colour = 'Black')) +
  geom_text(x = 1997 , y = 80, label = "BOYS", colour = "White", family = "Century Gothic", size = 15) +
  geom_text(x = 1997 , y = 20, label = "GIRLS", colour = "White", family = "Century Gothic", size = 15) +
  geom_text(x = 1942 , y = 35 , label = "Most", colour = "black", size = 10, family = "Century Gothic", fontface = "italic") +
   geom_text(x = 1950 ,y = 22 , label = "unisex year", colour = "black", size = 10, family = "Century Gothic", fontface = "italic") +
  geom_point(aes(x = 1949, y = 50.19474), fill = "white", colour = "black", size = 8, shape = 21) +
  geom_segment(aes(x = 1947.25, y = 50, xend = 1940, yend = 50)) +
  geom_segment(aes(x = 1940, y = 50, xend = 1940, yend = 41))

casey_plot

0 个答案:

没有答案