0

I am planning to plot a bar-plot/clustered column chart for time vs revenue with trend-line connecting each bars on top. Starting from year 1981 to 1988.

I have used this code to read the csv : read.csv(file_location/Revenue.csv",header = T, sep=",", dec = ".")

for the plotting : pl <- ggplot(data,aes(x=ï..Year))

and then : pl + geom_bar(color='red',fill='blue').

Unfortunately, i end up with something like this. Whereas, i'd prefer something like this. I used only ggplot2 library in this case, should i use tidyr, diplyr additionally ? Am i mistaking between continuous and discrete variables. Any advice regarding aesthetic modification to beautify it or solutions regarding this would be really appreciated as i am still in the basics of ggplot and data visualizations.

I have added the fine in case if you want to check it : Revenue.csv

1 Answer 1

1

Check the documentation here form some information, but the big change you should make is to use geom_col in place of geom_bar. Your current call specifies an x= aesthetic (what should be the x axis), but not the y= aesthetic (what should be the y axis). geom_bar indicates the number of cases/observations at each x value by default, whereas geom_col is used to display a bar of length y at each x value... but you need a y aesthetic.

With all that being said, try this:

pl <- ggplot(data,aes(x=ï..Year, y=your.y.column.name)) +
    geom_col(color='red',fill='blue')

As for aesthetics, I might change the color scheme a bit and also the theme, but that's ind of personal preference. My suggestion would be to at least change your color scheme for geom_bar/col. The color= specifies the outline on the bars, and the fill= is the color of the bars. Your code would give you bright blue bars with a red outline... not awesome. I would also change the width of your bars a to be a bit skinnier by adjusting the width= argument from the default of 1 to something smaller. Here is an example with a dummy dataset. Most people (me included) would not want to download someone else's data via a link, sorry.

df <- data.frame(x=1:10, y=1:10)
ggplot(df, aes(x=x, y=y)) +
    geom_col(fill='steelblue', color='black', width=0.5) +
    theme_bw()

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much, it worked fine for me. I customized the plot a bit with these codes : ggplot(data,aes(x=ï..Year, y=Amount,fill=Amount))+ geom_col() + geom_smooth(method = 'loess',se=FALSE) + theme_bw()+ scale_fill_gradient(low = '#08AEEA',high = '#2AF598') However i am having trouble with drawing a trend-line above the barplot, could you point me some smoothing method documentation regarding it or some solutions perhaps ? Also, numbers in my data are pretty huge and it's showing "1e+08", is there anything i can do about it so that i can have actual numbers there in the plot?
Sure: for trendline, you can try geom_smooth, or if you have the equation for a straight line, go for geom_abline. Check documentation for either of them for more information. As for number, probably the scales package will work for you. You can use math_format in particular to apply to labels= inside a scale_..._... function to set the format of your numbers. A bunch of other useful functions in that package for similar adjustments.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.