1
$\begingroup$

Source: https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2FNEI_data.zip

Here is my data prep.

NEI <- readRDS("summarySCC_PM25.rds")
Baltimore <- NEI[NEI$fips=="24510", ]
Baltimore$type <- as.factor(Baltimore$type)
Total_Emmisssions <- aggregate(Baltimore$Emissions, 
                               by=list(Baltimore$year, Baltimore$type),
                               FUN=sum)
names(Total_Emmisssions) <- c("Year","Type","Emissions")  

Plot code

library(ggplot2)
g <- ggplot(Total_Emmisssions,aes(x=Year, y=Emissions, colour=Type))
g1 <- g+geom_point()
g2 <- g1+facet_grid(. ~ Type)
g3 <- g2+geom_smooth(method="lm", se=FALSE)

However, in this plot, the year is always 2002-2008. How do I change the scale / ticks to show from 1999-2008 at an interval of 3 years. Using scale_x_discrete does not work and it messes the graph. Please help.

str(Total_Emmisssions)
'data.frame':   16 obs. of  3 variables:
 $ Year     : int  1999 2002 2005 2008 1999 2002 2005 2008 1999 2002 ...
 $ Type     : Factor w/ 4 levels "NON-ROAD","NONPOINT",..: 1 1 1 1 2 2 2 2 3 3 ...
 $ Emissions: num  522.9 240.8 248.9 55.8 2107.6 ...
$\endgroup$
3
$\begingroup$

Change Year to a factor and add group=1:

g <- ggplot(Total_Emmisssions,aes(x=factor(Year), y=Emissions, colour=Type, group=1))

you can leave the rest the same (you'll also prbly want to change the xlab).

enter image description here

$\endgroup$
1
  • $\begingroup$ How does the geom_smooth() continue to work? I mean, for a line to fit, I was of the understanding that there needs t be two set of numbers. In this case, I don't understand how a factor and a number can come to give a linear regression model line. Or may be I just don't understand it to begin with. $\endgroup$
    – Drj
    Sep 15 '15 at 2:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.