为什么我的xlabel在图表之外?

时间:2018-08-17 17:43:53

标签: r ggplot2 label zoo

我正在尝试使用ggplot在R中创建图表,并且xlabels显示在图表外部。

遵循代码:

#packages
library('readxl')
library('gridExtra')
library('scales')
library('ggplot2')
library('ggrepel')
library('zoo') 
library('ggpubr') 
#Reading data
dados <- read_excel("~/teste_dash.xlsx")
#transforming 'dados' as date
dados$Data<-as.yearmon(dados$Data)
cpl<-ggplot(dados, aes(x=Data,y = CPL))+
      ggtitle(label = "",subtitle = "CPL")+
      geom_point(aes(color=Origem),size=3,show.legend = F)+
      geom_line(aes(color=Origem),size=2,show.legend = F)+
      labs(y="",x="")+
      scale_color_manual(values = c("#1c4587", "#6d9eeb"))+
      theme_classic()+theme(axis.text.x= element_text(face = 'bold',colour = 'grey'),
      axis.line.x = element_line(colour = 'grey'),
      axis.ticks.x = element_line(colour='grey'),
      axis.text.y= element_text(face = 'bold',colour = 'grey'),
      axis.line.y = element_line(colour = 'grey'),
      axis.ticks.y = element_line(colour='grey'),
      plot.subtitle = element_text(colour = "#1c4587",face = 'bold'),
      legend.position = "top",legend.title = element_text(face = "bold",size = 12),
      legend.text =element_text( size=10))+
      scale_x_yearmon(format = '%b-%y',n=12 )

当我运行图表显示时,图表外带有x标签:

Chart with x-labels outside

我不知道出什么问题了,因为以前的数据中没有'Jul-17'。

我的数据

structure(list(Origem = c("Example 1", "Example 1", "Example 1", 
"Example 1", "Example 1", "Example 1", "Example 1", "Example 1", 
"Example 1", "Example 1", "Example 1", "Example 1", "Example 2", 
"Example 2", "Example 2", "Example 2", "Example 2", "Example 2", 
"Example 2", "Example 2", "Example 2", "Example 2", "Example 2", 
"Example 2"), Data = structure(c(1532044800, 1529452800, 1526774400, 
1524182400, 1521504000, 1519084800, 1516406400, 1513728000, 1511136000, 
1508457600, 1505865600, 1503187200, 1532044800, 1529452800, 1526774400, 
1524182400, 1521504000, 1519084800, 1516406400, 1513728000, 1511136000, 
1508457600, 1505865600, 1503187200), class = c("POSIXct", "POSIXt"
), tzone = "UTC"), CPL = c(23.11, 19.87, 17.96, 16.86, 14.44, 
11.26, 14.58, 22.24, 19.5, 19.34, 18, 14.95, 39.43, 40.96, 43.07, 
49.01, 36.53, 29.79, 24.06, 19.53, 48.46, 46.52, 43.86, 39.49
)), row.names = c(NA, -24L), class = c("tbl_df", "tbl", "data.frame"
))

1 个答案:

答案 0 :(得分:0)

这似乎是scale_x_yearmon()的问题,在ggplot2 3.0.0中对我来说很麻烦-似乎对panel.grid做了奇怪的事情,导致日期分布不均跨轴。我不能解决这个问题,但是我可以建议另一种方法。

与其将日期列转换为yearmon,而是将其转换为Date

dados$Data<-as.Date(dados$Data)

或使用dplyr

library(dplyr)
dados <- dados %>% mutate(Data = as.Date(Data))

与您之前使用scale_x_date所做的下一个绘图:

library(scales)

ggplot(dados, aes(x=Data,y = CPL))+
  ggtitle(label = "",subtitle = "CPL")+
  geom_point(aes(color=Origem),size=3,show.legend = F)+
  geom_line(aes(color=Origem),size=2,show.legend = F)+
  scale_x_date(labels = date_format('%b %y'))+
  labs(y="",x="")+
  scale_color_manual(values = c("#1c4587", "#6d9eeb"))+
  theme_classic()+theme(axis.text.x= element_text(face = 'bold',colour = 'grey'),
                        axis.line.x = element_line(colour = 'grey'),
                        axis.ticks.x = element_line(colour='grey'),
                        axis.text.y= element_text(face = 'bold',colour = 'grey'),
                        axis.line.y = element_line(colour = 'grey'),
                        axis.ticks.y = element_line(colour='grey'),
                        plot.subtitle = element_text(colour = "#1c4587",face = 'bold'),
                        legend.position = "top",legend.title = element_text(face = "bold",size = 12),
                        legend.text =element_text( size=10))

enter image description here

更新:

您可以使用scale_x_date轻松地将12个月放在图表上。一种方法是使用scales软件包和pretty_breaks(12)

scale_x_date(labels = date_format('%b %y'),breaks = pretty_breaks(12))

这会绘制数据准确的日期确切。如果您只想按数据点的月份和年份绘制数据,则解决方法是使用以下格式格式化数据:

dados$Data <- as.Date(as.yearmon(dados$Data))