0

These are my simple data (table.all):

  YR   count
2001      8
2002     25
2003     24
2004     26
2005     34
2006     37
2007     61
2008     46
2009     49
2010     54
2011     35

I want to plot the count, both with barplot, and ggplot, but this barplot code fails to plot the years as x-axis labels:

barplot((table.all$count),axes=T,ylim=c(0,80),
        cex.names=0.8,las=2,
        main=paste("Number of peer-reviewed papers"))

And this ggplot code fails to show me anything at all, which is weird since I have reused some code I've already used and that has worked on other data:

ggplot(table.all, aes(x=YR,y=count)) + ylim(0,80) + xlim(2000,2016)
  scale_y_continuous(expand = c(0, 0)) +
  geom_bar(stat='identity') + 
  theme_bw(base_size = 16,base_family = "Times") +
  annotate("text",x=2.5,y=14.9,
           label="Total number of peer-reviewed papers",cex=7)
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Dag
  • 569
  • 2
  • 5
  • 20
  • Have you tried converting the Years to a factor variable and apply this solution? http://stackoverflow.com/q/16350720/3250126 – loki Apr 17 '16 at 10:04

1 Answers1

0

Have a look at the names.arg parameter of barplot:

barplot((table.all$count),axes=T,ylim=c(0,80), names.arg = table.all$YR,
    cex.names=0.8,las=2,
    main=paste("Number of peer-reviewed papers"))

In your ggplot code you are just missing a + after xlim. If you convert YR to factor you get your tick marks:

table.all$YR <- as.factor(table.all$YR)
ggplot(table.all,aes(x=YR,y=count)) +
  scale_y_continuous(limits=c(0,75)) +
  geom_bar(stat='identity') +
  theme_bw(base_size = 16) +
  ggtitle("Total number of peer-reviewed papers")

ggplot with tick marks

rhole
  • 440
  • 2
  • 8
  • This works. The only thing I can't get as I want is that I need the expand-command to work only at the max limit of the y axis, at the same time not wanting any expansion of the ymin=0. I need y=0 to be level with the x-axis. – Dag Apr 17 '16 at 11:55