geom_rect,geom_bar,geom_point:如何仅为geom_bar图例设置线形?

时间:2014-07-08 20:00:38

标签: r ggplot2

我在geom_bar的图例中为geom_rectgeom_point独立设置垂直线有些麻烦。这对我来说很难,因为我无法从geom_bar组中拆分geom_rectfill来正确设置颜色和alpha。

这是一个数据集的例子(可重复):

library(reshape2)
s1 <- seq(1,130000)
y1 = (cos(s1)^3)
y2 = (sin(s1)^3)
y3 = (sin(sin(s1)^3))
df1 <- data.frame(s1, y1, y2, y3)
dm1 <- melt(df1, id.var='s1')
xmin <- c(34800,36400,36700,36900,37300,39600)
xmax <- c(36300, 36500, 36700, 37000, 39000, 4000)
ymin <- c(-0.56, -0.60, -0.65, -0.56, -0.60, -0.56)
rect <- data.frame(xmin, xmax, ymin)

library(ggplot2)
#Open a layer with some option
ggplot() + xlim(34800, 42000) + ylim(-1, 1) + xlab("") + ylab("") + 
#plot geom_bar
 geom_bar(data = subset(dm1, variable %in% c("y3")), aes(x=s1, y=value, fill = variable, alpha=variable, group=variable), stat = "identity", position="identity", alpha = 0.5 ) +
#plot geom_point
geom_point(data=dm1[dm1$variable!="y3", ], aes(x = s1, y = value, color = variable, alpha= variable, group=variable )) + 
#plot geom_rect
geom_rect(aes(NULL, NULL, xmin = rect$xmin, xmax = rect$xmax, ymin = rect$ymin, ymax = rect$ymin + 0.05, fill="rect", alpha = "rect"), alpha = 0.5) +
#set alpha for each plot (not include in the legend)  
scale_alpha_manual(values = c(0.8, 0.5, 0.4, 0.5), name = element_blank(), guide = 'none') + 
#set colour for fill group
scale_fill_manual(values = c("black", "red"), name = "back", labels = c("rect", "y3")) + 
#set colour for color group
scale_color_manual (values = c("green", "blue"), name="Point", labels = c("y1", "y2"))

当前输出: enter image description here

在这个阶段我只想为y3传奇设置一条红色垂直线(不是红色方块)。

尝试为y3

设置类似vline的形状
+  guides(fill = guide_legend(override.aes = list(linetype = 'solid', fill=c("black", NA))))

显然,由于fill群组之间的联系......

,它无法奏效

任何想法或建议?

0 个答案:

没有答案