首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >隐藏绘图上缺少的日期

隐藏绘图上缺少的日期
EN

Stack Overflow用户
提问于 2019-06-14 23:12:26
回答 4查看 715关注 0票数 0

我试图绘制3个不同年份(2016-7-8)的数据,但我只有夏季的月份(基本上没有从9月到4月的数据)。

当我绘制数据图时,没有数据的月份会出现在图表上,留下又大又难看的空白区域

当我在因子中传递日期时,这些空格就消失了。但是,日期不再出现在x轴上,我只能得到数字...

以下是我的一些数据:

代码语言:javascript
复制
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

我将我的数据转换为MD和AM作为因子级别

代码语言:javascript
复制
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'))

然后我使用以下命令进行绘图:

代码语言:javascript
复制
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
EN

回答 4

Stack Overflow用户

发布于 2019-06-14 23:49:13

是像这样吗?我还擅自清理了你的一些代码。

代码语言:javascript
复制
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)

看起来有点丑陋,但我认为这是因为你提供的数据。

免费的x-scale稍微改进了一下。

代码语言:javascript
复制
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')

票数 0
EN

Stack Overflow用户

发布于 2019-06-14 23:50:16

如果你不需要分析趋势(在这种情况下,留下“丑陋的空白”会更好),你可以做任何一种方法:

您可以根据月份绘制数据,并有几年的时间为线/点着色:

代码语言:javascript
复制
# 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. 展示你的图景:

使用您已经处理过的数据(长表单,日期作为日期):

代码语言:javascript
复制
# you can have geom_line too!
ggplot(dt, aes(x = date, 
               y = value, 
               color = variable))+
   geom_point()+
   facet_wrap(~year(date), scales = "free")
票数 0
EN

Stack Overflow用户

发布于 2019-06-14 23:50:32

您可以添加以下行:

代码语言:javascript
复制
# 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

然后在你的ggplot调用中:

代码语言:javascript
复制
# 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") +

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

https://stackoverflow.com/questions/56600964

复制
相关文章

相似问题

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