-1

I have a data as shown below:

Year    PostNumber
2008    2
2009    116
2010    511
2011    1578
2012    2429
2013    1724
2014    1026
2015    648
2016    668
2017    546
2018    496
2019    427
2020    115

I want to plot barplot and a line together in the same plot to show the trend.

I refer to post and this gave me the plot as shown below

enter image description here

The code is as follows:

p<-ggplot(data=df, aes(x=PostYear, y=PostNumber)) +
  geom_bar(stat="identity", width = 0.4, fill="#0CF7E0")+
  geom_text(aes(label=PostNumber), vjust=0) 

p+scale_x_continuous(breaks = 1:13, labels = df$PostYear) +
  geom_line() + 
  geom_smooth(lty = 2)

the class of PostYear is numeric and PostNumber is integer.

i am looking for only barplot with labels and a trend line.

djMohit
  • 151
  • 1
  • 10

1 Answers1

0

I am able to find the desired solution using below code

p<-ggplot(data=df, aes(x=PostYear, y=PostNumber)) +
  geom_bar(stat="identity", width = 0.4, fill="#0CF7E0")+
  geom_text(aes(label=PostNumber), vjust=0) 


p+scale_x_continuous(breaks=seq(2008,2020,1))+
geom_line() + 
  geom_smooth(method = 'gam', inherit.aes = T, lty=2)

This gave me the desired output as below

enter image description here

djMohit
  • 151
  • 1
  • 10