How to change colors in a grouped bar plot?

Hi!

I'm trying to change the colors of my grouped bar graph but I keep getting this error:

Error in barplot.default(data, col = 1:nrow(data), legend.text = TRUE, :
formal argument "col" matched by multiple actual arguments

data (need to be made into a table I think)
data.frame(
Freq = c(106, 111, 43, 126, 80, 116, 37, 153, 74, 122, 22, 168),
Var1 = as.factor(c("Correct","Incorrect",
"Nomatch","Share","Correct","Incorrect",
"Nomatch","Share","Correct","Incorrect","Nomatch",
"Share")),
Var2 = as.factor(c("99p","99p","99p",
"99p","98p","98p","98p","98p","97p","97p","97p",
"97p"))
)

library(RColorBrewer)
coul <- brewer.pal(4, "Set2")

barplot(data, # Draw barplot with properly aligned legend
col = 1:nrow(data),
legend.text = TRUE,
col = coul,
beside = TRUE,
args.legend = list(x = "topleft",
inset = c(0.01, -0.1)))

If I remove the "col = coul" line, then the bar graph works, but it gives me some standard colors which I want to change.

thank you!!!

HI @niko_bio, maybe you need this.

library(ggplot2)
ggplot(data, aes(x=Var1, y=Freq, fill=Var2)) +
  geom_bar(stat="identity")+
  ggtitle("name")

image

Then, after switching to ggplot you can easily add the RColorBrewer palette:

ggplot(data, aes(x=Var1, y=Freq, fill=Var2)) +
  geom_bar(stat="identity")+
  ggtitle("name") +
  scale_fill_brewer(palette = "Set2") 
  # or: 
  # scale_fill_manual(values = coul)

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.