如何绘制某些地区随时间变化的市场份额

时间:2017-08-31 17:06:30

标签: r ggplot2

我在y轴上考虑市场份额,在x轴上考虑时间,并且需要每次都显示我的5个不同位置。我有数据,只是无法获得ggplot命令来显示同一图表上的所有位置。

enter image description here

所以现在我有8个像这样的数据帧。市场份额在诺基亚手机中。如何绘制每个地区不断变化的市场份额?

1 个答案:

答案 0 :(得分:2)

试试这个:

require(ggplot2)
require(scales)

df <- data.frame(
  Location=c(paste0("Location_",c(sapply(1:5,function(x) rep(x,2))))),
  Market_Share=c(.10,.30,.30,.05,.20,.20,.15,.40,.25,.05),
  Date=c("2017-08-30","2017-08-31","2017-08-30","2017-08-31",
         "2017-08-30","2017-08-31","2017-08-30","2017-08-31",
         "2017-08-30","2017-08-31"),
  stringsAsFactors = FALSE
)

ggplot(df,aes(x=as.Date(Date),y=Market_Share,color=Location)) +
  geom_point() +
  geom_line() +
  scale_y_continuous(labels = percent_format()) +
  scale_x_date(labels = date_format("%b-%d-%Y")) +
  xlab("Date") +
  ylab("Market Share")

输出:

enter image description here

编辑:

鉴于您对问题的澄清,这样的事情可能更符合您的要求:

df <- data.frame(
  Location=c("Argentina","Argentina","Brazil","Brazil",
             "Peru","Peru","Venezuela","Venezuela",
             "Chile","Chile"),
  Units=c(7612.40,0.00,540.90,2139.10,879.70,5796.10,
          25.90,760.00, 2615.70, 1386.30),
  Date=c(rep(c("2007","2008"),5)),
  stringsAsFactors = FALSE
)

ggplot(df,aes(x=as.numeric(Date),y=as.numeric(Units),
              color=Location)) +
  geom_point() +
  geom_line() +
  scale_x_continuous(breaks=as.numeric(df$Date)) +
  xlab("Date") +
  ylab("Units")

新输出:

enter image description here