gpt4 book ai didi

r - 当它们由相同的变量设置时如何在图例中组合线型和颜色

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

我有以下代码:

structure(list(Type = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L), .Label = c("Complex-valued", "Magnitude-only"), class = "factor"),
AR.coef = c(0, 0.025, 0.05, 0.075, 0.1, 0.125, 0.15, 0.175,
0.2, 0.225, 0.25, 0.275, 0.3, 0, 0.025, 0.05, 0.075, 0.1,
0.125, 0.15, 0.175, 0.2, 0.225, 0.25, 0.275, 0.3), variable = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "p=phat", class = "factor"),
value = c(0.978520704704333, 0.97587336999776, 0.973265254008069,
0.968200928906036, 0.964398292699414, 0.958921339792823,
0.954257350293665, 0.949424417504299, 0.942783668590964,
0.937194487113138, 0.931500996099189, 0.925212095505999,
0.918554141700981, 0.978840106896505, 0.976226910306292,
0.973587133603869, 0.968631836203812, 0.965199812495933,
0.960256158986167, 0.956236529101204, 0.951525171907093,
0.944964615583583, 0.938993495998045, 0.933391264497136,
0.926722511197902, 0.920070531126096)), row.names = c(NA,
-26L), .Names = c("Type", "AR.coef", "variable", "value"), class = "data.frame")
>ere

其中包含 AR.df 数据框。现在我想对线型和颜色使用相同的变量进行绘图,但是我得到了两个图例,我想要一个图例,但是具有不同颜色和线型的线条是合适的。
这是我的代码:
ggplot(AR.df, aes(x = AR.coef, y = value, linetype = Type, colour = Type)) +  
geom_line(size=1.25) + xlab("AR(1) coefficient") +
ylab("Proportion") + theme_bw() +
theme(legend.justification=c(1,-0.2),
legend.position=c(0.45,0.5),
legend.text=element_text(size=10),
legend.title=element_text(size=10),
axis.title.x=element_text(size=10),
axis.title.y=element_text(size = 10),
legend.key = element_blank(),
legend.background = element_rect(color="black",size = 0.1)) +
scale_colour_manual(values=cbPalette, name="Analysis Type") +
scale_linetype_manual(values=c("solid", "dashed")) +
theme(legend.key.width=unit(3,"line"))

这给出了以下带有两个图例的图:
enter image description here

如何摆脱第一个图例框并让第二个框中的图例也具有线型?

最佳答案

感谢罗兰,这是答案:

require(ggplot2)
cbPalette <- c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442",
"#0072B2", "#D55E00", "#CC79A7")
cbPalette <- cbPalette[-1]

ggplot(subset(AR.df)) +
geom_line(aes(x = AR.coef, y = value, linetype = Type, colour = Type),size=1.25) +
xlab("AR(1) coefficient") +
ylab("Proportion") +
theme_bw() +
theme(legend.justification=c(1,-0.2),
legend.position=c(0.4,0.3),
legend.text=element_text(size=10),
legend.title=element_text(size=10),
axis.title.x=element_text(size=10),
axis.title.y=element_text(size = 10),
legend.key = element_blank(),
legend.background = element_rect(color="black",size = 0.1)) +
scale_colour_manual(values=cbPalette, name="Analysis Type") +
scale_linetype_manual(values=c("solid", "dashed"),name="Analysis Type") +
theme(legend.key.width=unit(3,"line"))

enter image description here

关于r - 当它们由相同的变量设置时如何在图例中组合线型和颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33739576/

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