gpt4 book ai didi

r - 将图例添加到具有不同类型美学的 ggplot 直方图

转载 作者:行者123 更新时间:2023-12-04 11:05:14 25 4
gpt4 key购买 nike

我想在我的一个情节中添加一个图例,但我有不同的审美观,而且我从未创建过图例,所以我发现很难确定如何构建它。

我的美学之一是填充代码,我将其作为矢量手动添加。另一种美学是我用 geom_vline 添加的垂直线。

从下图中,我想将三个特征添加到图例中:1) 深蓝色条,2) 浅蓝色条和 3) 垂直线。

有人对我如何有效地编写代码有什么建议吗?

#df
df <- data.frame(Time_Diff <- runif(1000, 0, 200))


# Show median, IQR range and outliers
colors <- c(rep("blue",3), rep("paleturquoise2",38))
bp_overall <- ggplot(data = df, aes(Time_Diff))
bp_overall +
geom_histogram(binwidth = 5, fill = colors) + #create histogram
ggtitle("Time Difference") +
xlab("Time in Days") +
ylab("Amount") +
geom_vline(xintercept = 3, linetype = "twodash", size = 1, colour= "darkblue") + #show median
scale_x_continuous(breaks = seq(0, 202, 10)) +
theme_light() +
theme(panel.grid.minor = element_blank(),
panel.border = element_blank(), #remove all border lines
axis.line.x = element_line(size = 0.5, linetype = "solid", colour = "black"), #add x-axis border line
axis.line.y = element_line(size = 0.5, linetype = "solid", colour = "black")) + #add y-axis border line
theme(plot.title = element_text(family = windowsFont("Verdana"), color="black", size=14, hjust = 0.5)) +
theme(axis.title = element_text(family = windowsFont("Verdana"), color="black", size=12))

在 Djork 的建议下,我得到了以下脚本,它有效并且我很满意。我现在唯一要做的就是让 Legend 成为一个整体(Histogram Legend 和 Line Legend 组合成一个连贯的整体)。有人有建议吗?

# reformat data
set.seed(1)
df <- data.frame(runif(1000, 0, 200))
colnames(df) <- "Time_Diff"

bp_overall +
geom_histogram(data = subset(df, Time_Diff <= 12.5), aes(x = Time_Diff, fill="BAR BLUE"), binwidth = 5) + # subset for blue data, where aes fill is fill group 1 label
geom_histogram(data = subset(df, Time_Diff > 12.5), aes(x = Time_Diff, fill="BAR TURQUOISE"), binwidth = 5) + # subset for turquoise data, where aes fill is fill group 2 label
scale_fill_manual("Histogram Legend", values=c("blue", "paleturquoise2")) + # manually assign histogram fill colors
geom_vline(aes(xintercept = 3, colour="LINE DARK BLUE"), linetype="twodash", size = 1) + # where aes colour is vline label
scale_colour_manual("Line Legend", values="darkblue") + #removed legend title
scale_x_continuous(breaks = seq(0, 202, 10)) +
ggtitle("Time Difference") +
xlab("Time in Days") +
ylab("Amount") +
theme_light() +
theme(panel.grid.minor = element_blank(),
panel.border = element_blank(),
axis.line.x = element_line(size = 0.5, linetype = "solid", colour = "black"),
axis.line.y = element_line(size = 0.5, linetype = "solid", colour = "black"),
legend.position = c(0.95, 0.95),
legend.justification = c("right", "top"),
legend.box.just = ("right"))

最佳答案

我认为 @Jimbou 的建议更可取,但是有一种变通方法可以通过将字符值分配给 geom_histogram aes fill< 来人工创建图例 值和 geom_vline aes colour 值,然后在 scale_fill_manualscale_colour_manual .

然而,使用这种方法 aes fill 只会取一个值(长度为 1),因此您必须将 df 的子集用于蓝色和绿松石色值并为每个值绘制一个直方图,截止值由您的 binwidth 确定。

这是方法。请注意您的数据需要重新格式化。

# reformat data
set.seed(1)
df <- data.frame(runif(1000, 0, 200))
colnames(df) <- "Time_Diff"


bp_overall <- ggplot(data = df)
bp_overall +
geom_histogram(data = subset(df, Time_Diff <= 12.5), aes(x = Time_Diff, fill="BAR BLUE"), binwidth = 5) + # subset for blue data, where aes fill is fill group 1 label
geom_histogram(data = subset(df, Time_Diff > 12.5), aes(x = Time_Diff, fill="BAR TURQUOISE"), binwidth = 5) + # subset for turquoise data, where aes fill is fill group 2 label
scale_fill_manual("Histogram Legend", values=c("blue", "paleturquoise2")) + # manually assign histogram fill colors
geom_vline(aes(xintercept = 3, colour="LINE DARK BLUE"), linetype="twodash", size = 1) + # where aes colour is vline label
scale_colour_manual("Line Legend", values="darkblue") + # manually assign vline colors
scale_x_continuous(breaks = seq(0, 202, 10)) +
ggtitle("Time Difference") +
xlab("Time in Days") +
ylab("Amount") +
theme_light() +
theme(panel.grid.minor = element_blank(),
panel.border = element_blank(),
axis.line.x = element_line(size = 0.5, linetype = "solid", colour = "black"),
axis.line.y = element_line(size = 0.5, linetype = "solid", colour = "black"))

等等。添加剩余的主题

enter image description here

已编辑:回答有关如何统一图例和减少两种图例类型之间间距的问题

(1) 通过在 scale_fill_manual 中设置为 ""来移除 vline 的图例 name,在中将 histogram fill legend name 更改为 "Legend"scale_colour_manual

(2) 指定图例出现的顺序,先填充,然后使用guides guide_legend 填充颜色。

(3) 通过将 legend.spacing.y 设置为 0 来移除两种图例类型之间的 y 间距,并使用 移除顶部和底部的边距>legend.margintheme

bp_overall <- ggplot(data = df) 
bp_overall +
geom_histogram(data = subset(df, Time_Diff <= 12.5), aes(x = Time_Diff, fill="BAR BLUE"), binwidth = 5) +
geom_histogram(data = subset(df, Time_Diff > 12.5), aes(x = Time_Diff, fill="BAR TURQUOISE"), binwidth = 5) +
scale_fill_manual(name="Legend", values=c("blue", "paleturquoise2")) +
geom_vline(aes(xintercept = 3, colour="LINE DARK BLUE"), linetype="twodash", size = 1) +
scale_colour_manual(name="", values="darkblue") +
scale_x_continuous(breaks = seq(0, 202, 10)) +
ggtitle("Time Difference") +
xlab("Time in Days") +
ylab("Amount") +
theme_light() +
theme(panel.grid.minor = element_blank(),
panel.border = element_blank(),
axis.line.x = element_line(size = 0.5, linetype = "solid", colour = "black"),
axis.line.y = element_line(size = 0.5, linetype = "solid", colour = "black"),
legend.spacing.y = unit(0, "cm"),
legend.margin=margin(t=0, r=0.5, b=0, l=0.5, unit="cm")) +
guides(fill = guide_legend(order = 1),
colour = guide_legend(order = 2))

enter image description here

关于r - 将图例添加到具有不同类型美学的 ggplot 直方图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46764729/

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