为什么我的 xlabels 在图表之外?
Why my xlabels are outside the chart?
我正在尝试使用 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 标签时:
我不知道出了什么问题,因为 '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"
))
这似乎是 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))
更新:
您可以使用 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))
我正在尝试使用 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 标签时:
我不知道出了什么问题,因为 '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"
))
这似乎是 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))
更新:
您可以使用 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))