删除ggplot中的一个图例

时间:2017-03-17 16:40:29

标签: r ggplot2

data = cbind(c("A","B","C","A"), c("John", "Kendra", "Martin", "Steve"), c("12","12","13","14"))
colnames(data) = c("Group", "Name", "Score")
data = as.data.frame(data)
p = ggplot(data, aes(x=Name, y=Score, fill=Name)) + geom_point(pch = 19, aes(col=Group)) + scale_colour_manual(values=c("black", "red", "green"))
print(p)

嘿,这段代码产生两个图例:名称和组。如何摆脱名字的传奇?我的意思是整个传奇,而不仅仅是它的标题。 编辑:我刚刚弄明白了。你这样说:

p = p + guides(fill=FALSE)

1 个答案:

答案 0 :(得分:2)

我会稍微调整你的例子以适合我的东西

您可以使用theme修改整个图例,但如果您想将修改限制为单个元素,请改用guide

data <- cbind(c("A","B","C","A"), c("John", "Kendra", "Martin", "Steve"), c("12","12","13","14"))
colnames(data) <- c("Group", "Name", "Score")
data <- as.data.frame(data)

p <- ggplot(data, aes(x = Name, y = Score, fill = Name)) + 
  geom_point(pch = 19, aes(col = Group)) + 
  scale_colour_manual(values = c("black", "red", "green")) +
  guides(fill = FALSE); p

在此更详细地解释:cookbook for R

希望这会有所帮助:)