首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用ggplot2复制食品可视化的节律

用ggplot2复制食品可视化的节律
EN

Stack Overflow用户
提问于 2017-02-07 03:00:17
回答 1查看 148关注 0票数 3

我正在尝试用我自己的数据集复制谷歌Rhythm of Food的漂亮可视化,显示我的公司每周雇佣了多少人。数据集(名为hiresbyweek)如下所示(这是81行中的25行,link to full dataset here)

代码语言:javascript
复制
            Week Year total.Hires     Month WeekNum
  2014-05-05 0:00:00 2014           1       May      18
  2014-05-12 0:00:00 2014           1       May      19
  2014-05-19 0:00:00 2014           1       May      20
  2014-05-26 0:00:00 2014           1       May      21
  2014-08-04 0:00:00 2014           1    August      31
  2014-09-08 0:00:00 2014           1 September      36
  2015-02-23 0:00:00 2015           3  February      08
  2015-03-23 0:00:00 2015           4     March      12
  2015-05-04 0:00:00 2015           1       May      18
  2015-06-01 0:00:00 2015           1      June      22
  2015-06-08 0:00:00 2015           1      June      23
  2015-09-14 0:00:00 2015           3 September      37
  2015-09-21 0:00:00 2015           4 September      38
  2015-09-28 0:00:00 2015          15 September      39
  2015-10-05 0:00:00 2015          20   October      40
  2015-10-12 0:00:00 2015          47   October      41
  2015-10-19 0:00:00 2015          40   October      42
  2015-10-26 0:00:00 2015          39   October      43
  2015-11-02 0:00:00 2015           5  November      44
  2015-11-09 0:00:00 2015           2  November      45
  2015-11-16 0:00:00 2015           7  November      46
  2015-11-23 0:00:00 2015           1  November      47
  2015-11-30 0:00:00 2015           7  November      48
  2015-12-07 0:00:00 2015           3  December      49
  2015-12-14 0:00:00 2015           7  December      50

目前我已经做到了这一点:

代码语言:javascript
复制
ggplot(hiresbyweek,aes( x=WeekNum, y=total.Hires,fill=as.factor(Year)))
+geom_histogram(stat="identity", aes( x=WeekNum, y=total.Hires,fill=as.factor(Year)))
+coord_polar()
+scale_fill_manual(values=c("#ACD9F4","#005DA6","#EC008C"))
+scale_x_discrete(labels = as.factor(hiresbyweek$Month))
+scale_y_discrete(expand=c(0.5,0))
+theme(text=element_text(family="Avenir")
       , axis.ticks = element_blank()
       , panel.grid = element_blank()
       , panel.background = element_blank()
       )

这就产生了一些类似的东西:

最基本的问题是:

1)这些标签离它们应该在的地方很远:注意最大的数字是在10月份,但根据图表,它们大多在4月或3月。

拥有的美好事物:

1)我想按照食物图表的节奏对这些标题进行分组和轮换,这样就会有更简单的标签

2)我希望大大减少上述条形图的相对大小;我已经这样做了(计数(geom_historgram(stat=" count ")或stat=“stat=”)),但这使得它们都是相等的,并消除了缩放的重要性,这是这里的关键。

3)我想在条之间插入一些空格。我试过在color=中添加一个“白色”图(hiresbyweek,aes( x=WeekNum,y=total.Hires,colour=“白色”,fill=as.factor(Year))和geom_histogram(stat="identity",aes( x=WeekNum,y=total.Hires,fill=as.factor(Year),color=“白色”)),它们都奇怪地得到了粉红色的轮廓……

第一部分的帮助是最重要的(当时我觉得它还不错),但欢迎任何人和所有人。感谢您的时间和想法。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-02-07 05:48:00

我一直在等待其他人发布一个更好的和更少的黑客回答,但我希望这将在此期间。

代码语言:javascript
复制
# 1. We can control the order of geom_bars based on the levels of the factor of X. 
# So we make a new factor variable and ensure that the levels are in the order of 
# < January1, January2, ..., February2, ..., December3, December4 >  
hiresbyweek <- hiresbyweek[order(hiresbyweek$WeekNum),]
hiresbyweek$X <- factor(paste0(hiresbyweek$WeekNum, hiresbyweek$Month), 
                    levels = unique(paste0(hiresbyweek$WeekNum, hiresbyweek$Month)))

# 2. But we don't want the axis labels to be: "Jan1, Jan2, Jan3, ..."
# Instead we'll extract only the month out of the X variable (though notice the weekNum
# variable was important so we could get the right order and distinct factor levels)
# But we also don't want repeated axis labels: "Jan, "Jan", "Jan", "Feb", "Feb", ....
# So try to place the unique axis label close to the middle, and leave the rest blank
# (ie. "", "Jan", "", "", "Feb")
makeLabels <- function(x) {
  x <- gsub("[0-9]", "", x)
  labs <- c();
  for (a in unique(x)) {
    b <- rep("", length(x[x == a]))
    b[ ceiling(length(x[x==a])/2) ] <- a
    labs <- append(labs, b)
  }
  return(labs)
}

# 3. Angle the axis labels to imitate Google's Rhythm of Food
ang <- -360 / length(unique(hiresbyweek$X)) * seq_along(hiresbyweek$X)
ang[ang <= -90 & ang >= -300] <- ang[ang <= -90 & ang >= -300] -180

ggplot(hiresbyweek, aes( x = X, y = total.Hires,fill = as.factor(Year))) +
  geom_histogram(stat="identity", width = 0.5) + # Use width arg for more space between bars
  coord_polar() + 
  scale_x_discrete(labels = makeLabels) + # Apply getLabel function to X
  scale_y_discrete(expand=c(0.5,0)) + 
  scale_fill_manual(values=c("#ACD9F4","#005DA6","#EC008C")) + 
  theme(axis.ticks = element_blank(), 
    panel.grid = element_blank(), 
    panel.background = element_blank(),
    text = element_text(family="Avenir"),
    title = element_blank(), # Remove all titles
    axis.text.x = element_text(angle= ang)) # Apply angles to x-axis labels

结果:

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42075261

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档