在ggplot2和R中打破scale_x_date [英] Breaks for scale_x_date in ggplot2 and R

查看:430
本文介绍了在ggplot2和R中打破scale_x_date的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在绘制ggplot2中的值〜日期(在R中)。我有以下代码。正如你所看到的,ggplot2在我的数据中所增加的x轴上添加了更多的中断。我只想每次在我的数据框中有一个数据点时都有x标签。我如何强制ggplot2仅在my.dates的值处显示中断? scale_x_date

  require(ggplot2)
my.dates = as.Date (c(2011-07-22,2011-07-23,
2011-07-24,2011-07-28,2011-07-29))
my.vals = c(5,6,8,7,3)
my.data< - data.frame(date = my.dates,vals = my.vals)
plot( my.dates,my.vals)
p <-ggplot(data = my.data,aes(date,vals))+ geom_line(size = 1.5)
p <-p + scale_x_date(format = %m /%d,')
p

解决方案

One方法是将x轴视为数字,并使用 scale_x_continuous()设置分隔符和标签美学。

  ggplot(my.data,aes(as.numeric(date),vals))+ 
geom_line(size = 1.5)+
scale_x_continuous(breaks = as。数字(my.data $ date)
,labels = format(my.data $ da te,format =%m /%d))

7/28在我看来有点奇怪。但是,我认为这就是你想要的?如果我误解了,请告诉我。

编辑



,我并不满意上面的突破方式,特别是背景中的灰色网格。这里有一种方法来维护矩形网格并只标记我们有数据的点。你可以在ggplot调用中完成这一切,但我认为在ggplot之外进行处理更容易。首先,创建一个包含与日期对应的数字序列的向量。然后,我们将更新适当的标签,并用替换NA条目,以防止这些条目在x轴上绘制任何内容:

  xscale < -  data.frame(breaks = seq(min(as.numeric(my.data $ date)),max(as.numeric my.data $ date)))
,labels = NA)

xscale $ labels [xscale $ breaks%in as.numeric(my.data $ date)]< - format (my.data $ date,format =%m /%d)
xscale $ labels [is.na(xscale $ labels)]< -



这给我们看起来像这样的东西:

 打破标签
1 15177 07/22
2 15178 07/23
3 15179 07/24
4 15180
5 15181
6 15182
7 15183 07/28
8 15184 07/29

然后可以传递给这样的比例:



scale_x_continuous(breaks = xscale $ breaks,labels = xscale $ labels)


I am plotting value~date in ggplot2 (in R). I have the following code. As you see ggplot2 adds more breaks on the x-axis that I have in my data. I just want to have the x-label everytime I have a data point in my data frame. How can I force ggplot2 to just show the breaks only at the values of my.dates? It seems there is no "breaks" argument for scale_x_date

require(ggplot2)
my.dates = as.Date(c("2011-07-22","2011-07-23",
                     "2011-07-24","2011-07-28","2011-07-29"))
my.vals  = c(5,6,8,7,3)
my.data <- data.frame(date =my.dates, vals = my.vals)
plot(my.dates, my.vals)
p <- ggplot(data = my.data, aes(date,vals))+ geom_line(size = 1.5)
p <- p + scale_x_date(format="%m/%d", ' ')
p

解决方案

One approach would be to treat the x-axis as numeric and set the breaks and labels aesthetics with scale_x_continuous().

ggplot(my.data, aes(as.numeric(date), vals)) + 
  geom_line(size = 1.5) +
  scale_x_continuous(breaks = as.numeric(my.data$date)
                     , labels = format(my.data$date, format = "%m/%d"))

though the break between 7/24 through 7/28 looks a bit strange in my opinion. However, I think that's what you want? Let me know if I've misinterpreted.

EDIT

As noted above, I wasn't thrilled with the way the breaks looked above, specifically with the gray grid in the background. Here's one way to maintain the rectangular grid and to only label the points where we have data. You could do this all within the ggplot call, but I think it's easier to do the processing outside of ggplot. First, create a vector that contains the sequence of numbers corresponding to the dates. Then we'll update the appropriate labels and replace the NA entries with " " to prevent anything from being plotted on the x-axis for those entries:

xscale <- data.frame(breaks = seq(min(as.numeric(my.data$date)), max(as.numeric(my.data$date)))
                      , labels = NA)

xscale$labels[xscale$breaks %in% as.numeric(my.data$date)] <- format(my.data$date, format = "%m/%d") 
xscale$labels[is.na(xscale$labels)] <- " "

This gives us something that looks like:

  breaks labels
1  15177  07/22
2  15178  07/23
3  15179  07/24
4  15180       
5  15181       
6  15182       
7  15183  07/28
8  15184  07/29

which can then be passed to the scale like this:

scale_x_continuous(breaks = xscale$breaks, labels = xscale$labels)

这篇关于在ggplot2和R中打破scale_x_date的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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