R barplot cumulative - x date,y freq

时间:2018-01-18 14:18:52

标签: r date plot

我试图在R中重新创建这种情节,但我并不是很成功。 其中X =日期和Y =离散变量的频率,在一个条上累积。 我也试图将它放在一个函数中,这样就可以更容易地将这种情节用于不同的变量。

Link to the plot image <---

我很感激任何帮助!

数据示例: Excel plot example <---

Purchase_date Phone
2014-10-23  Sony
2014-10-23  Apple
2014-10-23  Nokia
2014-10-23  Nokia
2014-10-24  NA
2014-10-24  Nokia
2014-10-24  Sony
2014-10-24  Other
2014-10-24  Apple
2014-10-25  Sony
2014-10-25  NA
2014-10-25  Apple
2014-10-25  Sony
2014-10-25  Nokia

另外 我有类似的东西,但它绝对不同于不同变量的通用方法:

  base_table %>%
  filter(year(as.Date(BUY_DATE)) >= 2014, year(as.Date(BUY_DATE)) <= 2017) %>%
  mutate(BUY_DATE = as.yearmon(as.Date(BUY_DATE))) %>%
  group_by(PHONETYPE, BUY_DATE) %>% summarise(n = n()) -> applPerTypeAndMonth

applPerTypeAndMonth %>% pull(PHONETYPE) %>% table()
filter(applPerTypeAndMonth, PHONETYPE == '') -> x
xts(x$n, order.by = x$BUY_DATE) -> type1
filter(applPerTypeAndMonth, PHONETYPE == 'NOKIA') -> x
xts(x$n, order.by = x$BUY_DATE) -> type2
filter(applPerTypeAndMonth, PHONETYPE == 'APPLE') -> x
xts(x$n, order.by = x$BUY_DATE) -> type3
filter(applPerTypeAndMonth, PHONETYPE == 'SONY') -> x
xts(x$n, order.by = x$BUY_DATE) -> type4
filter(applPerTypeAndMonth, PHONETYPE == 'HUAWEI') -> x
xts(x$n, order.by = x$BUY_DATE) -> type5
filter(applPerTypeAndMonth, PHONETYPE == 'LG') -> x
xts(x$n, order.by = x$BUY_DATE) -> type6
filter(applPerTypeAndMonth, PHONETYPE == 'OTHER') -> x
xts(x$n, order.by = x$BUY_DATE) -> type7
merge(type1,type2,type3,type4,type5,type6,type7) -> types
na.fill(types, fill = 0.0) -> types
barplot(types, col = rainbow(7))
types %>% apply(1, function(x) x / sum(x)) %>% barplot(col = rainbow(7))
# legend("topright", legend = names(types), fill = rainbow(7))

3 个答案:

答案 0 :(得分:0)

与此类似的东西,

dta <- structure(list(Purchase_date = structure(c(1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L), .Label = c("2014-10-23", 
"2014-10-24", "2014-10-25"), class = "factor"), Phone = structure(c(4L, 
1L, 2L, 2L, NA, 2L, 4L, 3L, 1L, 4L, NA, 1L, 4L, 2L), .Label = c("Apple", 
"Nokia", "Other", "Sony"), class = "factor")), .Names = c("Purchase_date", 
"Phone"), class = "data.frame", row.names = c(NA, -14L))

# install.packages(c("ggplot2"), dependencies = TRUE)
library(ggplot2)
g <- ggplot(dta, aes(Purchase_date))
g + geom_bar(aes(fill = Phone))

更新,这是包含在函数中的情节

function.name <- function(df)
{
 require(ggplot2)
  p <- ggplot(df, aes(x = Purchase_date))
  p + geom_bar(aes(fill = Phone))
}

function.name(dta)

devils work

我明显建议您take a look at this site学习标签,颜色,重新排序等。

答案 1 :(得分:0)

使用data.table首先创建一个摘要表,详细说明每天每部手机的频率。

summary = purchases[,list(Purchases = .N), by = list(Purchase_date, Phone)

然后按电话类型将其拆分,并按日期在每个子数据集中进行拆分,并添加累积购买变量。

splitted = split(summary, summary$Phone)
splitted = lapply(splitted, function(x){
      x = x[order(PurchaseDate)]
      x$CumulativePurchases = cumsum(x$Purchases)
      return(x)})

然后将rbindlist重新组合成一个数据帧,然后您就可以轻松使用GGplot。

summary = rbindlist(splitted)
plotted = ggplot(summary, aes(x = PurchaseDate, y = CumulativePurchases, fill = Phone)) + geom_bar(stat = "identity")

答案 2 :(得分:0)

# load packages
library(tidyverse)
library(lubridate)

# create a dataframe from your data
df <- frame_data(
    ~Purchase_date, ~Phone
    , "2014-10-23", "Sony"
    , "2014-10-23", "Apple"
    , "2014-10-23", "Nokia"
    , "2014-10-23", "Nokia"
    , "2014-10-24", "NA"
    , "2014-10-24", "Nokia"
    , "2014-10-24", "Sony"
    , "2014-10-24", "Other"
    , "2014-10-24", "Apple"
    , "2014-10-25", "Sony"
    , "2014-10-25", NA
    , "2014-10-25", "Apple"
    , "2014-10-25", "Sony"
    , "2014-10-25", "Nokia"
)

# make dates dates, if you want to
df <- df %>%
    mutate(Purchase_date = as_date(Purchase_date))

# and plot it
df %>%
    ggplot(aes(Purchase_date, fill = Phone)) +
    geom_bar()

ggplot()和geom_bar()是一个功能,他们做你想做的事情(如果需要,实际上还有很多)。如何绘制可以读取,例如,在R-Graphics Cookbook中,只要你需要它就可以提供帮助。