如何在 ggplot2 (geom_area) 中使 x 轴上的日期等距并将日期格式设置为“%d %b %Y”
How to make dates in x-axis equidistant in ggplot2 (geom_area) and format date as "%d %b %Y"
我经常遇到一个让我抓狂的问题...我正在使用“'geom_area'”绘制一个 ggplot2,并将我的 x 轴作为日期。我试图将日期分隔成彼此之间的相等距离,但我看不到如何......我正在附加我的虚拟数据。第一个图很好地绘制了我的数据,但如果日期彼此接近,我的日期就会聚集在一起。我想让它们像“选项 2”中那样等距,但是“date_sr”不会绘制我的百分比信息。
非常感谢您提供的任何帮助。
require(ggplot2)
library(reshape2)
library(RColorBrewer)
sex <- c('F','F','F',
'M','M','M')
date <- c("26/11/2018","08/02/2020","08/09/2020",
"26/11/2018","08/02/2020","08/09/2020")
percentage <- c(40, 30, 20, 60, 70, 80)
df <- data.frame(sex, date, percentage)
print(df)
#option 1
df$date<- as.Date(df$date,format="%d/%m/%Y")
ourdates<-(unique(df$date))
df
area1 <- ggplot(df, aes(date, percentage,fill=sex)) +
geom_area()+
scale_y_continuous(breaks = seq(0,100,10))+
scale_x_date(breaks = ourdates, date_labels = "%d %b %Y")+
scale_fill_brewer(labels=c("Female","Male"),palette ="Paired")
plot(area1)
#option 2
df$date<- as.Date(df$date,format="%d/%m/%Y")
mydate<-format(df$date, "%d %b %Y")
date_sr<-factor(mydate, levels = rev(unique(mydate)),ordered = TRUE)
#if we do not re-define date_sr as date it won't plot the graph (but then it won't plot the date in the correct format)
#date_sr<-as.Date(df$date, format="%d/%b/%Y")
area2<-ggplot(df,aes(fill=sex,y=percentage,x=date_sr))+
geom_area()+
scale_y_continuous(breaks = seq(0,100,10))+
scale_fill_brewer(labels=c("Female","Male"),palette ="Paired")
plot(area2)
geom_area 绘制男女性别比例。注意 08/Feb/2020 更接近 08/Sep/2020。我希望 3 个日期彼此等距绘制,并将日期格式设置为“%d %b %Y”。
这似乎很棘手。 geom_area
当 x 是一个因子时不绘制。
但是,如果您想要等距日期,我们可以使用 rank
.
sex <- c('F','F','F',
'M','M','M')
date <- c("26/11/2018","08/02/2020","08/09/2020",
"26/11/2018","08/02/2020","08/09/2020")
percentage <- c(40, 30, 20, 60, 70, 80)
df <- data.frame(sex,
as.Date(date, format = "%d/%m/%Y"),
percentage)
area1 <- ggplot(df, aes(rank(date), percentage,fill=sex)) +
geom_area()+
scale_y_continuous(breaks = seq(0,100,10))+
scale_x_continuous(breaks = rank(df$date),
labels = format(df$date, "%d/%m/%Y")) +
scale_fill_brewer(labels=c("Female","Male"),palette ="Paired")
plot(area1)
我经常遇到一个让我抓狂的问题...我正在使用“'geom_area'”绘制一个 ggplot2,并将我的 x 轴作为日期。我试图将日期分隔成彼此之间的相等距离,但我看不到如何......我正在附加我的虚拟数据。第一个图很好地绘制了我的数据,但如果日期彼此接近,我的日期就会聚集在一起。我想让它们像“选项 2”中那样等距,但是“date_sr”不会绘制我的百分比信息。
非常感谢您提供的任何帮助。
require(ggplot2)
library(reshape2)
library(RColorBrewer)
sex <- c('F','F','F',
'M','M','M')
date <- c("26/11/2018","08/02/2020","08/09/2020",
"26/11/2018","08/02/2020","08/09/2020")
percentage <- c(40, 30, 20, 60, 70, 80)
df <- data.frame(sex, date, percentage)
print(df)
#option 1
df$date<- as.Date(df$date,format="%d/%m/%Y")
ourdates<-(unique(df$date))
df
area1 <- ggplot(df, aes(date, percentage,fill=sex)) +
geom_area()+
scale_y_continuous(breaks = seq(0,100,10))+
scale_x_date(breaks = ourdates, date_labels = "%d %b %Y")+
scale_fill_brewer(labels=c("Female","Male"),palette ="Paired")
plot(area1)
#option 2
df$date<- as.Date(df$date,format="%d/%m/%Y")
mydate<-format(df$date, "%d %b %Y")
date_sr<-factor(mydate, levels = rev(unique(mydate)),ordered = TRUE)
#if we do not re-define date_sr as date it won't plot the graph (but then it won't plot the date in the correct format)
#date_sr<-as.Date(df$date, format="%d/%b/%Y")
area2<-ggplot(df,aes(fill=sex,y=percentage,x=date_sr))+
geom_area()+
scale_y_continuous(breaks = seq(0,100,10))+
scale_fill_brewer(labels=c("Female","Male"),palette ="Paired")
plot(area2)
geom_area 绘制男女性别比例。注意 08/Feb/2020 更接近 08/Sep/2020。我希望 3 个日期彼此等距绘制,并将日期格式设置为“%d %b %Y”。
这似乎很棘手。 geom_area
当 x 是一个因子时不绘制。
但是,如果您想要等距日期,我们可以使用 rank
.
sex <- c('F','F','F',
'M','M','M')
date <- c("26/11/2018","08/02/2020","08/09/2020",
"26/11/2018","08/02/2020","08/09/2020")
percentage <- c(40, 30, 20, 60, 70, 80)
df <- data.frame(sex,
as.Date(date, format = "%d/%m/%Y"),
percentage)
area1 <- ggplot(df, aes(rank(date), percentage,fill=sex)) +
geom_area()+
scale_y_continuous(breaks = seq(0,100,10))+
scale_x_continuous(breaks = rank(df$date),
labels = format(df$date, "%d/%m/%Y")) +
scale_fill_brewer(labels=c("Female","Male"),palette ="Paired")
plot(area1)