本文介绍了更改x轴上日期的ggplot2栏的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下数据,并试图在R中创建一个带ggplot2的barplot,其中有与日期值相关的值
conv = c(10,4.76,17.14,25.26.47,37.5,20.83,25.53,32.5,16.7,27.33)
click = c(20,42,35,28,34,48,48,47, (日期= c(7月7日,7月8日,7月9日,7月10日,7月11日 ,7月12日,7月13日,
7月14日,7月15日,7月16日,7月17日),click = c(click),conv = c(conv))
dat
但是,当我运行以下命令时,条形图不在
library(ggplot2)
ggplot(dat,aes(date,conv))+ geom_bar(fill =#336699)+ ylim(c(0,50))+
opts(title =)+
opts(axis.text.y = theme_text(family =sans,face =bold,size = 10))+
opts(axis.text.x = theme_text(family =sans,face =bold,size = 8))+
opts .title = theme_text(size = 15,f ())
$ p>可变日期从7月7日到7月17日正确订购,不知道为什么ggplot2有这个问题。有没有快速的功能来解决这个问题,而不必重新排序原始数据集中的数据。排序顺序不起作用是因为你有一个字符串,而不是日期。您的最佳选择是将日期转换为日期格式:
dat $ date< - as.Date(paste(dat $日期,2011),format =%b%d%Y)
ggplot(dat,aes(as.character(date),conv))+ geom_bar(fill =#336699 )+
ylim(c(0,50))+
opts(title =)+
opts(axis.text.y = theme_text(family =sans,face =bold,size = 10))+
opts(axis.text.x = theme_text(family =sans,face =bold,size = 8,angle = 90))+
opts(plot.title = theme_text(size = 15,face =bold))+
xlab()+ ylab()
I have the following data and am trying to create a barplot in R with ggplot2 which have values associated with date values
conv = c(10, 4.76, 17.14, 25, 26.47, 37.5, 20.83, 25.53, 32.5, 16.7, 27.33)
click = c(20, 42, 35, 28, 34, 48, 48, 47, 40, 30, 30)
dat <- data.frame(date=c("July 7", "July 8", "July 9", "July 10", "July 11", "July 12", "July 13",
"July 14", "July 15", "July 16", "July 17"), click=c(click), conv=c(conv))
dat
However, when I run the following commands, the bars aren't in the proper order.
library(ggplot2)
ggplot(dat, aes(date, conv)) + geom_bar(fill="#336699") + ylim(c(0,50)) +
opts(title="") +
opts(axis.text.y=theme_text(family="sans", face="bold", size=10)) +
opts(axis.text.x=theme_text(family="sans", face="bold", size=8)) +
opts(plot.title = theme_text(size=15, face="bold")) +
xlab("") + ylab("")
The variable date is properly ordered from July 7 to July 17, and don't know why ggplot2 has a problem with this. Is there a quick function to fix this problem without having to reorder the data in the original data set.
解决方案 The reason that your sort order does not work is because you have a character string, not dates. Your best option is to convert your dates into date format:
dat$date <- as.Date(paste(dat$date, "2011"), format="%b %d %Y")
ggplot(dat, aes(as.character(date), conv)) + geom_bar(fill="#336699") +
ylim(c(0,50)) +
opts(title="") +
opts(axis.text.y=theme_text(family="sans", face="bold", size=10)) +
opts(axis.text.x=theme_text(family="sans", face="bold", size=8, angle=90)) +
opts(plot.title = theme_text(size=15, face="bold")) +
xlab("") + ylab("")
这篇关于更改x轴上日期的ggplot2栏的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!