我正在尝试绘制3个不同年份(2016-7-8)的数据,但我只有夏季月份(基本上没有9月至4月的数据)。
当我绘制数据时,图中没有数据的月份出现了,留下了巨大的丑陋空白空间
当我通过因素中的日期时,那些空格消失了。但是日期不再出现在x轴上,我只得到数字...
这是我的一些数据:
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作为因子水平
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'))
然后我使用:
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
答案 0 :(得分:0)
喜欢吗?我还自由地清理了一些代码。
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刻度可以使它得到改善
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')
答案 1 :(得分:0)
如果您不需要分析趋势(在这种情况下最好将“丑陋的空白空间”留在那里),则可以采用以下两种方法:
您可以针对月份绘制数据,并用多年的时间为线条/点着色:
# 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()
使用已经混乱的数据(长格式,日期作为日期):
# you can have geom_line too!
ggplot(dt, aes(x = date,
y = value,
color = variable))+
geom_point()+
facet_wrap(~year(date), scales = "free")
答案 2 :(得分:0)
您可以添加以下几行:
# 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调用中:
# 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") +
答案 3 :(得分:0)