简体   繁体   中英

R: Altering the scale of the x-axis in ggplot

guys I have used the code:

Combine_mean %>% ggplot(aes(x=factor(Year), y=Length, colour=gender, group=gender)) + geom_line()

Which has produce a comprehensive graph, x-axis is however cluttered, listing every year from 1674 to 1834. I would prefer to display this in 10 year increments, would I need to adjust my initial data frame and so the results for the lines for both male and female or could this be done through editing the code above.

我附上了一张图表的图片作为上下文。

This should alleviate the issue (I have used dummy data):

library(dplyr)
library(ggplot2)
#Data
Combine_mean <- data.frame(Year=c(seq(1674,1834),seq(1674,1834)),
                           gender=c(rep('female',161),rep('male',161)),
                           Length=c(cumsum(rnorm(161)),cumsum(rnorm(161))))
#Code
Combine_mean %>% 
  ggplot(aes(x=factor(Year), y=Length, colour=gender, group=gender)) +
  geom_line()+
  scale_x_discrete(limits=as.character(seq(1674,1834,by=10)))

Output:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM