【问题标题】:how create custom labels on a circular line chart如何在圆形折线图上创建自定义标签
【发布时间】:2021-04-01 22:09:15
【问题描述】:

我试图弄清楚如何每周绘制数据,同时在圆轴上仅使用几个月的标签。

here 部分涵盖了该主题,但他们拥有的数据在结构上有所不同。

我有一个数据集,其中包含每周发布的帖子数:

   id week posts   weekdate year month  col fct_month
1:  1    1    26 2001-01-01 2001     1 grey       Jan
2:  2    2    24 2001-01-08 2001     1 grey       Jan
3:  3    3    35 2001-01-15 2001     1 grey       Jan
4:  4    4    35 2001-01-22 2001     1 grey       Jan
5:  5    5    19 2001-01-29 2001     1 grey       Jan
6:  6    6    55 2001-02-05 2001     2 grey       Feb

我需要为每一年创建一个圆形图。我可以用每周的标签来做到这一点:


library(data.table)
library(dplyr)
library(ggplot2)
library(hrbrthemes)

a<-"
id   week posts   weekdate year month  col fct_month
1     1    26 2001-01-01 2001     1 grey       Jan
2     2    24 2001-01-08 2001     1 grey       Jan
3     3    35 2001-01-15 2001     1 grey       Jan
4     4    35 2001-01-22 2001     1 grey       Jan
5     5    19 2001-01-29 2001     1 grey       Jan
6     6    55 2001-02-05 2001     2 grey       Feb
7     7    32 2001-02-12 2001     2 grey       Feb
8     8    31 2001-02-19 2001     2 grey       Feb
9     9    36 2001-02-26 2001     2 grey       Feb
10   10    36 2001-03-05 2001     3 grey       Mar
11   11    24 2001-03-12 2001     3 grey       Mar
12   12    42 2001-03-19 2001     3 grey       Mar
13   13    29 2001-03-26 2001     3 grey       Mar
14   14    57 2001-04-02 2001     4 grey       Apr
15   15    31 2001-04-09 2001     4 grey       Apr
16   16    33 2001-04-16 2001     4 grey       Apr
17   17    17 2001-04-23 2001     4 grey       Apr
18   18    16 2001-04-30 2001     4 grey       Apr
19   19    35 2001-05-07 2001     5 grey       May
20   20    23 2001-05-14 2001     5 grey       May
"
mindf<-fread(a)
ggplot(data=mindf, aes(x=factor(week), y=posts, group = year, color = col)) + 
  ylim(0,NA) +
  geom_point(, stat='identity') +
  geom_polygon(fill=NA)+
  coord_polar(start =0) +
  scale_color_identity() +
  xlab('Week of the year')+
  ylab('Job ads posted per week') +
  theme_ipsum()

结果是:

但是当我用标记月份来做这件事时,一切都会失败,因为我猜它会按月份分组:

ggplot(mindf, aes(x = fct_month, y = posts, 
               group = year, colour = col)) + 
  geom_line(size = .5) +
  # geom_polygon(fill=NA)+
  scale_color_identity()+
  # scale_y_discrete(expand = c(0, 1), breaks = NULL) +
  xlim(month.abb) + 
  coord_polar() +
  theme_bw() + xlab(NULL) + ylab(NULL)

更新:

如果我添加 scale_x_date(date_breaks = "1 month", date_labels = "%b") 正如@stefan 下面建议的那样,这会导致多个月份具有相同的名称:

这是用于检查的可重现代码:

library(data.table)
library(dplyr)
library(ggplot2)


a<-" week posts   weekdate year month  col fct_month
    1    26 2001-01-01 2001     1 grey       Jan
    2    24 2001-01-08 2001     1 grey       Jan
    3    35 2001-01-15 2001     1 grey       Jan
    4    35 2001-01-22 2001     1 grey       Jan
    5    19 2001-01-29 2001     1 grey       Jan
    6    55 2001-02-05 2001     2 grey       Feb
    7    32 2001-02-12 2001     2 grey       Feb
    8    31 2001-02-19 2001     2 grey       Feb
    9    36 2001-02-26 2001     2 grey       Feb
   10    36 2001-03-05 2001     3 grey       Mar
    2    34 2002-01-07 2002     1 grey       Jan
    3    34 2002-01-14 2002     1 grey       Jan
    4    27 2002-01-21 2002     1 grey       Jan
    5    27 2002-01-28 2002     1 grey       Jan
    6    25 2002-02-04 2002     2 grey       Feb
    7    36 2002-02-11 2002     2 grey       Feb
    8    34 2002-02-18 2002     2 grey       Feb
    9    41 2002-02-25 2002     2 grey       Feb
   10    34 2002-03-04 2002     3 grey       Mar
   11    34 2002-03-11 2002     3 grey       Mar
"
mindf<-fread(a) %>% 
  mutate(weekdate=as.Date(weekdate, format="%Y-%m-%d"))
ggplot(data=mindf, aes(x=weekdate, y=posts, group = year, color = col)) + 
  ylim(0,NA) +
  geom_point(stat='identity') +
  geom_polygon(fill=NA, aes(group=year,color=year))+
  coord_polar(start =0) +
  scale_color_identity() +
  xlab('Week of the year')+
  ylab('Job ads posted per week') +
  scale_x_date(date_breaks = "1 month", date_labels = "%b") 

【问题讨论】:

    标签: r date ggplot2 dplyr


    【解决方案1】:

    如果你想要月份标签,你可以使用这样的日期刻度:

    library(data.table)
    library(hrbrthemes)
    library(ggplot2)
    
    mindf<-fread(a)
    mindf$weekdate <- as.Date(mindf$weekdate)
    ggplot(data=mindf, aes(x=weekdate, y=posts, group = year, color = col)) + 
      ylim(0,NA) +
      geom_point(, stat='identity') +
      geom_polygon(fill=NA)+
      coord_polar(start =0) +
      scale_color_identity() +
      scale_x_date(date_breaks = "1 month", date_labels = "%b") +
      xlab('Week of the year')+
      ylab('Job ads posted per week') +
      theme_ipsum()
    

    编辑 如果是多年,日期刻度仍然是可能的。在这种情况下,我们可以修复映射到 x 上的日期年份,我在代码中将其称为 monthdate

    library(data.table)
    library(hrbrthemes)
    library(ggplot2)
    
    mindf<-fread(a)
    mindf$weekdate <- as.Date(mindf$weekdate)
    mindf$day <- format(mindf$weekdate, "%d")
    mindf$monthdate <- as.Date(paste("2001", mindf$month, mindf$day, sep = "-"))
    
    ggplot(data=mindf, aes(x=monthdate, y=posts, group = year, color = col)) + 
      ylim(0,NA) +
      geom_point(, stat='identity') +
      geom_polygon(fill=NA)+
      coord_polar(start =0) +
      scale_color_identity() +
      scale_x_date(date_breaks = "1 month", date_labels = "%b") +
      xlab('Week of the year')+
      ylab('Job ads posted per week') +
      theme_ipsum()
    

    【讨论】:

    • 谢谢!但请参阅更新。不幸的是,在这种情况下,它开始重复月份名称(几个 1 月、2 月等)
    • 嗨菲利普。好的。我知道了。看看我的编辑。也许这为您的问题提供了解决方案。
    • 是的。据我所知,这应该可行。当然你可以选择任何你想要的年份。 (;
    • (: 是的。骑自行车时也有同样的想法。不使用真实数据集时,人们总是会忘记细节。Best S.
    猜你喜欢
    • 1970-01-01
    • 2019-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多