R 图例控件,在ggplot2中具有两个不同x比例和不同几何图形的数据帧
有人能给我解释一下如何在ggplot2中完全控制图例,在两种不同的几何图形中显示两个具有不同x比例的数据帧。“name1”和“name2”是使用其他筛选函数创建的函数 。 为什么geom_点形状出现在“组1”的图例中?我希望图例将只显示组1的颜色和组2的形状 也可以重新排列传说吗?i、 e组2在行中首先出现R 图例控件,在ggplot2中具有两个不同x比例和不同几何图形的数据帧,r,ggplot2,legend,R,Ggplot2,Legend,有人能给我解释一下如何在ggplot2中完全控制图例,在两种不同的几何图形中显示两个具有不同x比例的数据帧。“name1”和“name2”是使用其他筛选函数创建的函数 。 为什么geom_点形状出现在“组1”的图例中?我希望图例将只显示组1的颜色和组2的形状 也可以重新排列传说吗?i、 e组2在行中首先出现 df1 <- data.frame(g1 = c("a", "b", "c", "e"), y1 = c(12, 8, 3, 20)) df2 &l
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())
df1关键是在aes()和中定义fill
和shape
。然后,您可以将不需要的形状
和填充
定义为NA
ggplot(NULL, aes(x=g1, y=y1)) +
geom_bar(data = df1, stat = "identity", aes(fill=name2, shape=name2)) +
geom_point(data = df2, size = 5, aes(shape=name1, fill=name1)) +
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()) +
scale_shape_manual(values=c(2, NA)) +
scale_fill_manual(values=c(NA, "red")) +
guides(fill = guide_legend(reverse = TRUE),
shape = guide_legend(reverse = TRUE))