简体   繁体   English

ggplot删除特定的x轴标签

[英]ggplot delete specific x-axis labels

library(tidyverse)

df <- data.frame(date = as.Date(c("2017-12-01", "2018-01-01", "2018-02-01", 
                                  "2018-03-01", "2018-04-01", "2018-05-01", 
                                  "2018-06-01", "2018-07-01", "2018-08-01", 
                                  "2018-09-01", "2018-10-01", "2018-11-01")), 
                 value = c(0.567859562, 0.514907158, 0.035399304, 0.485728823, 
                           0.925127361, 0.237531067, 0.301930968, 0.133373326, 
                           0.082275426, 0.464255614, 0.2366749, 0.652084264))

ggplot(df, aes(date, value)) + 
  geom_col() + 
  scale_x_date(date_breaks = "1 month", 
                 date_labels = "%b") + 
  theme(axis.text.x = element_text(angle = 90, vjust = 0.3))

I want to maintain my plot shown below, exactly as is, with two exceptions. 我想保持下面所示的情节,完全一样,有两个例外。 I want to remove the first Nov on the x-axis label and the last Dec on the x-axis label. 我想删除x轴标签上的第一个Nov和x轴标签上的最后一个Dec I added coord_cartesian(xlim = as.Date(c("2017-12-01", "2018-11-01"))) to my code chunk above, but this eliminates the 'blank space' padding at either end of my x-axis. 我在上面的代码块中添加了coord_cartesian(xlim = as.Date(c("2017-12-01", "2018-11-01"))) ,但这消除了我的两端的'空格'填充x轴上。

How do I simply tell ggplot to delete the text of the first and last x-axis labels? 如何告诉ggplot删除第一个和最后一个x轴标签的文本? This would be the first Nov and the last Dec . 这将是Nov的第一个和去年Dec Note that these do not exists in my df data frame at all so dplyr filters probably won't work. 请注意 ,这些在我的df数据框中根本不存在,因此dplyr过滤器可能不起作用。

在此输入图像描述

You could achieve what you want by setting breaks using seq.date: 你可以通过使用seq.date设置中断来实现你想要的:

library(tidyverse);library(lubridate)

df <- data.frame(date = as.Date(c("2017-12-01", "2018-01-01", "2018-02-01", 
                                  "2018-03-01", "2018-04-01", "2018-05-01", 
                                  "2018-06-01", "2018-07-01", "2018-08-01", 
                                  "2018-09-01", "2018-10-01", "2018-11-01")), 
                 value = c(0.567859562, 0.514907158, 0.035399304, 0.485728823, 
                           0.925127361, 0.237531067, 0.301930968, 0.133373326, 
                           0.082275426, 0.464255614, 0.2366749, 0.652084264))



ggplot(df, aes(date, value)) + 
  geom_col() + 
  scale_x_date(
               date_labels = "%b",
               breaks = seq.Date(ymd("2017-12-01"),ymd("2018-11-01"), by = "month")) + 
  theme(axis.text.x = element_text(angle = 90, vjust = 0.3))

在此输入图像描述

I think this is what you want. 我想这就是你想要的。 The date_breaks are unnecessary. date_breaks是不必要的。

ggplot(df, aes(date, value)) + 
  geom_col() + 
  scale_x_date(date_labels = "%b", breaks = df$date) + 
  theme(axis.text.x = element_text(angle = 90, vjust = 0.3))

结果情节

我建议在R中研究Lubridate软件包 - 你可以将任何凌乱的日期值转换为适当的POSIXT格式,并且还可以非常容易地提取月份信息 - 您还可以将这些日期转换为实际月份的单个列,并使用作为你的轴标签,这是更清洁,因为你会有一个额外的相应月份的列 - 你也可以根据那个月填写并做一些其他很酷的东西!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM