如何在R ggplot2中使用高数组的日期日期x轴顺序? [英] How to use this date of tall array in R ggplot2 Date x-axis order?

查看:318
本文介绍了如何在R ggplot2中使用高数组的日期日期x轴顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑如何将高数组格式的字符串Date数据转换为 Date 并将它组织为 ggplot 在x轴上由 scale_x_date
由Henrik的建议激发的伪代码
$ b


  1. 将字符串数据格式更改为 as.Date

    code>,可能类似于以下内容 ggplot 的参数 x = ...

      as.Date(time.data,format(%d。%m。%Y)
    ggplot 中应用 scale_x_date code>

  2. code> with date_breaks(2 day)


使用虚拟数据代码 data3

  library(ggplot2 )
#For RStudio
options(device =pdf)#https://stackoverflow.com/questions/6535927/how-do-i-prevent-rplots-pdf-from-being-generated
filename.pdf< - paste0(getwd(),/,Rplots,.pdf,sep =)
pdf(file = filename.pdf)
#虚拟数据
data3< - 结构(列表(Time.data = c(16.7.2017,15.7.2017,
14.7.2017,13.7.2017, 12.7.2017,11.7.2017,9.7.2017,
7.7.2017,6.7.2017,5.7.2017,4.7.2017,3.7.2017,2.7.2017,
1.7.2017,30.6。 2017,29.6.2017,28.6.2017,16.7.2017,
15.7.2017,14.7.2017,13.7.2017,12.7.2017, 11.7.2017,
9.7.2017,7.7.2017,6.7.2017,5.7.2017,4.7.2017,3.7.2017,
2.7.2017,1.7.2017,30.6.2017,29.6.2017,28.6.2017,
16.7.2017,15.7.2017,14.7.2017 ,13.7.2017,12.7.2017,
11.7.2017,9.7.2017,7.7.2017,6.7.2017,5.7.2017,
4.7.2017,3.7.2017,2.7.2017,1.7.2017,30.6.2017,
29.6.2017,28.6.201 (1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,2L, 2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,3L,3L,3L,3L, 3L,3L,3L,3L,3L,3L,3L,3L,3L,3L,3L,3L,3L
),。标签= c因子),
值= C(69,75,83,97,101,73,77,78,98,79,78,95,
70,81,78,71,72, 58,59,59,58,54,56,60,60,62,58,
56,63,58,58,63,58,56,48,51,51,48,48,48, 52,53,
52,49,48,53,50,50,54, ),row.names = c(NA,-51L
),.Names = c(Time.data,variable,value),class =data.frame)

#基于Henrik提议的相关代码部分,
#rejected timestamp方法,输出在图1中错误地显示了x轴标签
p < - ggplot(data3 ,aes(x = as.Date(Time.data,format =%d。%m。%Y),y = value,fill = variable))+
geom_bar(stat ='identity')+
theme(axis.text.x = element_text(angle = 90,hjust = 1),
text = element_text(size = 10))+
scale_x_discrete(Date)+
scale_x_date(date_breaks =2 days,date_labels =%d。%m。%Y)

print(p)
dev.off()

我不明白的输出

 'x'的比例已经存在。为'x'添加另一个比例,它将替换现有的比例。 

1根据Henrik的建议输出





或者,您可以使用名称参数至 scale_x_date()

  ggplot(data3,aes(x = as.Date(Time.data,format =%d。%m。%Y),y = value,fill = variable))+ 
geom_col()+
theme(axis.text.x = element_text(angle = 90,hjust = 1),
text = element_text(size = 10))+
scale_x_date(name =Time.date,date_breaks =2 days,date_labels =%d。%m。%Y)



附录:保存图



如果您的目的是只保存一个文件中的一个图在调用 ggplot()之后添加对 ggsave()的调用,即

  ggplot(... 
ggsave(Rplots.pdf)

而不是

  options(device =pdf)#https: //www.microsoft.com/downloads/details.aspx?displaylang=zh-cn&displaylang=zh-cn&displaylang=zh-cn&displaylang=zh-cn&displaylang=zh-cn&displaylang=zh-cn&displaylang=zh-cn如何使用 ,.pdf,sep =)
pdf(file = filename.pdf)
p< - ggplot(...
print(p)
dev.off ()

根据 help(ggsave)


ggs ave()是保存绘图的一个方便的函数。它默认使用当前
图形设备的大小来保存您显示的最后一幅图的
。它还猜测
扩展名中的图形设备的类型。







另一个问题是创建文件路径。而不是

  filename.pdf < -  paste0(getwd(),/,Rplots,.pdf ,sep =)

最好使用

  filename.pdf<  -  file.path(getwd(),Rplots.pdf)

它以独立于平台的方式从组件构建文件路径。


I am thinking how to convert string Date data of tall array format to Date and organise the ggplot by it in the x-axis by scale_x_date. Pseudocode motivated by Henrik's proposal

  1. Change string data format to as.Date, maybe something similar to the following in ggplot's parameter x = ...

    as.Date(time.data, format("%d.%m.%Y") 
    

  2. Apply scale_x_date in ggplot with date_breaks("2 day")

Code with dummy data data3

library("ggplot2")
# For RStudio
options(device="pdf") # https://stackoverflow.com/questions/6535927/how-do-i-prevent-rplots-pdf-from-being-generated
filename.pdf <- paste0(getwd(), "/", "Rplots", ".pdf", sep = "")
pdf(file=filename.pdf)
# Dummy data
data3 <- structure(list(Time.data = c("16.7.2017", "15.7.2017", 
                                             "14.7.2017", "13.7.2017", "12.7.2017", "11.7.2017", "9.7.2017", 
                                             "7.7.2017", "6.7.2017", "5.7.2017", "4.7.2017", "3.7.2017", "2.7.2017", 
                                             "1.7.2017", "30.6.2017", "29.6.2017", "28.6.2017", "16.7.2017", 
                                             "15.7.2017", "14.7.2017", "13.7.2017", "12.7.2017", "11.7.2017", 
                                             "9.7.2017", "7.7.2017", "6.7.2017", "5.7.2017", "4.7.2017", "3.7.2017", 
                                             "2.7.2017", "1.7.2017", "30.6.2017", "29.6.2017", "28.6.2017", 
                                             "16.7.2017", "15.7.2017", "14.7.2017", "13.7.2017", "12.7.2017", 
                                             "11.7.2017", "9.7.2017", "7.7.2017", "6.7.2017", "5.7.2017", 
                                             "4.7.2017", "3.7.2017", "2.7.2017", "1.7.2017", "30.6.2017", 
                                             "29.6.2017", "28.6.2017"), variable = structure(c(1L, 1L, 1L, 
                                                                                               1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 
                                                                                               2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 
                                                                                               3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L
                                             ), .Label = c("ave_max", "ave", "lepo"), class = "factor"), 
                        value = c(69, 75, 83, 97, 101, 73, 77, 78, 98, 79, 78, 95, 
                                  70, 81, 78, 71, 72, 58, 59, 59, 58, 54, 56, 60, 60, 62, 58, 
                                  56, 63, 58, 58, 63, 58, 56, 48, 51, 51, 48, 48, 48, 52, 53, 
                                  52, 49, 48, 53, 50, 50, 54, 46, 47)), row.names = c(NA, -51L
                                  ), .Names = c("Time.data", "variable", "value"), class = "data.frame")

#Relevant part of the code based on Henrik's proposal, 
#rejected timestamp approach which output has wrongly shown x-axis label in Fig. 1
p <- ggplot(data3, aes(x = as.Date(Time.data, format = "%d.%m.%Y"), y = value, fill = variable)) + 
  geom_bar(stat='identity') + 
  theme(axis.text.x = element_text(angle = 90, hjust=1), 
        text = element_text(size=10)) +
  scale_x_discrete("Date") +
  scale_x_date(date_breaks = "2 days", date_labels = "%d.%m.%Y") 

print(p)
dev.off()

Output which I do not understand

Scale for 'x' is already present. Adding another scale for 'x', which will replace the existing scale.

Fig. 1 Output based on Henrik's proposal

Expected output: as such but with correct x-label there on the x-axis

OS: Debian 9
R: 3.4.0
RStudio: 1.0.143
Other sources: Date format for subset of ticks on time axis, scale_datetime shifts x axis, Time series plot gets offset by 2 hours if scale_x_datetime is used

解决方案

You have specified two different scales for the x axis, a discrete scale and a continuous date scale, presumably in an attempt to rename the label on the x axis. For this, xlab() can be used:

library(ggplot2)
ggplot(data3, aes(x = as.Date(Time.data, format = "%d.%m.%Y"), y = value, fill = variable)) +
  # use new geom_col() instead of  geom_bar(stat = "identity")
  # see http://ggplot2.tidyverse.org/articles/releases/ggplot2-2.2.0.html#stacking-bars
  geom_col() + 
  theme(axis.text.x = element_text(angle = 90, hjust=1), 
        text = element_text(size=10)) +
  # specify label for x axis
  xlab("Time.date") +
  scale_x_date(date_breaks = "2 days", date_labels = "%d.%m.%Y")

Alternatively, you can use the name parameter to scale_x_date():

ggplot(data3, aes(x = as.Date(Time.data, format = "%d.%m.%Y"), y = value, fill = variable)) + 
  geom_col() + 
  theme(axis.text.x = element_text(angle = 90, hjust=1), 
        text = element_text(size=10)) +
  scale_x_date(name = "Time.date", date_breaks = "2 days", date_labels = "%d.%m.%Y")

Addendum: Saving plots

If the intention is to save just one plot in a file you can add a call to ggsave() after the call to ggplot(), i.e.,

ggplot(...
ggsave("Rplots.pdf")

instead of

options(device="pdf") # https://stackoverflow.com/questions/6535927/how-do-i-prevent-rplots-pdf-from-being-generated
filename.pdf <- paste0(getwd(), "/", "Rplots", ".pdf", sep = "")
pdf(file=filename.pdf)
p <- ggplot(...
print(p)
dev.off()

According to help("ggsave")

ggsave() is a convenient function for saving a plot. It defaults to saving the last plot that you displayed, using the size of the current graphics device. It also guesses the type of graphics device from the extension.


Another issue is the creation of the file path. Instead of

filename.pdf <- paste0(getwd(), "/", "Rplots", ".pdf", sep = "")

it is better to use

filename.pdf <- file.path(getwd(), "Rplots.pdf")

which constructs the path to a file from components in a platform-independent way.

这篇关于如何在R ggplot2中使用高数组的日期日期x轴顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆