SHW*_*SHW 3 r legend histogram ggplot2 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))
Run Code Online (Sandbox Code Playgroud)
在Djork的建议之后,我进入了以下脚本,该脚本有效,我很满意。我现在要完成的唯一一件事就是使图例成为一个整体(直方图图例和行图例合并为一个连贯的整体)。有人有建议吗?
# 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"))
Run Code Online (Sandbox Code Playgroud)
我认为@Jimbou的建议更可取,但是存在一种变通方法,可以通过将字符值分配给geom_histogram aes fillvalue和geom_vline aes colourvalue,然后在scale_fill_manualor中设置颜色来人为地创建图例scale_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"))
Run Code Online (Sandbox Code Playgroud)
等添加您剩余的theme。
编辑:回答有关如何统一图例并减小两种图例类型之间的间距的问题
(1)name通过在中设置为“”来删除vline的图例,在中将scale_fill_manual直方图填充图例更改name为“ Legend” scale_colour_manual。
(2)指定order应在其中显示图例的位置,先填充,然后使用着色guides guide_legend。
(3)通过设置legend.spacing.y为来0删除两个图例类型之间的y-spacing ,并使用legend.marginin 删除顶部和底部的边距。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))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1500 次 |
| 最近记录: |