could explain me on how full control on legends in ggplot2 2 data frames different x-scales presented in 2 different geoms. 'name1' , 'name2' function created other filtering function.
1. why geom_point shape appears in legend "group 1"? expect legend show colour in group1 , shape group2.
is possible rearrange legends well? i.e group2 appears first in row.
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())
the key define fill
, shape
in both aes()
. can define shape
, fill
na
1 don't need.
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))
Comments
Post a Comment