本文介绍了ggplot逐年比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我想制作一张比较不同年份获得的数据的图。我们的目标是在X轴上 12个月,并用每个年份的值绘制不同的行。 我使用的是下一个数据框: 月份Marg Fiscal.Year 1 2009- 04-01 20904494 2009 2 2009-05-01 43301981 2009 3 2009-06-01 14004552 2009 ... 38 2012-05-01 58343271 2012 39 2012-06-01 38723765 2012 40 2012-07-01 77246753 2012 我的剧情代码是: g geom_line()+ geom_point()+ geom_smooth(method =loess)+ scale_x_date(breaks =1 month ,labels = date_format(%b)); 但是这个图表在2009年到2012年的 x轴48个月和一行,其中显示每个值(每个月的每年不会有不同的行)。 请帮忙我解决这个问题? (我是新的R,任何帮助将不胜感激)。 感谢您的回答! #示例数据 data< - read.table(text =Month Marg Fiscal 。年 2009-01-01 20904494 2009 2009-02-01 30904494 2009 2009-03-01 40904494 2009 2009-04-01 30904494 2009 2009-05-01 43301981 2009 2009-06-01 14004552 2009 2009-07-01 24004552 2009 2009-08-01 34004552 2009 2009-09-01 44004552 2009 2009-10-01 54004552 2009 2009-11-01 64004552 2009 2009-12-01 44004552 2009 2012-02-01 58343271 2012 2012-03-01 68343271 2012 2012-04-01 58343271 2012 2012-05-01 58343271 2012 2012-06-01 38723765 2012 2012-07-01 77246753 2012, header = TRUE,sep =,nrows = 18)数据$ MonthN 数据$月份 $ bg geom_line()+ geom_point()+ scale_x_discrete(breaks =数据$ MonthN,labels = data $ Month)g I want to produce one plot comparing data obtained in different years. The goal is having 12 months on the x axis and drawing different lines with the values for each of the years.The data frame I'm using is the next one: Month Marg Fiscal.Year1 2009-04-01 20904494 20092 2009-05-01 43301981 20093 2009-06-01 14004552 2009...38 2012-05-01 58343271 201239 2012-06-01 38723765 201240 2012-07-01 77246753 2012My code of the plot is:g <- ggplot(data = data, stat="identity", aes(x = Month, y = Marg)) + geom_line() + geom_point() + geom_smooth(method = "loess") + scale_x_date(breaks = "1 month", labels = date_format("%b"));but this plots in the x axis 48 months from 2009 to 2012, and one line in it showing each of the values (not different lines for each year for each of the months).Could you please help me solving this issue? (I'm new with R, any help will be appreciated).Thanks in advance for your answer! 解决方案 library(ggplot2)# Sample datadata <- read.table(text = "Month Marg Fiscal.Year 2009-01-01 20904494 2009 2009-02-01 30904494 2009 2009-03-01 40904494 2009 2009-04-01 30904494 2009 2009-05-01 43301981 2009 2009-06-01 14004552 2009 2009-07-01 24004552 2009 2009-08-01 34004552 2009 2009-09-01 44004552 2009 2009-10-01 54004552 2009 2009-11-01 64004552 2009 2009-12-01 44004552 2009 2012-02-01 58343271 2012 2012-03-01 68343271 2012 2012-04-01 58343271 2012 2012-05-01 58343271 2012 2012-06-01 38723765 2012 2012-07-01 77246753 2012", header=TRUE, sep="", nrows=18)data$MonthN <- as.numeric(format(as.Date(data$Month),"%m")) # Month's numberdata$Month <- months(as.Date(data$Month), abbreviate=TRUE) # Month's abbr.g <- ggplot(data = data, aes(x = MonthN, y = Marg, group = Fiscal.Year, colour=Fiscal.Year)) + geom_line() + geom_point() + scale_x_discrete(breaks = data$MonthN, labels = data$Month)g 这篇关于ggplot逐年比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-29 07:37