[英]Add legend to ggplot histogram with different types of aesthetics
我想為我的一個情節添加一個圖例,但我有不同的美學,我從未創造過一個傳奇,所以我發現很難確定如何構建它。
我的一個美學是填充代碼,我手動添加為矢量。 另一種美學是我用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成為一個整體(直方圖圖例和線條圖例組合成一個連貫的整體)。 有人有建議嗎?
# 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_manual
或scale_colour_manual
設置顏色, scale_fill_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"))
等等。添加你剩下的theme
。
編輯:回答有關如何統一圖例和減少兩個圖例類型之間的間距的問題
(1)拆下傳奇name
通過設置V線,以“”中scale_fill_manual
,變化柱狀圖填充圖例name
為“傳說中的” scale_colour_manual
。
(2)指定圖例應出現的order
,首先使用guides
guide_legend
填充然后填充顏色。
(3)通過設置拆下兩個圖例類型之間的y間隔legend.spacing.y
到0
,以及頂部和底部使用上去除邊緣legend.margin
在theme
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))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.