R中日期直方图的轴颜色

时间:2022-02-23 21:18:35

I have successfullly created a histogram using a date field.

我使用日期字段成功创建了直方图。

hist(df.sat$created_at, breaks="hours", freq=T, xlab="Time", 
  main="Sat  Volume")

My issue is that when I attempt to fill in the bars using col="red" both the bars and both the x/y axes change to red, when I only want the bars. What is the best way way only fill in the bars?

我的问题是当我尝试使用col =“red”填充条形时,当我只想要条形时,条形和两个x / y轴都变为红色。只填充酒吧的最佳方式是什么?

R中日期直方图的轴颜色

Here are some data:

以下是一些数据:

    > dput(df.sat$created_at[sample(c(1:9000), 50)])
structure(list(sec = c(41, 3, 13, 11, 49, 55, 19, 21, 6, 15, 
54, 45, 45, 39, 50, 27, 35, 25, 22, 35, 42, 31, 45, 29, 1, 3, 
8, 47, 38, 2, 13, 29, 34, 42, 15, 19, 3, 39, 41, 12, 34, 50, 
15, 27, 0, 29, 47, 26, 21, 5), min = c(46L, 38L, 4L, 35L, 26L, 
56L, 9L, 52L, 51L, 15L, 49L, 3L, 41L, 59L, 30L, 30L, 30L, 53L, 
25L, 51L, 23L, 38L, 30L, 3L, 43L, 33L, 36L, 52L, 0L, 21L, 27L, 
22L, 51L, 31L, 0L, 37L, 3L, 2L, 12L, 3L, 45L, 13L, 59L, 10L, 
11L, 7L, 41L, 21L, 5L, 20L), hour = c(14L, 16L, 18L, 15L, 15L, 
16L, 16L, 18L, 18L, 13L, 18L, 16L, 14L, 13L, 16L, 15L, 18L, 17L, 
18L, 18L, 16L, 17L, 17L, 19L, 15L, 18L, 17L, 18L, 19L, 17L, 16L, 
17L, 18L, 20L, 18L, 15L, 14L, 14L, 18L, 18L, 19L, 19L, 16L, 15L, 
17L, 17L, 15L, 17L, 17L, 17L), mday = c(9L, 9L, 9L, 9L, 9L, 9L, 
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L), mon = c(3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L), year = c(111L, 111L, 111L, 111L, 111L, 111L, 111L, 111L, 
111L, 111L, 111L, 111L, 111L, 111L, 111L, 111L, 111L, 111L, 111L, 
111L, 111L, 111L, 111L, 111L, 111L, 111L, 111L, 111L, 111L, 111L, 
111L, 111L, 111L, 111L, 111L, 111L, 111L, 111L, 111L, 111L, 111L, 
111L, 111L, 111L, 111L, 111L, 111L, 111L, 111L, 111L), wday = c(6L, 
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L), yday = c(98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 
98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 
98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 
98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 
98L), isdst = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L)), .Names = c("sec", "min", "hour", "mday", 
"mon", "year", "wday", "yday", "isdst"), class = c("POSIXlt", 
"POSIXt"), tzone = c("America/New_York", "EST", "EDT"))

2 个解决方案

#1


6  

You'll have to get around it a bit by plotting the histogram first and the axes later :

你必须首先绘制直方图并稍后绘制轴来绕过它:

hist(Data, breaks="hours", freq=T, xlab="Time", col="red",
  main="Sat  Volume",axes=F)
Axis(Data,col="black",side=1)
axis(2,col="black")

Reason to use the generic Axis(), is that it takes into account that your variable is a TimeDate class. The default axis() doesnt.

使用泛型Axis()的原因是它考虑到您的变量是TimeDate类。默认轴()不是。


EDIT :

FYI, this behaviour is only to be seen with histograms where DateTime classes are used on the X axis. The default hist() function doesn't change the color of the axis when using a fill color for the bars.

仅供参考,这种行为只能在直方图中看到,其中在X轴上使用DateTime类。使用条形的填充颜色时,默认的hist()函数不会更改轴的颜色。

#2


4  

Plot the histogram without axes and then add them in later:

绘制没有轴的直方图,然后在以后添加它们:

hist(dat, breaks="hours", freq=TRUE, col = "red", axes = FALSE)
axis.POSIXct(side = 1, dat)
axis(2)

R中日期直方图的轴颜色

#1


6  

You'll have to get around it a bit by plotting the histogram first and the axes later :

你必须首先绘制直方图并稍后绘制轴来绕过它:

hist(Data, breaks="hours", freq=T, xlab="Time", col="red",
  main="Sat  Volume",axes=F)
Axis(Data,col="black",side=1)
axis(2,col="black")

Reason to use the generic Axis(), is that it takes into account that your variable is a TimeDate class. The default axis() doesnt.

使用泛型Axis()的原因是它考虑到您的变量是TimeDate类。默认轴()不是。


EDIT :

FYI, this behaviour is only to be seen with histograms where DateTime classes are used on the X axis. The default hist() function doesn't change the color of the axis when using a fill color for the bars.

仅供参考,这种行为只能在直方图中看到,其中在X轴上使用DateTime类。使用条形的填充颜色时,默认的hist()函数不会更改轴的颜色。

#2


4  

Plot the histogram without axes and then add them in later:

绘制没有轴的直方图,然后在以后添加它们:

hist(dat, breaks="hours", freq=TRUE, col = "red", axes = FALSE)
axis.POSIXct(side = 1, dat)
axis(2)

R中日期直方图的轴颜色