ggplot减少图例中的线宽

时间:2016-03-22 18:46:43

标签: r ggplot2 legend

我正在尝试使用ggplot2

来控制情节

示例脚本:

dat1 <- data.frame(
     sex = factor(c("Female","Female","Male","Male")),
     time = factor(c("Lunch","Dinner","Lunch","Dinner"), levels=c("Lunch","Dinner")),
     total_bill = c(13.53, 16.81, 16.24, 17.42)
)
p = ggplot(data=dat1, aes(x=sex, y=total_bill, group=time, shape=time, color=time)) + geom_line() + geom_point()

现在我想控制图例中形状和线条的样子。我希望在传奇中有更大的形状和细线。但我不能同时执行这两项工作。

如果我这样做,

p = p + guides(colour = guide_legend(override.aes = list(size=5)))

线条和形状都很粗,类似于图B.

如果我这样做,

p = p + guides(colour = guide_legend(override.aes = list(size=5,linetype=0)))

然后形状的大小正确,但线条消失(图A)。我尝试过这样的事情失败了。

p = p + guides(colour = guide_legend(override.aes = list(size=5,linetype=0.5)))

Example

如何在传奇中实现大形状和细线?

1 个答案:

答案 0 :(得分:5)

感谢Sandy Musprattuser20650。这两个链接都很有用。

为了简单起见,我使用了user20650的代码。

此处填写完整代码:

dat1 <- data.frame(
    sex = factor(c("Female","Female","Male","Male")),
    time = factor(c("Lunch","Dinner","Lunch","Dinner"), levels=c("Lunch","Dinner")),
    total_bill = c(13.53, 16.81, 16.24, 17.42)
)
p = ggplot(data=dat1, aes(x=sex, y=total_bill, group=time, shape=time, color=time)) 
p = p + geom_line() + geom_point(size=5, alpha=0) + geom_point(show.legend=FALSE) 
p = p + guides(colour = guide_legend(override.aes = list(alpha=1)))

感谢。