gpt4 book ai didi

r - ggplot2使图例键填充透明

转载 作者:行者123 更新时间:2023-12-04 01:39:25 27 4
gpt4 key购买 nike

我正在尝试使ggplot的图例键填充透明。我按照哈德利(Hadley)的ggplot2指南之一中的说明更改图例键填充,但是由于某种原因,当我将填充设置为透明时,它会用灰色填充。即使将图例键填充设置为白色,在最终绘图中它仍显示为灰色。

这是一个例子:

library(ggplot2)

data1 = c(0,10, 11, 23, 33, 40, 41, 50, 59, 68, 76, 88, 90, 99)
data2 = c(2, 8, 10, 22, 39, 47, 49, 55, 62, 70, 76, 86, 88, 95)

df = data.frame(data1, data2)

(plot = ggplot() +
geom_smooth(data=df, aes(data1, data2,colour="sample1"))+
geom_abline(intercept=0, slope=1,linetype="dashed", color = "black")+
scale_x_continuous(expand=c(0,0), limits=c(0,100)) +
scale_y_continuous(expand=c(0,0), limits=c(0,100))+
theme_classic()+
labs(y="data2", x="data1",
title="sample 1 data1 vs data2") +
theme(plot.title = element_text(size=18, face="bold"),
legend.key = element_rect(colour = "transparent", fill = "white"),
legend.justification = c(1,0), legend.position = c(1,0))+
scale_color_discrete(name="Sample") )

Example_plot

如果设置 theme(legend.key = element_rect(colour = "transparent", fill = "red")),将得到以下图:
red_fill

因此,似乎可以更改图例键的填充,但不能更改为白色或透明。

有谁知道我在做什么错,还是没有办法使图例键填充为透明/白色?

编辑:设置theme(legend.key = element_rect(fill = alpha("white", 0.0)))不能解决问题。

请参阅此处:
library(ggplot2)
library(scales)

data1 = c(0,10, 11, 23, 33, 40, 41, 50, 59, 68, 76, 88, 90, 99)
data2 = c(2, 8, 10, 22, 39, 47, 49, 55, 62, 70, 76, 86, 88, 95)

df = data.frame(data1, data2)

(plot = ggplot() +
geom_smooth(data=df, aes(data1, data2,colour="sample1"))+
theme_classic()+
labs(y="data2", x="data1",
title="sample 1 data1 vs data2") +
theme(plot.title = element_text(size=18, face="bold"),
legend.key = element_rect(colour = "transparent", fill = alpha("red", 0)),
legend.justification = c(1,0), legend.position = c(1,0))+
scale_color_discrete(name="Sample") )

编辑2:如果我使用geom_line()而不是geom_smooth,则能够将图例键填充设置为NA,因此必须是因为geom_smooth中的行周围有一个灰色区域作为置信区间,因此,图例键会镜像。
(plot = ggplot() +
geom_smooth(data=df, aes(data1, data2,colour="sample1"))+
geom_abline(intercept=0, slope=1,linetype="dashed", color = "black")+
scale_x_continuous(expand=c(0,0), limits=c(0,100)) +
scale_y_continuous(expand=c(0,0), limits=c(0,100))+
theme_classic()+
labs(y="data2", x="data1",
title="sample 1 data1 vs data2") +
theme(plot.title = element_text(size=18, face="bold"),
legend.key = element_rect(colour = NA, fill = NA),
legend.justification = c(1,0), legend.position = c(1,0))+
scale_color_discrete(name="Sample") )

geom_line

最佳答案

如果需要,可以欺骗它。添加第二个geom_smooth()。第一个带有置信度的乐队,您不会显示图例。在第二个中,您删除了乐队,但显示了图例。

df$Color <- "Red"
df1 <- df
(plot = ggplot() +
geom_smooth(data=df, aes(data1, data2,colour=Color), se = TRUE, show.legend = FALSE) +
geom_smooth(data=df1, aes(data1, data2,colour=Color), se=FALSE) +
geom_abline(intercept=0, slope=1,linetype="dashed", color = "black")+
scale_x_continuous(expand=c(0,0), limits=c(0,100)) +
scale_y_continuous(expand=c(0,0), limits=c(0,100))+
theme_classic()+
labs(y="data2", x="data1",
title="sample 1 data1 vs data2") +
theme(plot.title = element_text(size=18, face="bold"),
legend.key = element_rect(colour = "transparent", fill = "white"),
legend.justification = c(1,0), legend.position = c(1,0))+
scale_color_discrete(name="Sample"))

enter image description here

关于r - ggplot2使图例键填充透明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35108443/

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