开发者

Adding a Unique Trend Line to a Barplot in 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")) +
      开发者_StackOverflow社区  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.

Adding a Unique Trend Line to a Barplot in GGPLOT2


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.


The answer to your question clearly is: Do not do this with R. R is not optimized for this. You'll get quicker and closer to what you want if you use some vector graphics program. Use Illustrator, Inkscape (free) or whatever graphics tool you like. Maybe you also want to create the graph in R and modify it later on in one of those programs.

I can understand the comments and also agree that this graph does not pass some tests. Not only academic tests, but also the look-and-feel-not-stuck-in-the-nineties-test. That reference to Edward Tufte was also for aesthetic reasons. However I think it's a legit question that does not deserve the down vote just cause the graph looks ugly. Sometimes academics also need to get their stuff published outside academic journals and graphics end up edited by editors for the masses. So knowing in advance – better do not try to do such stuff with R might be noteworthy.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜