简体   繁体   English

在地块上隐藏缺少的日期

[英]Hide missing dates on a plot

I'm trying to plot data from 3 different years (2016-7-8) but I only have the summer months (basically no data from September to April). 我试图绘制3年不同年份(2016-7-8)的数据,但我只有夏季月份(基本上没有9月到4月的数据)。

When I plot the data, the months with no data appear on the graph, leaving big ugly empty spaces 当我绘制数据时,图表上会显示没有数据的月份,留下很大的空白空间

When I pass the dates in factors, those spaces disappear. 当我在因子中传递日期时,这些空格会消失。 But then the dates do not appear on the x-axis anymore, I only get numbers ... 但是日期不再出现在x轴上,我只得到数字......

Here is some of my data: 以下是我的一些数据:

date    MD g/day    AM g/day
26/04/2016  82  154
27/04/2016  238 140
28/04/2016  140 661
29/04/2016  181 304
30/04/2016  92  329
07/07/2017  976 126
08/07/2017  923 47
09/07/2017  527 77
10/07/2017  285 84
11/07/2017  8704    155
07/06/2018  3115    170
08/06/2018  151 65
09/06/2018  415 247
10/06/2018  153 402
11/06/2018  172 95
12/06/2018  188 114

I transform my data to have MD and AM as factor levels 我将我的数据转换为MD和AM作为因子水平

a <- loads$MD.g.day
b <- loads$AM.g.day
d <- data.frame(date=loads$date, MD=a, AM=b)
d$date <- as.Date(loads$date, format='%d/%m/%Y')
colnames(d) <- c('date','MD','AM')
e <- rbind(data.frame(date=c(d$date), gday=c(d$MD), factor='MD'),
           data.frame(date=c(d$date), gday=c(d$AM), factor='AM'))

and then I plot using: 然后我用以下情节绘图:

p <- ggplot(data=e,aes(x=date))+ #select data
  geom_point(aes(y=gday*7/35, color=factor, shape=factor), data=e[e$factor=='MD', ], )+ #select and scale
  geom_line(aes(y=gday*7/35, color=factor), data=e[e$factor=='MD', ])+ #select and scale md

  geom_point(aes(y=gday, color=factor, shape=factor), data=e[e$factor=='AM', ])+ #select other compound
  geom_line(aes(y=gday, color=factor), data=e[e$factor=='AM', ])+ #select other compound


  scale_y_continuous(name = 'AM [g/day]\n',
                     sec.axis = sec_axis(~.*35/7, name = "MD [g/day]\n"), limits = c(0,7000))+  #add y-axis texts and secondary y-axis
  scale_x_date(date_labels = '%e %b %y', date_breaks='1 month')+  #arrange text for the x-axis
  scale_color_manual(values=c(MD='magenta', AM='light green'))+ #define colors
  scale_shape_manual(values=c(MD=21, AM=21))+ #define dot shapes
  scale_size_manual(values=c(MD=1.5, AM=2.5))+ #define dot sizes
  theme(axis.text.x = element_text(angle=90)), #turn text from the x-axis

Like this? 像这样? I also took the liberty to clean a few bits of your code. 我也冒昧地清理了一些代码。

library(ggplot2) # for ggplot
library(data.table) # for fread, melt, year

a <- fread('date    "MD g/day"    "AM g/day"
      26/04/2016  82  154
      27/04/2016  238 140
      28/04/2016  140 661
      29/04/2016  181 304
      30/04/2016  92  329
      07/07/2017  976 126
      08/07/2017  923 47
      09/07/2017  527 77
      10/07/2017  285 84
      11/07/2017  8704    155
      07/06/2018  3115    170
      08/06/2018  151 65
      09/06/2018  415 247
      10/06/2018  153 402
      11/06/2018  172 95
      12/06/2018  188 114')


a$date <- as.Date(a$date, format='%d/%m/%Y')

a$year <- year(a$date) # an extra year-column

#your rescaling:
a$`MD g/day` <- a$`MD g/day`/5

# to long-format
b <- melt(a, id.vars = c('date','year'), variable.name = 'group')

# plot
ggplot(b, aes(x = date, y = value, color = group)) +
  geom_line() +
  geom_point(aes(shape = group))+
  scale_y_continuous(name = 'AM [g/day]\n',
                     sec.axis = sec_axis(~./5, name = "MD [g/day]\n"), limits = c(0,7000))+
  scale_x_date(date_labels = '%e %b %y',date_breaks = '1 month')+
  scale_color_manual(values=c('magenta', 'light green'))+
  scale_shape_manual(values=c(21, 21))+
  scale_size_manual(values=c(1.5, 2.5))+
  theme(axis.text.x = element_text(angle=90)) +
  facet_wrap(~year)

Looks a bit ugly, but I think this is because of the data you presented. 看起来有点难看,但我认为这是因为您提供的数据。

free x-scales improves it a bit 免费的X尺度改善了一点

ggplot(b, aes(x = date, y = value, color = group)) +
  geom_line() +
  geom_point(aes(shape = group))+
  scale_y_continuous(name = 'AM [g/day]\n',
                     sec.axis = sec_axis(~./5, name = "MD [g/day]\n"), limits = c(0,7000))+
  scale_x_date(date_labels = '%e %b %y',date_breaks = '1 month')+
  scale_color_manual(values=c('magenta', 'light green'))+
  scale_shape_manual(values=c(21, 21))+
  scale_size_manual(values=c(1.5, 2.5))+
  theme(axis.text.x = element_text(angle=90)) +
  facet_wrap(~year, scales = 'free_x')

If you don't need to analyze the trend (in which case it will be better to leave the "ugly empty spaces" there), you could do either approach: 如果你不需要分析趋势(在这种情况下最好留下“丑陋的空白”),你可以做任何一种方法:

You can plot the data against months, and have years to color the lines / points: 您可以根据月份绘制数据,并有多年为线/点着色:

# make data
library(data.table)
dt <- fread("date    MD_g/day    AM_g/day
26/04/2016  82  154
27/04/2016  238 140
28/04/2016  140 661
29/04/2016  181 304
30/04/2016  92  329
07/07/2017  976 126
08/07/2017  923 47
09/07/2017  527 77
10/07/2017  285 84
11/07/2017  8704    155
07/06/2018  3115    170
08/06/2018  151 65
09/06/2018  415 247
10/06/2018  153 402
11/06/2018  172 95
12/06/2018  188 114")

# convert date to date
dt[, date = dmy(date)]

# it's necessary to convert wide data to long format:
dt <- melt(dt, id.vars = "date")

# plot data - notice you can go with geom_line too!
ggplot(dt, aes(x = month(date), 
               y = value, 
               color = variable, 
               type = year(date)))+
  geom_point()
  1. Facet your plot: 面对你的情节:

With your already wrangled data (long form, dates as dates): 使用已经存在争议的数据(长格式,日期为日期):

# you can have geom_line too!
ggplot(dt, aes(x = date, 
               y = value, 
               color = variable))+
   geom_point()+
   facet_wrap(~year(date), scales = "free")

You could add theses lines: 你可以添加这些行:

# Pre-processing to put different months into different groups.
e$month <- lubridate::floor_date(e$date, "1 month")
# You might alternately look for gaps in the data of at least
#    x days to define each new group

Then in your ggplot call: 然后在你的ggplot调用中:

# More appropriate breaks for this data:
scale_x_date(date_labels = '%e %b %y', date_breaks='1 day')+  #arrange text for the x-axis
facet_wrap(~month, scales = "free_x") +

在此输入图像描述

Your code should work pretty much as is, with one minor change and the addition of facet_wrap . 您的代码应该可以正常工作, facet_wrap稍作修改即可添加facet_wrap In scale_x_date simply set date_breaks to "1 day" instead of "1 month" , and then call: scale_x_date只需将date_breaks设置为"1 day"而不是"1 month" ,然后调用:

library(lubridate) # Needed for `year` function.
p + facet_wrap(~year(date), scales = "free_x")

The above code returns the following plot: 上面的代码返回以下图:

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM