在ggplot图例中正确显示线型

时间:2017-10-24 01:13:11

标签: r ggplot2

我有以下数据框和图。在情节的图例中,不显示点的形状,线条太粗。有办法解决这个问题吗?

library(ggplot2)
library(ggalt)

x <- rnorm(100)
y <- rnorm(100)
group <- rep(c("A","B","C","D"), 25)
dat <- data.frame(x,y,group)

ggplot(dat, aes(x=x, y=y, shape=group, color=group)) +
  geom_point() +
  geom_encircle(data=subset(dat, group=='A'), aes(x=x,y=y),size=2, linetype=2) +
  geom_encircle(data=subset(dat, group=='B'), aes(x=x,y=y), size=3, linetype=3) +
  geom_encircle(data=subset(dat, group=='C'), aes(x=x,y=y),size=4, linetype=4) +
  geom_encircle(data=subset(dat, group=='D'), aes(x=x,y=y))

enter image description here

1 个答案:

答案 0 :(得分:2)

不是在相同的图例中使用大小调整,而是将形状和线条类型拆分为不同的图例可能是最容易的。不完全是你想要的,但我觉得它看起来不错。


library(ggplot2)
library(ggalt)

x <- rnorm(100)
y <- rnorm(100)
group <- rep(c("A","B","C","D"), 25)
dat <- data.frame(x,y,group)

ggplot(dat, aes(x=x, y=y, shape=group, color=group)) + 
  geom_point() +
  geom_encircle(aes(size=group, linetype = group)) + 
  scale_size_manual(values=c("A" = 2, "B" = 3, "C" = 4, "D" = 1)) +
  scale_shape_discrete(name = 'Shapes') +
  scale_color_discrete(name = 'Linetypes') +
  scale_linetype_discrete(name = 'Linetypes') +
  guides(shape = guide_legend(override.aes = list(size = 3)),
         linetype = guide_legend(override.aes = list(shape = NA)),
         size = FALSE) +
  theme(legend.key.size = unit(1, 'cm'),
        legend.box = 'horizontal')