向 ggplot 添加一个点会弄乱图例
Adding a point to the ggplot messes up the legend
我有一个数据数据框
dat <- data.frame(cond = rep(c("A", "B"), each=10),
xvar = 1:20 + rnorm(20,sd=3),
yvar = 1:20 + rnorm(20,sd=3))
我想使用 ggplot2
散点图
假设附加点只是数据中的 15th
行
g1 <- dat[15,]
我现在可以生成绘图了
使用
scat1 <- ggplot(dat, aes(x=dat[,2], y=dat[,3], shape=factor(dat[,1]), size=2.5,
colour = factor(dat[,1]))) + geom_point(alpha=1)
#Add point to the plot
scat1 <- scat1 + geom_point(x=g1[,2],y=g1[,3],
colour="blue", size=4) # this adds a blue point
#here the legend goes awry and color changes to blue
#Add a label for the added point
scat1 <- scat1 + geom_text(x=g1[,2], y=1+g1[,3], label="Added", col="Black")
# this adds a label for the blue point
# Format the figure
scat1 <- scat1 + theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.text=element_text(size=14),
plot.title = element_text(size = rel(2),
colour = "black", face="bold"),
axis.title=element_text(size=16,face="bold"),
panel.background = element_blank(),
axis.line = element_line(colour = "black"),
legend.text=element_text(size=14),
axis.text.x = element_blank(),
legend.title=element_text(size=14),
legend.justification = c(1, 1),
legend.position = c(0.25,1))
#Add sensible xlabel and ylabel
scat1 <- scat1 + ylab(colnames(dat)[3]) + xlab(colnames(dat)[2])
scat1 <- scat1 + ggtitle(paste("The variable", colnames(dat)[2], "and", colnames(dat)[3] ))
#Delete multiple legends and add a suitable title to the legend
scat1 <- scat1 + guides(shape=guide_legend(title="sale year"), size=FALSE,
color= guide_legend(title="sale year"))
但是,我想将图例更改为与图像中原始颜色和形状匹配的形状和颜色,如下所示。我怎样才能做到这一点?
show_guide
会帮助你。
请注意您在 aes
中定义事物的方式!只引用实际的变量名称(aes
中没有 dat
)并将你想要设置(而不是映射)的东西移到 aes
调用之外。这也意味着您不必删除 size
的第二个图例。
ggplot(dat, aes(x = xvar, y = yvar, shape = cond,
colour = cond), size = 2.5) +
geom_point(alpha = 1) +
geom_point(data = g1, colour = "blue", size = 4, show_guide = FALSE)
我有一个数据数据框
dat <- data.frame(cond = rep(c("A", "B"), each=10),
xvar = 1:20 + rnorm(20,sd=3),
yvar = 1:20 + rnorm(20,sd=3))
我想使用 ggplot2
假设附加点只是数据中的 15th
行
g1 <- dat[15,]
我现在可以生成绘图了
使用
scat1 <- ggplot(dat, aes(x=dat[,2], y=dat[,3], shape=factor(dat[,1]), size=2.5,
colour = factor(dat[,1]))) + geom_point(alpha=1)
#Add point to the plot
scat1 <- scat1 + geom_point(x=g1[,2],y=g1[,3],
colour="blue", size=4) # this adds a blue point
#here the legend goes awry and color changes to blue
#Add a label for the added point
scat1 <- scat1 + geom_text(x=g1[,2], y=1+g1[,3], label="Added", col="Black")
# this adds a label for the blue point
# Format the figure
scat1 <- scat1 + theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.text=element_text(size=14),
plot.title = element_text(size = rel(2),
colour = "black", face="bold"),
axis.title=element_text(size=16,face="bold"),
panel.background = element_blank(),
axis.line = element_line(colour = "black"),
legend.text=element_text(size=14),
axis.text.x = element_blank(),
legend.title=element_text(size=14),
legend.justification = c(1, 1),
legend.position = c(0.25,1))
#Add sensible xlabel and ylabel
scat1 <- scat1 + ylab(colnames(dat)[3]) + xlab(colnames(dat)[2])
scat1 <- scat1 + ggtitle(paste("The variable", colnames(dat)[2], "and", colnames(dat)[3] ))
#Delete multiple legends and add a suitable title to the legend
scat1 <- scat1 + guides(shape=guide_legend(title="sale year"), size=FALSE,
color= guide_legend(title="sale year"))
但是,我想将图例更改为与图像中原始颜色和形状匹配的形状和颜色,如下所示。我怎样才能做到这一点?
show_guide
会帮助你。
请注意您在 aes
中定义事物的方式!只引用实际的变量名称(aes
中没有 dat
)并将你想要设置(而不是映射)的东西移到 aes
调用之外。这也意味着您不必删除 size
的第二个图例。
ggplot(dat, aes(x = xvar, y = yvar, shape = cond,
colour = cond), size = 2.5) +
geom_point(alpha = 1) +
geom_point(data = g1, colour = "blue", size = 4, show_guide = FALSE)