3

I would like to plot a bar chart for a dataframe containing both positive and negative values and the negative values should be in red.

I would like to avoid ggplot and do it with basic 'barplot'

Any suggestion?

Here is my code

df <- data.frame(MM=(1:12), value=rnorm(12))
df

toplot <- df
options(repr.plot.width=6, repr.plot.height=4)
label <- seq(1:12)
a <-barplot(df$value, 
        names=label, 
        col='deepskyblue3',
        xaxt = "n", yaxt = "n",
        )
axis(1, cex.axis=0.55, las=1, at=a, labels=label)
axis(2, las='2', cex.axis=0.6)
abline(h = 0, col = "black") 
gianca
  • 35
  • 2

1 Answers1

2

Add an ifelse condition to col

a <- barplot(df$value,
            names=label,
            col= ifelse(df$value < 0,"red","blue"),
            xaxt = "n", yaxt = "n"
)

Refer Change colours of particular bars in a bar chart

arjunsiva
  • 315
  • 2
  • 13