ggplot of two data set with different colors
1
0
Entering edit mode
1 day ago
gogeni5529 ▴ 60

I'm trying to plot my data in a line plot using ggplots and geom_line.

The two data frames I want to plot are"

> group1 
    gene            T     value     Group1  Group
1  RPLP0  mean_ctrlt0 3600.4375       blue Group1
2  RPS16  mean_ctrlt0 4268.8750 darkorange Group1
3  RPS18  mean_ctrlt0 3559.8125  darkgreen Group1
4  RPS3A  mean_ctrlt0 3369.5625        red Group1
5  RPS4X  mean_ctrlt0 2954.7500      black Group1
6  RPLP0  mean_ctrlt8  514.1875       blue Group1
7  RPS16  mean_ctrlt8 2554.9375 darkorange Group1
8  RPS18  mean_ctrlt8  727.0000  darkgreen Group1
9  RPS3A  mean_ctrlt8 3364.7500        red Group1
10 RPS4X  mean_ctrlt8  271.9375      black Group1
11 RPLP0 mean_ctrlt16  128.0625       blue Group1
12 RPS16 mean_ctrlt16  885.0000 darkorange Group1
13 RPS18 mean_ctrlt16  125.1875  darkgreen Group1
14 RPS3A mean_ctrlt16 1525.8750        red Group1
15 RPS4X mean_ctrlt16   94.6875      black Group1
> group2
    gene             T    value    Group2  Group
1  RPLP0   mean_ctrlt0 3600.438 lightblue Group2
2  RPS16   mean_ctrlt0 4268.875   #ffbb78 Group2
3  RPS18   mean_ctrlt0 3559.812   #98df8a Group2
4  RPS3A   mean_ctrlt0 3369.562      pink Group2
5  RPS4X   mean_ctrlt0 2954.750      grey Group2
6  RPLP0  mean_Palbot8 2900.000 lightblue Group2
7  RPS16  mean_Palbot8 4754.250   #ffbb78 Group2
8  RPS18  mean_Palbot8 3496.500   #98df8a Group2
9  RPS3A  mean_Palbot8 3361.062      pink Group2
10 RPS4X  mean_Palbot8 2200.875      grey Group2
11 RPLP0 mean_Palbot16 2402.125 lightblue Group2
12 RPS16 mean_Palbot16 4470.250   #ffbb78 Group2
13 RPS18 mean_Palbot16 2986.438   #98df8a Group2
14 RPS3A mean_Palbot16 3007.312      pink Group2
15 RPS4X mean_Palbot16 1711.750      grey Group2
`

Now I want to color the lines of my plot base on the colors given in the data frame, but my ggplot2 command overwrite the second set of colors. I don't understand how to make it happen.

I'm trying to use scale_colour_manual to assign the colors to each group

group1 <- essentials.melt.screenPro_byGene |> 
  filter(T %in% c("mean_ctrlt0", "mean_ctrlt8", "mean_ctrlt16")) |> 
  mutate(Color = rep(c("blue", "darkorange", "darkgreen", "red", "black"), times=3))

group2 <- essentials.melt.screenPro_byGene |> 
  filter(T %in% c("mean_ctrlt0", "mean_Palbot8", "mean_Palbot16"))|> 
  mutate(Color = rep(c("lightblue", "#ffbb78", "#98df8a", "pink", "grey"), times=3))

ggplot() +
  # Solid lines for group 1 (group1)
  geom_line(data = group1, aes(x = T, y = value, group = gene, colour = gene), size = 1) +
  # Solid lines for group 2 (group2)
  geom_line(data = group2, aes(x = T, y = value, group = gene, colour = gene), size = 1) +
  # Add dots for each gene, using the same color as the lines (for both groups)
  geom_point(data = group1, aes(x = T, y = value, colour = gene), size = 3) +
  geom_point(data = group2, aes(x = T, y = value, colour = gene), size = 3) +
  # Manually set colors for each group (group1 and group2)
  scale_colour_manual(
    values = c(
      setNames(group1$Color, group1$gene),  # For group1
      setNames(group2$Color, group2$gene)   # For group2
    )
  ) +
  # Optional: separate the legend for each group
  guides(colour = guide_legend(title = "Gene"))

but the results is as such:

no second color group

I would appreciate your help.

geom_line scale_colour_manual ggplot2 • 145 views
ADD COMMENT
1
Entering edit mode

You are trying to colour the same gene names with two different codes. Either make two subplots for the groups or change the shape of dots based on groups and colour by the group.

ADD REPLY
0
Entering edit mode
1 day ago
Watermelon • 0

Try ggnewscale package, add new_scale_color() between the plots. Something like this should work

ggplot() +
  geom_line(data = group1_melt, aes(x = T, y = value, group = gene, colour = gene), size = 1) +
  geom_point(data = group1, aes(x = T, y = value, colour = gene), size = 3) +
  scale_colour_manual(values = c(group1$Color ), labels=c(group1$gene))+ new_scale_color() +
  geom_line(data = group2, aes(x = T, y = value, group = gene, colour = gene), size = 1) +
  geom_point(data = group2, aes(x = T, y = value, colour = gene), size = 3) +
  scale_colour_manual(values = c( group2$Color), labels=c(group2$gene))
ADD COMMENT

Login before adding your answer.

Traffic: 1693 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6