[英]How do I change the tick labels of x and y-axis in ggplot2, axis disappearing
[英]How to rearrange string X axis tick labels in ggplot2
我正在尝试绘制行程长度分布图(对于距离每增加10英里,我想找出该特定年份该箱中的行程百分比)。 当我将其绘制在ggplot2中时,我的X轴刻度标签按字母顺序排列,而不是按距离递增的顺序排列。 我尝试使用建议的各种技巧( 更改离散x比例的顺序 ),但没有任何进展。 一个链接My code在下面,数据集在这里( http://goo.gl/W1jjfL )。
library(ggplot2)
library(reshape2)
nwpt <- subset(nonwork, select=c(Distance, PersonTrips1995, PersonTrips2001, PersonTrips2009))
nwpt <- melt(nwpt, id.vars="Distance")
ggplot(data=nwpt, aes(x=Distance, y=value, group=variable, colour=variable)) + scale_x_discrete(name="Distance") + geom_line(size=0.5) + ggtitle("Non Work Person Trips") + ylab("Percent")
我检查了Distance变量是否是一个因子,如下所示:
is.factor(nwpt $ Distance) 1是
但是,我得到的输出不是我想要的。 而不是低于10英里是第一类,接下来是10-14英里,等等。我得到如下图所示的图(此处为PDF: http : //goo.gl/V7yvxT )。
任何帮助表示赞赏。 TIA克里希南
这是一种方法:
library(ggplot2)
library(reshape2)
nwpt <- subset(nonwork,
select=c(DID,Distance,PersonTrips1995,PersonTrips2001,PersonTrips2009))
nwpt <- melt(nwpt, id.vars=c("DID","Distance"))
ggplot(data=nwpt, aes(x=DID, y=value, colour=variable)) +
geom_line(size=0.5) +
labs(title="Non Work Person Trips", y="Percent") +
scale_x_discrete(name="Distance", labels=nwpt$Distance) +
theme(axis.text.x=element_text(angle=90))
用您的数据集产生此结果:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.