【发布时间】:2016-05-06 22:48:01
【问题描述】:
有人可以向我解释如何完全控制 ggplot2 中的图例,其中两个数据框具有不同的 x 比例,并呈现在两个不同的几何图形中。 'name1' 和 'name2' 是使用其他过滤函数创建的函数。
1。 为什么 geom_point 形状出现在“第 1 组”的图例中?我希望图例仅在 Group1 中显示颜色,在 Group2 中显示形状。
是否也可以重新排列图例?即 Group2 出现在行中的第一个。
df1 <- data.frame(g1 = c("a", "b", "c", "e"),
y1 = c(12, 8, 3, 20))
df2 <- data.frame(g1 = letters[1:5],
y1 = 20:24)
name1 <- "Group 1"
name2 <- "Group 2"
require(ggplot2)
ggplot(NULL, aes(x=g1, y=y1)) +
geom_bar(data = df1, stat = "identity",
aes(fill=factor(name1))) +
geom_point(data = df2, stat = "identity",
size = 5, shape = 2, aes(fill=factor(name2))) +
theme(plot.margin = unit(c(2,1,1,1), "lines"),
plot.title = element_text(hjust = 0, size=18),
axis.title = element_text(face = "bold", size = 12),
legend.position = 'top',
legend.text = element_text(size = 12),
legend.title = element_blank())
【问题讨论】: