1
votes

I have the following data:

> Dummydata 
   Sample      r.K
1      E1 0.084150
2      E2 0.015170
3      E3 0.010662
4      E4 0.016123
5     EK1 0.010289
6     EK2 0.017484
7     EK3 0.014685
8     EK4 0.014272
9     EK5 0.012551
10     K1 0.010069
11     K2 0.010253
12     K3 0.010568
13     K4 0.011230
14     K5 0.010286

I made a geom_col plot with my data:

plot_dummy_data <- Dummydata %>% ggplot(aes(x = Sample, y =r.K)) + 
geom_col(fill = "#FAE0B1") + labs(y= "fitness cost", x = "sample")

Plot

I want to color the first 4 columns the same color as they correspond to a specific host and the next 5 columns another color and then the last 5 columns a third color.

I have seen the function scale_fill_manual() but I don't understand how I can choose a specific color for a group of columns but not all of them.

I've been trying all day and gone through everything I could find on here but I still haven't figured it out. I'm a beginner at R so I really appreciate any help.

1

1 Answers

0
votes

One option to achieve your desired result would be to

  1. Add an identifier for the column groups, e.g. in the code below case of your example data you could use gsub("\\d", "", Sample) to remove the numbers from Sample column.
  2. Map the group identifier variable on the fill aesthetic.
  3. Set your desired colors via scale_fill_manual.
library(ggplot2)
library(dplyr)


Dummydata %>%
  mutate(group = gsub("\\d", "", Sample)) %>%
  ggplot(aes(x = Sample, y = r.K, fill = group)) +
  geom_col() +
  scale_fill_manual(values = c(E = "red", EK = "blue", K = "yellow")) +
  labs(y = "fitness cost", x = "sample")

DATA

Dummydata <- structure(list(Sample = c(
  "E1", "E2", "E3", "E4", "EK1", "EK2",
  "EK3", "EK4", "EK5", "K1", "K2", "K3", "K4", "K5"
), r.K = c(
  0.08415,
  0.01517, 0.010662, 0.016123, 0.010289, 0.017484, 0.014685, 0.014272,
  0.012551, 0.010069, 0.010253, 0.010568, 0.01123, 0.010286
)), class = "data.frame", row.names = c(
  "1",
  "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13",
  "14"
))