我正在尝试使用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)))

r - ggplot减少图例中的线条粗细-LMLPHP

如何在图例中实现大形状和细线?

最佳答案

谢谢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)))

谢谢。

关于r - ggplot减少图例中的线条粗细,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36162718/

10-12 22:39