0

After successfully running my bar plot, I have a PROBLEM where the numbers on the y axis are in codes and not in full, and instead of showing normal figures, it shows codes like 0e+00.

I would like the Y axis to show between 102,000 and 106,000.

The bar plot also has different figures. But it is not showing clearly the difference in yearly sales.

# Table of average total sales by year
yearly_sales <- DataSet %>% group_by(Year) %>% summarise(sales = mean(Weekly_Sales))
summarise(yearly_sales)
mycols <- c("#FF7F50", "#DE3163", "#6495ED")

# Bar chart for Average Yearly Sales 2010 to 2012
barplot(height = yearly_sales$sales, names = yearly_sales$Year, col = mycols)

This is the table I am trying to visualize

The bar plot is shown here. enter image description here

How can I get the chart to show clearly the difference in sales and how can I get the actual values too instead of 0e+00

Opeyemi Odedeyi
  • 766
  • 1
  • 10
  • 38

2 Answers2

1

This can be done using ylim and xpd, though I caution representing your data this way as it exaggerates the true difference between years and is a general violation of data visualization. The figure you represented in your question is more accurate.

At any rate, if you wanted to do this:

# Example data
data <- c(1033660, 1046239, 1059670)

# Plot, `a` saves the position of the x axis to place years text
a <- barplot(data, ylim = c(1020000, 1060000), xpd = FALSE, col = c("#FF7F50", "#DE3163", "#6495ED"))
axis(1, at = a, labels = c(2010, 2011, 2012), tick = FALSE)

ylim sets the limits and xpd clips the overhang at the lower axis.

enter image description here

jpsmith
  • 11,023
  • 5
  • 15
  • 36
1

Either temporally disabling scientific notation ,

op <- options(scipen=999)  ## set par
barplot(sales ~ Year, Yearly_sales, col=2:4)
par(op)  ## restore old par

or formatCing.

barplot(sales ~ Year, data=Yearly_sales, col=2:4, yaxt='n')
axis(side=2, at=axTicks(2), labels=formatC(axTicks(2), format='fg'))

Just like @jpsmith I'm neither a fan of narrowing the x-axis, because that's exactly how folks pimp their statistics and give the impression of huge differences while they are actually tiny, thus deceiving their readers. Please don't do this.

It might be more professional to show the percentage differences, e.g. compared to the first measurement in 2010.

Yearly_sales <- transform(Yearly_sales, 
                          diff=(sales - sales[Year == 2010])/sales[Year == 2010])

barplot(diff ~ Year, Yearly_sales, col=2:4, yaxt='n', ylim=c(-.04, 0))
axis(side=2, at=axTicks(2), labels=paste0(axTicks(2)*100, '%'))

enter image description here


Data:

Yearly_sales <- structure(list(sales = c(1033660, 1046239, 1059670), Year = 2012:2010, 
    diff = c(-0.0245453773344532, -0.0126747006143422, 0)), class = "data.frame", row.names = c(NA, 
-3L))
jay.sf
  • 60,139
  • 8
  • 53
  • 110