简体   繁体   English

不同年份类别的ggplot

[英]ggplot for different year categories

I am really struggling with the plots in R. I have to make a plot of logmx versus the age of males in different years in the same plot for males. 我真的在R中的绘图上苦苦挣扎。我必须在同一绘图中针对男性,绘制logmx与不同年份中的男性年龄的绘图。 In this plot there must be 4 lines for "1870-1879", "1940-1949", "1960-1969", "1980-1989" I made 4 differents subsets with these specific years and tried to combine them but I am new in R and I do not know what I am doing wrong. 在该图中,必须有4行代表“ 1870-1879”,“ 1940-1949”,“ 1960-1969”,“ 1980-1989”,我将这些特定年份划分为4个不同的子集,并尝试将它们组合起来,但我是新手在R中,我不知道我在做什么错。 I saw lots of similar answers but I could not solve it.I need to make it with ggplot2 package. 我看到了很多类似的答案,但我无法解决它。我需要用ggplot2软件包来制作它。

males11<-males[445:1555,1:3]
males12<-males[4885:5995, 1:3]
males13<-males[9325:10435, 1:3]
males14<-males[13765:14653, 1:3] #for the subsets males14<-males[13765:14653, 1:3] #for子集

d1 <- data.frame(males11$Age, log(males11$mx), males11$Year)
d2 <- data.frame(males12$Age, log(males12$mx), males12$Year)
d3 <- data.frame(males13$Age, log(males13$mx), males13$Year)
d4 <- data.frame(males14$Age, log(males14$mx), males14$Year)

ggplot()
+ geom_line(aes(males11$Age, log(males11$mx), colour=males11$Year), d1) + + geom_line(aes(males11$Age, log(males11$mx), colour=males11$Year), d1) +
geom_line(aes(males12$Age, log(males12$mx), colour=males12$Year), d2) + geom_line(aes(males12$Age, log(males12$mx), colour=males12$Year), d2) +
geom_line(aes(males13$Age, log(males13$mx), colour=males13$Year), d3) + geom_line(aes(males13$Age, log(males13$mx), colour=males13$Year), d3) +
geom_line(aes(males14$Age, log(males14$mx), colour=males14$Year), d4)

You should build a factor in your original data frame and group by that, similar to this: 您应该在原始数据框架中建立一个因子并以此分组,类似于以下内容:

males$group <- cut(males$Year, 
                   breaks=seq(1799, 2099, 10), 
                   dig.lab=4)
library(ggplot2)
ggplot(males[males$group %in% c("(1869-1879]", "(1939-1949]", "(1959-1969]", "(1979-1989]"), ], 
       aes(Age, mx, colour=group)) + 
  geom_line() + 
  scale_y_log10()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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