Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a Unique Trend Line to a Barplot in GGPLOT2

Tags:

r

ggplot2

This is in part related to my question yesterday.

So here is the data and a plot created in ggplot2.

df = data.frame(date=c(rep(2008:2013, by=1)),
                value=c(303,407,538,696,881,1094))


ggplot(df, aes(date, value, width=0.64)) + 
        geom_bar(stat = "identity", fill="#336699", colour="black") +
        ylim(c(0,1400)) + opts(title="U.S. Smartphone Users") +
        opts(axis.text.y=theme_text(family="sans", face="bold")) +
        opts(axis.text.x=theme_text(family="sans", face="bold")) +
        opts(plot.title = theme_text(size=14, face="bold")) +
        xlab("Year") + ylab("Users (in millions)") +        
        opts(axis.title.x=theme_text(family="sans")) +
        opts(axis.title.y=theme_text(family="sans", angle=90)) +
        geom_segment(aes(x=2007.6, xend=2013, y=550, yend=1350), arrow=arrow(length=unit(0.4,"cm")))

Is it possible to produce the squigly trend line in the following graph with ggplot2

I had created the plot in R and then made prettied it up in Adobe Photoshop, and I'm wondering if could have produced that squiggly trend line straight in R.

If this can't be done in ggplot2, are there any specific R packages which would be amenable to this task?

I'm not asking about reproducing the graph. That's not an issue. Just producing the trend line seems to be an issue.

enter image description here

like image 772
ATMathew Avatar asked Aug 30 '11 15:08

ATMathew


People also ask

How do I add a trendline to a plot in R?

Adding line to a plot is dead simple. Just say lines(b) , where b specifies the line you want to plot after you have used the plot() function.

How do I add labels to a BarPlot in R?

To add labels on top of each bar in Barplot in R we use the geom_text() function of the ggplot2 package. Parameters: value: value field of which labels have to display.


1 Answers

As per all the comments: the squiggly line is scientifically dubious, since it isn't based upon a statistical model of the data. (And it appears to show the number of smartphone users levelling off, when the data shows no such thing.) So the best advice is "don't do it".

Since you seem really keen on the idea though, yes it is possible.

You can add any line you like with geom_line. To replicate the silly line in your infographic, you could do a straight line plus a sine curve for give it wiggle. Assuming your plot was named p

p + geom_line(
  aes(date, value), 
  data = data.frame(
    date = seq(2008, 2013, length.out = n),
    value = seq(600, 1300, length.out = n) + 100 * sin(seq(0, 4 * pi, length.out = n))  
  ), 
  arrow = arrow(length = unit(0.4, "cm"))
)

A better approach would be to use a loess smoothed curve.

p + geom_smooth(method = "loess", se = FALSE)  #maybe also span = 0.5, for extra wigglyness.
like image 83
Richie Cotton Avatar answered Sep 26 '22 23:09

Richie Cotton