在我的数据集上,出现了两个传说。一种是scale_color_manual(命名为“突变”),另一种是stat_difference (命名为“区域”)。我想把图例“区域”放在底部,把图例“突变”放在右上角。“突变”没有问题,但我不能成功地将“区域”移到底部。我该怎么做呢?以下是我的样例数据集:
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
下面是我的代码:
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))
我尝试使用以下两行代码。
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))
我的图表现在有两个“区域”图例。右边的一个是“变异”的传说。一个在底部,像下面这样。duplicate legend如何从右侧删除此重复图例?
发布于 2021-08-19 21:56:57
进行这样的黑客攻击总是需要大量的努力才能让它变得正确。但也许这就是你要找的:
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))
数据
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))
https://stackoverflow.com/questions/68853526
复制相似问题