0

With my datasets, two legends have arisen. One is with scale_color_manual(named "Mutations") and the other is in stat_difference (named "Regions"). I want to put the legend "Regions" at the bottom and the legend "Mutations" at the top right. It's all ok with "Mutations", but I am unsuccessful to move "Regions" to the bottom. How should I do that? Here is my sample dataset:

Position    Wild_Score  A15S_Score
4   1.07    1.07
5   1.076   1.076
6   1.067   1.067
7   1.112   1.112
8   1.112   1.112
9   1.169   1.169
10  1.146   1.146
11  1.16    1.16
12  1.188   1.181
13  1.188   1.181
14  1.201   1.194
15  1.201   1.194
16  1.155   1.148

Here is my code:

library(ggplot2)
library(ggh4x)
setwd("F:/Mutations/Graph_input")
d <- read.csv(file = "ORF7b.csv", sep = ",", header = TRUE)
p1 <- ggplot(d, aes(x= Position,y= Wild_Score)) + xlab("Positions") + ylab("Scores") +
  stat_difference(aes(ymin = 1, ymax = Wild_Score), alpha = 0.5, levels = c("Antigenic", "Non antigenic", "Neutral")) + 
  scale_fill_discrete(name = "Regions") + geom_line(aes(y=1)) + geom_line(d,aes(y = A15S_Score), color = "blue", size = 1) + theme(legend.position = c(0.92,0.8)) + 
  geom_point(d = d[,c(1,3)], aes(x= 15, y = 1.194, color = "A15S"), size = 3) + scale_color_manual(name = "Mutations", values = "A15S" = "blue") +
  ggtitle("ORF7b protein") + theme(plot.title = element_text(hjust = 0.5))

I tried with the following two lines of code.

    guide_color <- get_legend(p1 + guides(value = "none")) 
plot_grid(p1 + guides(color = "none") + theme(legend.position = "bottom"), guide_color, ncol = 2, rel_widths = c(.9, .01)) 

My graph has now two "Regions" legend. One in the right side along with "Mutation" legend. One at the bottom like following. duplicate legend How do I remove this duplicate legend from right side?

2

1 Answer 1

0

Doing such hacks is always a lot of fiddling to get it right. But maybe this is what you are looking for:

library(ggplot2)
library(ggh4x)
library(cowplot)

p1 <- ggplot(d, aes(Position, Wild_Score)) +
  stat_difference(aes(ymin = 1, ymax = Wild_Score), alpha = 0.5, levels = c("Antigenic", "Non antigenic", "Neutral")) +
  scale_fill_discrete(name = "Regions") +
  geom_line(aes(y = 1)) +
  geom_line(data = d, aes(y = A15S_Score), color = "blue", size = 1) +
  geom_point(data = d[, c(1, 3)], aes(x = 15, y = 1.194, color = "A15S"), size = 3) +
  scale_color_manual(name = "Mutations", values = c("A15S" = "blue")) +
  labs(title = "ORF7b protein", x = "Positions", y = "Scores") +
  theme(plot.title = element_text(hjust = 0.5))

guide_fill <- get_legend(p1 + guides(color = "none") + theme(legend.position = "bottom"))

plot_grid(p1 +
            guides(fill = "none") + 
            theme(legend.position = c(0.92, 0.8)), 
          guide_fill, nrow = 2, rel_heights = c(10, 1))

Data

d <- structure(list(Position = 4:16, Wild_Score = c(1.07, 1.076, 1.067, 
                                               1.112, 1.112, 1.169, 1.146, 1.16, 1.188, 1.188, 1.201, 1.201, 
                                               1.155), A15S_Score = c(1.07, 1.076, 1.067, 1.112, 1.112, 1.169, 
                                                                      1.146, 1.16, 1.181, 1.181, 1.194, 1.194, 1.148)), class = "data.frame", row.names = c(NA, 
                                                                                                                                                            -13L))

Not the answer you're looking for? Browse other questions tagged or ask your own question.