Pablo O
Pablo O

Reputation: 198

Choosing specific colors for different levels of column in a data_frame when graphing with ggplot2

I'm using ggplot2 to make several graphs from a large data frame. The graphs are bar graphs showing proportions of two levels of a column in the data-frame, Yes and No. The colors are chosen by default, and want to make one of the levels either completely transparent, or choose a different color for it.

My code to graph is the following:

    ggplot(data=encuestas ,aes(x = colonia,fill = p1))  +
    theme(legend.position="bottom", axis.title.x=element_blank())  +
    geom_bar(position = "fill")

I have tried using geom_bar, and the colors change successfully, but the chart graphs count instead of proportion when using geom_bar.

These are the default colors for the levels:

These are the default colors for the levels that I'd like to change or make one of them translucent:

Any help would be greatly appreciated

Upvotes: 1

Views: 705

Answers (1)

Henrik
Henrik

Reputation: 67818

You can use scale_fill_manual to set the colour of the bars, and ylab to change the default axis label ('count').

ggplot(mtcars, aes(x = factor(cyl), fill = factor(vs))) +
  geom_bar(position = "fill") +
  scale_fill_manual(values = c("red", "blue")) +
  ylab("Proportion")

Upvotes: 1

Related Questions