R-使散点图看起来像点图,其计数在零以上

时间:2018-07-05 22:49:28

标签: r ggplot2 scatter-plot

我只是在学习R,所以可能有更简单的方法来做到这一点。我有一张数据表,显示一组商店以及它们在一年前同期的市场份额变化。我提供了指向前两个期间的数据的链接。

Sample Data

我目前有一个散点图,看起来像这样

enter image description here

每个行业为期四个星期,每个商店都根据其在上升者和下降者中的排名(正面或负面)以一个点表示。这接近于我要寻找的内容,但是所有的空间都已关闭,并且数据点相互融合。我正在尝试构建看起来更像这样的东西:

Example

基本上看起来更像是点图,但计数在行的上方和下方。散点图似乎并不可行,但我看不到如何制作一个点图,该点图将在线上方显示我的赢家,在线下方显示我的输家,以便零线始终保持一致。这是我用于散点图的代码:

sp1 <- ggplot(store_change_ranked, aes(x=date, y=rank)) +
        geom_point(aes(color = cut(share_chg_yag, c(-Inf, -.1, -.05, -.025, -.015, 0, .015, .025, .05, .1, Inf)))) +
        scale_color_manual(name = "Share Change",
                           values = c("(-Inf,-0.1]" = "red4",
                                      "(-0.1,-0.05]" = "red",
                                      "(-0.05,-0.025]" = "orangered",
                                      "(-0.025,-0.015]" = "darkorange2",
                                      "(-0.015,0]" = "darkorange",
                                      "(0,0.015]" = "greenyellow",
                                      "(0.015,0.025]" = "lightgreen",
                                      "(0.025,0.05]" = "green",
                                      "(0.05,0.1]" = "green2",
                                      "(0.1, Inf]" = "green4"),
                           labels = c("< -10%", " ", "-2.5% to -5.0% ", " ", "0 to -1.5%", "0 to 1.5%", " ", "2.5% to 5.0% ", " ", "10% +")) +
        labs(x = "4-Week Period", title = "Count of Stores Gaining/Losing Share",
             subtitle = "For the 13 periods ending June 2018", y = "# Stores")+
        scale_x_date(date_breaks = "1 month", date_labels = "%m-%y")+
        theme(legend.position = "right", axis.text.y = element_blank(),panel.background=element_blank(),
              panel.grid.major=element_blank(),
              panel.grid.minor=element_blank())

任何帮助将不胜感激。

谢谢!

1 个答案:

答案 0 :(得分:0)

正如其他人在评论中指出的那样,您的情节是不可复制的。话虽这么说,我不能确切地指出您的问题是什么,但是我认为,如果您按照我的所作所为,便可以按照您想要的方式绘制数据。

我为绘图模拟了一些数据,因此它看起来与第二张图片看起来并不完全相同,但是它给出了相同的想法。另外,由于我不知道黑点和灰点是什么,因此我跳过了它们。

这是我想出的情节: enter image description here

这是它的代码:

# **************************************************************************** #
# Simulate Data                                                             ---- 
# **************************************************************************** #

set.seed(123)

create_data <- function(year, month, sector.rising, 
                        max.percent, max.number.sectors) {

  reps <- sum(max.number.sectors, 1)

  if(sector.rising == 1){
    multiplier <- 1
  } else multiplier <- -1

  tmp <- data.frame(
    Year.Month = factor(rep(paste0(year,",", month),reps)),
    Sector = rep(sector.rising,reps),
    Sector.Count = multiplier*seq(0, max.number.sectors),
    Percent = multiplier*sort(runif(reps,min =0, max = max.percent))
  )

  return(tmp)
}


df.tmp <- NULL

for (k.sector in 1:2){

  for (i.year in 2006:2016){
    for (j.month in 1:12) {

      if (k.sector == 1) { # 1 for rising, 2 for falling
        ran.percent <- runif(1,0,1)
      } else ran.percent <- runif(1,0,1.25)

      ran.number.sectors <- rbinom(1, 20, 0.5)

      tmp <- create_data(year = i.year,
                         month = j.month,
                         sector.rising = k.sector, 
                         max.percent = ran.percent,
                         max.number.sectors = ran.number.sectors
      )

      df.tmp <- rbind(df.tmp, tmp)

    }
  }

}

# **************************************************************************** #
# Plot                                                                      ---- 
# **************************************************************************** #

p <- ggplot(
      data = df.tmp,
      aes(x=Year.Month,
          y=Sector.Count, 
          color = cut(Percent, breaks = seq(-1.25,1,.25),include.lowest = T)
      )
    ) + 
    geom_point(
      size=2,
      alpha = 1,
      pch = 19
    ) +
    scale_x_discrete(
      position = "top",
      breaks = c("2007,1","2008,1","2009,1","2010,1","2011,1",
                 "2012,1","2013,1","2014,1","2015,1"
      ),
      labels = c("2007","2008","2009","2010","2011",
                 "2012","2013","2014","2015"
      ),
      name = ""
      ) +
    scale_y_continuous(
      limits = c(-20,20),
      breaks = seq(-20,20,5),
      labels = as.character(seq(-20,20,5)),
      name = "< SECTORS FALLING       SECTORS RISING >",
      expand = c(0,0)
      ) + 
    scale_color_manual(
      values = c("#d53e4f","#f46d43","#fdae61","#fee08b",
                 "#ffffbf","#e6f598","#abdda4","#66c2a5","#3288bd"),
      name = "", 
      drop = FALSE,
      labels = c("     ",
                 "-1%   ",
                 "     ",
                 "     ",
                 "     ",
                 "0%   ",
                 "     ",
                 "     ",
                 ".75% "),
      guide = guide_legend(
        direction = "horizontal",
        keyheight = unit(2, units = "mm"),
        keywidth = unit(2, units = "mm"),
        nrow = 1,
        byrow = T,
        reverse = F,
        label.position = "bottom",
        override.aes=list(shape=15, cex = 7),
        label.hjust = -0.4,
        title.hjust = 0.5
      )
    ) +
    theme(
      text = element_text(size = 10, color = "#4e4d47"),
      panel.background = element_blank(),
      legend.key.size = unit(1,"mm"),
      legend.position = "top",
      axis.title = element_text(size = 8, color = "#4e4d47"),
      legend.text = element_text(size = 6, color = "#4e4d47")
    )

p