[英]How to use only part of the value of a character for x-axis
If I am not allowed to add an additional column, how can I select only the year data(the first column of the shown data in picture 1) to appear on the x-axis (see the desired result in picture 2)如果不允许添加附加列,如何仅选择年份数据(图 1 中显示数据的第一列)出现在 x 轴上(参见图 2 中的所需结果)
Picture 2:the desired result图2:想要的结果
Your help is much appreciated!!!非常感谢您的帮助!!!
I would suggest an approach using ggplot2
.我建议使用
ggplot2
的方法。 In order to obtain what you want, you can use substring()
function to extract the first four elements defining start and end position over your quarter
variable.为了获得您想要的内容,您可以使用
substring()
函数来提取定义quarter
变量的开始和结束位置的前四个元素。 You can also use factor()
to keep all years instead of showing numeric quantities as ggplot2
uses to do.您还可以使用
factor()
来保留所有年份,而不是像ggplot2
那样显示数字数量。 Here I have added a small example:这里我添加了一个小例子:
library(ggplot2)
#Data
df <- data.frame(quarter=paste0(rep(c(1975:1980),each=4),'-','Q',rep(1:4,6)),
v2=seq(0,100,length.out = 24),stringsAsFactors = F)
#Plot
ggplot(df,aes(x=factor(substring(quarter,1,4)),y=v2,group=1))+
geom_line()+
xlab('year')+
theme_bw()+
theme(axis.text.x = element_text(angle=90))
Output:输出:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.