R 绘制时间序列+;堆叠长条图

R 绘制时间序列+;堆叠长条图,r,ggplot2,time-series,bar-chart,R,Ggplot2,Time Series,Bar Chart,我有一个数据集“销售”: Date <- c("2010-01-28", "2010-01-28", "2010-01-28", "2010-01-28", "2010-02-28","2010-02-28", "2010-02-28", "2010-02-28") Type <- c("A", &qu

我有一个数据集“销售”:

Date <- c("2010-01-28", "2010-01-28", "2010-01-28", "2010-01-28",
 "2010-02-28","2010-02-28", "2010-02-28", "2010-02-28")

Type <- c("A", "B", "C", "D", "A", "B", "C", "D")

Dollars <- c(1787, 800, 500, 300, 1950, 1100, 890, 450)
Sales <- data.frame(Date, Type, Dollars)

Date尝试以下任何一种方法:

library(tidyverse)
#Data
Date <- c("2010-01-28", "2010-01-28", "2010-01-28", "2010-01-28",
          "2010-02-28","2010-02-28", "2010-02-28", "2010-02-28")
Type <- c("A", "B", "C", "D", "A", "B", "C", "D")
Dollars <- c(1787, 800, 500, 300, 1950, 1100, 890, 450)
Sales <- data.frame(Date, Type, Dollars)
#Month plot 1
Sales %>% mutate(Date=format(as.Date(Date),'%Y-%m')) %>%
  ggplot(aes(x=Date,y=Dollars,fill=Type,group=Type))+
  geom_bar(stat = 'identity',color='black')+
  scale_y_continuous(labels = scales::comma)+
  theme_bw()+
  theme(legend.position = 'top',
        plot.title = element_text(hjust=0.5))+
  ggtitle('Title of my plot')
输出:


这是否回答了您的问题?
#Month plot 2
Sales %>% mutate(Date=format(as.Date(Date),'%Y-%m')) %>%
  ggplot(aes(x=Date,y=Dollars,fill=Type,group=Type))+
  geom_bar(stat = 'identity',color='black',position='fill')+
  scale_y_continuous(labels = scales::percent)+
  theme_bw()+
  theme(legend.position = 'top',
        plot.title = element_text(hjust=0.5))+
  ggtitle('Title of my plot')+ylab('Proportion of Dollars')