Categories
data science

Smooth line for barplot graph – R

The bar plot

Start with a dataframe with the values already sorted, then place each into a vector.

core<-tdf$'core'
mid<-tdf$'middle'
per<-tdf$'periphery'

The three back into a dataframe

bpDataFrame<-data.frame(per,mid,core)

The Bar Plot

Place the bartop into a variable to use later

theBarPlot<-barplot(as.matrix(t(bpDataFrame)))

The graph should look something like this

step1

A Ragged Line

Take the Gini coef column from the original dataframe…

theGiniCoef<-tdf$'gini'

Put everything together now and plot the bar graph and the line …

lines(theBarPlot,theGiniCoef,col="Grey",lwd=2)

The graph now looks like this

step2

The Smooth Line

Now add a smooth line that will show an over-all trend for the Gini coef using loess()

x<-1:length(theGiniCoef)
lo<-loess(theGiniCoef~x)

Now plot the thing: white line, thikness = 2.

lines(theBarPlot,predict(lo),col='white',lwd=2)

Now, it looks likes this!

step3

DONE!

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s