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

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

问题描述

我正在考虑如何将 tall 数组格式的字符串日期数据转换为 Date 并通过 scale_x_date 在 x 轴上通过它组织 ggplot代码>.受 Henrik 提议启发的伪代码

  1. 将字符串数据格式改为as.Date,可能类似于ggplot的参数x = ...

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

  2. ggplot 中应用 scale_x_datedate_breaks("2 day")

带有虚拟数据的代码data3

库(ggplot2")# 对于 RStudiooptions(device="pdf") # https://stackoverflow.com/questions/6535927/how-do-i-prevent-rplots-pdf-from-being-generated文件名.pdf <- paste0(getwd(), "/", "Rplots", ".pdf", sep = "")pdf(文件=文件名.pdf)# 虚拟数据data3 <- 结构(列表(时间.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"), 变量 = 结构(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"),值 = 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")#相关部分代码基于Henrik的提议,#rejected 时间戳方法,其输出错误地显示了图 1 中的 x 轴标签p <- ggplot(data3, aes(x = as.Date(Time.data, format = "%d.%m.%Y"), y = value, fill = variable)) +geom_bar(stat='身份') +主题(axis.text.x = element_text(角度 = 90,hjust=1),文本 = element_text(size=10)) +scale_x_discrete("日期") +scale_x_date(date_breaks = "2 天", date_labels = "%d.%m.%Y")打印(p)dev.off()

我不明白的输出

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

图.1 基于 Henrik 提议的输出

预期输出:同样,但在 x 轴上有正确的 x-label

操作系统:Debian 9
R:3.4.0
RStudio:1.0.143
其他来源:

或者,您可以将 name 参数用于 scale_x_date():

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

附录:保存地块

如果打算只在文件中保存一个绘图,您可以在调用 ggplot() 之后添加对 ggsave() 的调用,即,p>

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

而不是

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

根据help("ggsave")

<块引用>

ggsave() 是一个保存绘图的便捷函数.它默认为保存您显示的最后一个绘图,使用当前的大小图形设备.它还猜测图形设备的类型扩展名.

<小时>

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

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 Date x 轴顺序中使用这个 tall 数组的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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