0

I'm stuck with a problem that's already been solved many times here, but none of the solution provided solves my problem.

I built a barplot using ggplot2 My data is stocked in an excel database, imported into R. It looks like this :

Subject_name |      group   |        IFI
"    XX    " | 0, 1, 2 or 3 | 0 or 1 (= yes or no)

Example:

"    XX1   " |       1      | 0
"    XX2   " |       2      | 1
"    XX3   " |       2      | 0

With this code :

ggplot(database) +
  aes(x = group, fill = factor(IFI)) +
  geom_bar(position = "fill", show.legend = F, colour = "NA", alpha = 0.4) +
  scale_fill_manual(values=c(NA, "red")) +
  scale_y_continuous(name = "% yes", limits=c(0,0.5),oob = rescale_none, breaks = seq(0,0.5,0.05), labels = percent_format(suffix = "%", accuracy = 1))

I printed the left barplot :

stacked bar plot in ggplot2

I can't find any solution to change color of the first column only (see right barplot, modified with Paint).

In scale_fill_manual(values=c()), only two colors are accepted, but it changes the upper (here NA = no color) and lower (here red) part of the plot.

B_slash_
  • 309
  • 2
  • 17
  • What do you expect by setting fill to NA? If you want to change an aesthetic such as fill by some variable, you need to create a variable to differentiate those groups. It doesn't seem like `fill = factor(IFI)` matches what you describe – camille May 09 '19 at 17:36
  • I am not sure to understand your first question, can you explain it? In fact, i am not even sure that this barplot is the best way to represent what I want (just show a percentage of "yes" (of "1") in each of my subgroup)... – B_slash_ May 09 '19 at 18:00
  • You set the fill scale with `values=c(NA, "red")`. What do you expect to see for bars where you've set the fill as `NA`? – camille May 09 '19 at 18:15
  • ```NA``` is for setting to transparent the part of the bar that I don't want to see (the other 95%, 70%, 63%, 56% of the bar, respectively) – B_slash_ May 09 '19 at 19:03

0 Answers0