我怎样才能自己把数字放在y轴的x轴上?
How can I put number in x-axis of y-axis by myself?
y <- c(2, 15, 8, 20,3,4, 7)
x <- c(2015, 2016, 2017, 2018, 2019, 2020, 2021)
dat <-data.frame(x,y)
p<-ggplot(dat, aes(x,y, label=y))+ geom_line()+xlab("") + ylab("")+
theme(axis.text.y = element_text(face='bold',size=24),axis.text.x= element_text(face='bold', size=20))+
scale_y_continuous("", limits=c(0,30))+xlab("")+theme_bw()+geom_text(vjust=-1, colour="purple")
我运行这段代码后,x轴只显示2016、2018、2020。我想将所有 x <- c(2015, 2016, 2017, 2018, 2019, 2020, 2021)
写入图表。
我应该使用哪个命令?在 y 轴上,我想显示 0,5,10,15,20,25,20 而不是 0,10,20,30。
要扩展 Gregor 的评论,您需要在 scale
函数中使用 breaks
命令,例如 scale_x_continuous
:
scale_x_continuous(breaks = 2015:2021)
所以你的 plot 命令是
p + scale_x_continuous(breaks = 2015:2021) +
scale_y_continuous(breaks = seq(0,20, by = 5))
y <- c(2, 15, 8, 20,3,4, 7)
x <- c(2015, 2016, 2017, 2018, 2019, 2020, 2021)
dat <-data.frame(x,y)
p<-ggplot(dat, aes(x,y, label=y))+ geom_line()+xlab("") + ylab("")+
theme(axis.text.y = element_text(face='bold',size=24),axis.text.x= element_text(face='bold', size=20))+
scale_y_continuous("", limits=c(0,30))+xlab("")+theme_bw()+geom_text(vjust=-1, colour="purple")
我运行这段代码后,x轴只显示2016、2018、2020。我想将所有 x <- c(2015, 2016, 2017, 2018, 2019, 2020, 2021)
写入图表。
我应该使用哪个命令?在 y 轴上,我想显示 0,5,10,15,20,25,20 而不是 0,10,20,30。
要扩展 Gregor 的评论,您需要在 scale
函数中使用 breaks
命令,例如 scale_x_continuous
:
scale_x_continuous(breaks = 2015:2021)
所以你的 plot 命令是
p + scale_x_continuous(breaks = 2015:2021) +
scale_y_continuous(breaks = seq(0,20, by = 5))