如何只使用字符的部分值作为 x 轴
How to use only part of the value of a character for x-axis
如果不允许我添加额外的列,我如何才能select只有年份数据(图1中显示数据的第一列)出现在x轴上(参见图 2)
中的预期结果
Picture 1:the dataset
Picture 2:the desired result
非常感谢您的帮助!!!
我建议使用 ggplot2
的方法。为了获得你想要的,你可以使用 substring()
函数来提取定义开始和结束位置的前四个元素 quarter
变量。您还可以使用 factor()
来保留所有年份,而不是像 ggplot2
那样显示数字数量。这里我添加了一个小例子:
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))
输出:
如果不允许我添加额外的列,我如何才能select只有年份数据(图1中显示数据的第一列)出现在x轴上(参见图 2)
中的预期结果Picture 1:the dataset
Picture 2:the desired result
非常感谢您的帮助!!!
我建议使用 ggplot2
的方法。为了获得你想要的,你可以使用 substring()
函数来提取定义开始和结束位置的前四个元素 quarter
变量。您还可以使用 factor()
来保留所有年份,而不是像 ggplot2
那样显示数字数量。这里我添加了一个小例子:
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))
输出: