将时间变量添加到ggplot

时间:2018-08-24 12:59:45

标签: r

我正在尝试创建一个条形列,该条列按年份查看按品牌衡量的NPS。

我的data.frame = nps 和变量是nps $ NPS.survey,nps $ brand,nps $ Review,date

我加载

library(lubridate)
library(ggplot2)
library(NPS)
library(NPS)

我创建

nps$year <- as.factor(year(nps$Response.Date))

yearly <- aggregate(nps$NPS.Survey, list(nps$year), FUN = nps, 
nps$NPS.Survey)

brand <- aggregate(nps$NPS.Survey, list(nps$Brand), FUN = nps, 
nps$NPS.Survey)

以下给出了一个条形图,显示了所有数据按年份显示的NPS

ggplot(yearly) +
  geom_col(aes(x=as.factor(Group.1), y=x, fill=as.factor(Group.1)), 
  width=0.5) +

  xlab("Year") + 
  ylab("NPS") + 

  geom_text(aes(x=as.factor(Group.1), y=x, label=round(x,2)),  vjust = 1, 
  hjust = 1)  +
  coord_flip()  + 
  scale_fill_brewer("Year", palette = "Set1") 

这给了我品牌的NPS,每一列都是一个品牌

ggplot(brand) +
  geom_col(aes(x=as.factor(Group.1), y=x, fill=as.factor(Group.1)), 
  width=0.5) +

  xlab("Brand") + 
  ylab("NPS") + 

  geom_text(aes(x=as.factor(Group.1), y=x, label=round(x,2)),  vjust = 1, 
  hjust = 1)  +
  coord_flip()  + 
  scale_fill_brewer("Year", palette = "Set1") 

我想按品牌显示NPS。

任何帮助将不胜感激。

0 个答案:

没有答案