gpt4 book ai didi

r - 在 ggplot 图例中正确显示线型

转载 作者:行者123 更新时间:2023-12-04 03:08:43 26 4
gpt4 key购买 nike

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

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

最佳答案

与其在同一个图例中调整大小,不如将形状和线型拆分为不同的图例。不完全是您要找的东西,但我认为它看起来不错。


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

关于r - 在 ggplot 图例中正确显示线型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46900721/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com