ggplot2绘图并绘制平均值和最小值

时间:2017-09-19 06:12:15

标签: r

这是我的代码:

library(dplyr); library(tidyr)

T0.modified <- T0data %>%

  # create year range based on each company's T0 year
  mutate(Year.M1 = Year - 1,
         Year.M2 = Year - 2,
         Year.M3 = Year - 3,
         Year.P1 = Year + 1,
         Year.P2 = Year + 2,
         Year.P3 = Year + 3) %>%

  # convert to long format, match with Alldata based on both company & year
  gather(reference.year, actual.year, -Company, -Price) %>%
  left_join(Alldata, by = c("Company" = "Company", "actual.year" = "Year")) %>%

  # keep T0 price for year T0, & use matched prices for all other years
  mutate(Price = ifelse(reference.year == "Year", Price.x, Price.y)) %>%

  # take maximum of all matched prices for each company each year
  group_by(Company, reference.year) %>%
  summarise(Price = max(Price)) %>%
  ungroup() %>%

  # order reference.year for correct sequence in ggplot's x-axis
  mutate(reference.year = factor(reference.year,
                                 levels = c("Year.M3", "Year.M2", "Year.M1", "Year",
                                            "Year.P1", "Year.P2", "Year.P3"),
                          labels = c("T-3", "T-2", "T-1", "T0", "T+1", "T+2", "T+3")))

ggplot(T0.modified,
       aes(x = reference.year, y = Price, group = Company, color = Company)) +
  geom_line(aes()) +
  xlab("Year") + theme_bw() + 

  stat_summary(fun.y = mean, geom = "line", group = 1,
                 linetype = 2, size = 1.5, colour = "grey") +
  annotate("label", x = 7, y = 200, label = "Average", 
           fill = "grey", alpha = 0.5, hjust = 1)

这是我的数据:

T0data:

structure(list(Company = structure(1:3, .Label = c("Amazon", 
"Cisco", "McDonald's"), class = "factor"), Year = c(2011L, 2008L, 
2013L), Price = c(182, 21.82, 95.15)), .Names = c("Company", 
"Year", "Price"), row.names = c(NA, 3L), class = "data.frame")

所有数据:

structure(list(Company = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L
), .Label = c("Amazon", "Cisco", "McDonald's"), class = "factor"), 
    Year = c(2008L, 2008L, 2008L, 2008L, 2009L, 2009L, 2010L, 
    2010L, 2010L, 2011L, 2011L, 2012L, 2012L, 2013L, 2013L, 2014L, 
    2014L, 2014L, 2008L, 2010L, 2010L, 2010L, 2011L, 2011L, 2012L, 
    2012L, 2013L, 2013L, 2014L, 2014L, 2014L, 2015L, 2015L, 2016L, 
    2016L, 2016L, 2005L, 2005L, 2005L, 2006L, 2006L, 2007L, 2007L, 
    2007L, 2008L, 2008L, 2009L, 2009L, 2009L, 2010L, 2010L, 2011L, 
    2011L, 2011L), Price = c(91L, 77L, 81L, 87L, 63L, 88L, 110L, 
    75L, 117L, 170L, 190L, 215L, 245L, 316L, 275L, 330L, 378L, 
    390L, 55L, 62L, 66L, 65L, 72L, 98L, 93L, 88L, 99L, 101L, 
    94L, 103L, 96L, 99L, 116L, 112L, 123L, 113L, 19L, 17L, 18L, 
    20L, 19L, 26L, 31L, 27L, 24L, 21L, 14L, 22L, 18L, 26L, 22L, 
    14L, 16L, 15L)), .Names = c("Company", "Year", "Price"), class = "data.frame", row.names = c(NA, 
-54L))

这是我的问题:

如何使折线图仅显示所有值的2个值,平均值和最小值? 我如何绘制一个随机公司来表示图中的第三行,以便将其与最小值和平均值进行比较?

1 个答案:

答案 0 :(得分:0)

这样的东西?它绘制了平均值,最小值和随机公司(见子集)。

p = ggplot(T0.modified) + xlab("Year") + theme_bw() + 
               stat_summary(aes(x = reference.year, y = Price),fun.y = mean, geom = "line", group = 1, linetype = 2, size = 1.5, colour = "grey") +
               stat_summary(aes(x = reference.year, y = Price),fun.y = min, geom = "line", group = 1, linetype = 2, size = 1.5, colour = "red") +
               annotate("label", x = 7, y = 200, label = "Average", fill = "grey", alpha = 0.5, hjust = 1) +
               annotate("label", x = 7, y = 30, label = "Min", fill = "grey", alpha = 0.5, hjust = 1) +
               geom_line(data = subset(T0.modified,Company=="Amazon"),aes(x = reference.year, y = Price,group=Company),color="blue")