我已经创建了一个4 * 2的实验设计(每次治疗4次治疗和2次重复)并且想要使用4种形状和2种颜色符号来对ggplot2中的4 * 2实验进行分类。我有两个传说,一个用于形状,另一个用于两种颜色,我想将两个传说合并为一个传奇。我搜索了帖子(Combine legends for color and shape into a single legend),但仍然无法解决问题。提前谢谢!
My data:
x y Treatment Repeat
75.74907227 73.6 A 1
236.4477148 242.8 A 2
93.88145508 98.5 B 1
66.58028809 67.1 B 2
53.54458984 55.2 C 1
32.34567383 31.9 C 2
210.5494727 201.2520117 D 1
497.5761328 532.715625 D 2
My code:
library("ggplot2")
p<-ggplot(hg,aes(hg$x,hg$y))
p<-p+geom_point(stat = "identity",size=3,aes(shape=factor(hg$Treatment),
colour=factor(hg$Repeat)))+
scale_shape_manual(name="Treatment",values = c(0, 1, 2, 5),
labels=c("A","B","C","D")) +
scale_colour_manual(name="Repeat",values = c("red","darkgreen"),labels=c("1","2")) +
ggtitle("hg")+
coord_equal()+
scale_x_continuous(breaks = seq(0, 750, 50), limits = c(0, 750),expand = c(0, 0)) +
scale_y_continuous(breaks = seq(0, 750, 50), limits = c(0, 750),expand = c(0, 0)) +
theme_bw() +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.position=c(0.85,0.3))+
geom_abline(intercept = 0, slope = 1,
colour = "red",size=0.5,show_guide = FALSE)
p
答案 0 :(得分:1)
如果您在链接的问题中发现,group2
是图例中合并的两个组合的组合。你需要做同样的事情。此外,您需要确保scale_shape_manual
和scale_colour_manual
之间的标签相同。
hg <- read.table(header=T, text='
x y Treatment Repeat
75.74907227 73.6 A 1
236.4477148 242.8 A 2
93.88145508 98.5 B 1
66.58028809 67.1 B 2
53.54458984 55.2 C 1
32.34567383 31.9 C 2
210.5494727 201.2520117 D 1
497.5761328 532.715625 D 2
')
hg$TR <- paste(hg$Treatment, hg$Repeat)
p <- ggplot(hg, aes(x, y, shape=TR, colour=TR))+
geom_point(stat = "identity", size=3)+
scale_shape_manual(name="Treatment & Repeat",
labels=c("A,1","A,2","B,1","B,2","C,1","C,2","D,1","D,2"),
values = rep(c(0, 1, 2, 5), each=2)) +
scale_colour_manual(name="Treatment & Repeat",
labels=c("A,1","A,2","B,1","B,2","C,1","C,2","D,1","D,2"),
values = rep(c("red","darkgreen"), 4)) +
ggtitle("hg")+
coord_equal()+
scale_x_continuous(breaks = seq(0, 750, 50), limits = c(0, 750),expand = c(0, 0)) +
scale_y_continuous(breaks = seq(0, 750, 50), limits = c(0, 750),expand = c(0, 0)) +
theme_bw() +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.position=c(0.85,0.3))+
geom_abline(intercept = 0, slope = 1,
colour = "red",size=0.5,show_guide = FALSE)
p