在 ggplot 上覆盖胡须或错误条线
Overlaying whiskers or error-bar-esque lines on a ggplot
我正在创建类似于下面第一个示例图像的图,并且需要像下面第二个示例那样的图。
library(ggplot2)
library(scales)
# some data
data.2015 = data.frame(score = c(-50,20,15,-40,-10,60),
area = c("first","second","third","first","second","third"),
group = c("Findings","Findings","Findings","Benchmark","Benchmark","Benchmark"))
data.2014 = data.frame(score = c(-30,40,-15),
area = c("first","second","third"),
group = c("Findings","Findings","Findings"))
# breaks and limits
breaks.major = c(-60,-40,-22.5,-10, 0,10, 22.5, 40, 60)
breaks.minor = c(-50,-30,-15,-5,0, 5, 15,30,50)
limits =c(-70,70)
# plot 2015 data
ggplot(data.2015, aes(x = area, y = score, fill = group)) +
geom_bar(stat = "identity", position = position_dodge(width = 0.9)) +
coord_flip() +
scale_y_continuous(limit = limits, oob = squish, minor_breaks = breaks.minor, breaks = breaks.major)
data.2014 只有 "Findings" 组的值。我想在 appropriate/corresponding data.2015$ 区域的绘图上显示那些 2014 年的调查结果值,那里有 2014 年的数据可用。
为了仅在 "Finding"(红色条)数据上显示去年的数据,我想使用来自相关数据值的单边 errorbar/whisker。2015栏,并终止于 data.2014 值,例如:
我想通过使用图层和绘制误差条来做到这一点,这样 2015 年的数据就可以重叠,但是当 2014 年的结果 abs() 小于 2015 年的结果并因此被遮挡时,这不起作用。
注意事项:
- 我希望 errorbar/whisker 与条形宽度相同,甚至可以是带实心帽的虚线。
- 值减少时红线加分,值增加时加绿线
- 我在一个循环中生成了很多这样的图,有时有很多组,每个图中的区域数量不同。 2014年的数据(现阶段)总是只显示一个组,每个地区都有一些数据(除了只有一个NA案例,但需要为那个场景做准备)
编辑
所以我已经添加到下面的解决方案中,我使用了那个确切的代码,而是使用了 geom_linerange
以便它会添加没有大写的行,然后我还使用了 geom_errorbar
,但是 ymin 和 ymax 设置为相同的值,所以结果是 ggplot
geom_bar
中的单边误差线!谢谢您的帮助。
我相信你可以通过一些数据操作来获得你想要的大部分内容。对两个数据集进行外部连接将使您可以添加带有适当闪避的误差线。
alldat = merge(data.2015, data.2014, all = TRUE, by = c("area", "group"),
suffixes = c(".2015", ".2014"))
要使误差线单向显示,您需要 ymin
与 y
或 NA
相同,具体取决于组。似乎最容易创建一个新变量,我称之为 plotscore
,以实现此目的。
alldat$plotscore = with(alldat, ifelse(is.na(score.2014), NA, score.2015))
我做的最后一件事是为 2015 年分数与 2014 年相比下降与上升的时间创建一个变量 direction
。我将 Benchmark
组的第三个类别作为填充物,因为我 运行在没有它的情况下躲避一些问题。
alldat$direction = with(alldat, ifelse(score.2015 < score.2014, "dec", "inc"))
alldat$direction[is.na(alldat$score.2014)] = "absent"
用于绘图的数据集如下所示:
area group score.2015 score.2014 plotscore direction
1 first Benchmark -40 NA NA absent
2 first Findings -50 -30 -50 dec
3 second Benchmark -10 NA NA absent
4 second Findings 20 40 20 dec
5 third Benchmark 60 NA NA absent
6 third Findings 15 -15 15 inc
我使用的最终代码如下所示:
ggplot(alldat, aes(x = area, y = score.2015, fill = group)) +
geom_bar(stat = "identity", position = position_dodge(width = 0.9)) +
geom_errorbar(aes(ymin = plotscore, ymax = score.2014, color = direction),
position = position_dodge(width = .9), lwd = 1.5, show.legend = FALSE) +
coord_flip() +
scale_y_continuous(limit = limits, oob = squish, minor_breaks = breaks.minor, breaks = breaks.major) +
scale_color_manual(values = c(NA, "red", "green"))
我正在使用 ggplot2、ggplot2_1.0.1.9002 的开发版本,show_guide
现在已被弃用,取而代之的是 show.legend
,我在 geom_errorbar
.
中使用
我显然没有将误差线的线型更改为带有实线帽的虚线,也没有移除底部的胡须,因为我不知道做这些事情的简单方法。
回应建议我添加完整解决方案作为答案的评论:
library(ggplot2)
library(scales)
# some data
data.2015 = data.frame(score = c(-50,20,15,-40,-10,60),
area = c("first","second","third","first","second","third"),
group = c("Findings","Findings","Findings","Benchmark","Benchmark","Benchmark"))
data.2014 = data.frame(score = c(-30,40,-15),
area = c("first","second","third"),
group = c("Findings","Findings","Findings"))
# breaks and limits
breaks.major = c(-60,-40,-22.5,-10, 0,10, 22.5, 40, 60)
breaks.minor = c(-50,-30,-15,-5,0, 5, 15,30,50)
limits =c(-70,70)
# reconfigure data to create values for the additional errorbar/linerange
alldat = merge(data.2015, data.2014, all = TRUE, by = c("area", "group"),
suffixes = c(".2015", ".2014"))
alldat$plotscore = with(alldat, ifelse(is.na(score.2014), NA, score.2015))
alldat$direction = with(alldat, ifelse(score.2015 < score.2014, "dec", "inc"))
alldat$direction[is.na(alldat$score.2014)] = "absent"
ggplot(alldat, aes(x = area, y = score.2015, fill = group)) +
geom_bar(stat = "identity", position = position_dodge(width = 0.9)) +
# set the data min and max as the same to have a single 'cap' with no line
geom_errorbar(aes(ymin = score.2014, ymax = score.2014, color = direction),
position = position_dodge(width = .9), lwd = 1.5, show.legend = FALSE) +
#then add the line
geom_linerange(aes(ymin = score.2015, ymax = score.2014, color = direction),
position = position_dodge(width = .9), lwd = 1.5, show.legend = FALSE) +
coord_flip() +
scale_y_continuous(limit = limits, oob = squish, minor_breaks = breaks.minor, breaks = breaks.major) +
scale_color_manual(values = c(NA, "red", "green"))
我正在创建类似于下面第一个示例图像的图,并且需要像下面第二个示例那样的图。
library(ggplot2)
library(scales)
# some data
data.2015 = data.frame(score = c(-50,20,15,-40,-10,60),
area = c("first","second","third","first","second","third"),
group = c("Findings","Findings","Findings","Benchmark","Benchmark","Benchmark"))
data.2014 = data.frame(score = c(-30,40,-15),
area = c("first","second","third"),
group = c("Findings","Findings","Findings"))
# breaks and limits
breaks.major = c(-60,-40,-22.5,-10, 0,10, 22.5, 40, 60)
breaks.minor = c(-50,-30,-15,-5,0, 5, 15,30,50)
limits =c(-70,70)
# plot 2015 data
ggplot(data.2015, aes(x = area, y = score, fill = group)) +
geom_bar(stat = "identity", position = position_dodge(width = 0.9)) +
coord_flip() +
scale_y_continuous(limit = limits, oob = squish, minor_breaks = breaks.minor, breaks = breaks.major)
data.2014 只有 "Findings" 组的值。我想在 appropriate/corresponding data.2015$ 区域的绘图上显示那些 2014 年的调查结果值,那里有 2014 年的数据可用。
为了仅在 "Finding"(红色条)数据上显示去年的数据,我想使用来自相关数据值的单边 errorbar/whisker。2015栏,并终止于 data.2014 值,例如:
我想通过使用图层和绘制误差条来做到这一点,这样 2015 年的数据就可以重叠,但是当 2014 年的结果 abs() 小于 2015 年的结果并因此被遮挡时,这不起作用。
注意事项:
- 我希望 errorbar/whisker 与条形宽度相同,甚至可以是带实心帽的虚线。
- 值减少时红线加分,值增加时加绿线
- 我在一个循环中生成了很多这样的图,有时有很多组,每个图中的区域数量不同。 2014年的数据(现阶段)总是只显示一个组,每个地区都有一些数据(除了只有一个NA案例,但需要为那个场景做准备)
编辑
所以我已经添加到下面的解决方案中,我使用了那个确切的代码,而是使用了 geom_linerange
以便它会添加没有大写的行,然后我还使用了 geom_errorbar
,但是 ymin 和 ymax 设置为相同的值,所以结果是 ggplot
geom_bar
中的单边误差线!谢谢您的帮助。
我相信你可以通过一些数据操作来获得你想要的大部分内容。对两个数据集进行外部连接将使您可以添加带有适当闪避的误差线。
alldat = merge(data.2015, data.2014, all = TRUE, by = c("area", "group"),
suffixes = c(".2015", ".2014"))
要使误差线单向显示,您需要 ymin
与 y
或 NA
相同,具体取决于组。似乎最容易创建一个新变量,我称之为 plotscore
,以实现此目的。
alldat$plotscore = with(alldat, ifelse(is.na(score.2014), NA, score.2015))
我做的最后一件事是为 2015 年分数与 2014 年相比下降与上升的时间创建一个变量 direction
。我将 Benchmark
组的第三个类别作为填充物,因为我 运行在没有它的情况下躲避一些问题。
alldat$direction = with(alldat, ifelse(score.2015 < score.2014, "dec", "inc"))
alldat$direction[is.na(alldat$score.2014)] = "absent"
用于绘图的数据集如下所示:
area group score.2015 score.2014 plotscore direction
1 first Benchmark -40 NA NA absent
2 first Findings -50 -30 -50 dec
3 second Benchmark -10 NA NA absent
4 second Findings 20 40 20 dec
5 third Benchmark 60 NA NA absent
6 third Findings 15 -15 15 inc
我使用的最终代码如下所示:
ggplot(alldat, aes(x = area, y = score.2015, fill = group)) +
geom_bar(stat = "identity", position = position_dodge(width = 0.9)) +
geom_errorbar(aes(ymin = plotscore, ymax = score.2014, color = direction),
position = position_dodge(width = .9), lwd = 1.5, show.legend = FALSE) +
coord_flip() +
scale_y_continuous(limit = limits, oob = squish, minor_breaks = breaks.minor, breaks = breaks.major) +
scale_color_manual(values = c(NA, "red", "green"))
我正在使用 ggplot2、ggplot2_1.0.1.9002 的开发版本,show_guide
现在已被弃用,取而代之的是 show.legend
,我在 geom_errorbar
.
我显然没有将误差线的线型更改为带有实线帽的虚线,也没有移除底部的胡须,因为我不知道做这些事情的简单方法。
回应建议我添加完整解决方案作为答案的评论:
library(ggplot2)
library(scales)
# some data
data.2015 = data.frame(score = c(-50,20,15,-40,-10,60),
area = c("first","second","third","first","second","third"),
group = c("Findings","Findings","Findings","Benchmark","Benchmark","Benchmark"))
data.2014 = data.frame(score = c(-30,40,-15),
area = c("first","second","third"),
group = c("Findings","Findings","Findings"))
# breaks and limits
breaks.major = c(-60,-40,-22.5,-10, 0,10, 22.5, 40, 60)
breaks.minor = c(-50,-30,-15,-5,0, 5, 15,30,50)
limits =c(-70,70)
# reconfigure data to create values for the additional errorbar/linerange
alldat = merge(data.2015, data.2014, all = TRUE, by = c("area", "group"),
suffixes = c(".2015", ".2014"))
alldat$plotscore = with(alldat, ifelse(is.na(score.2014), NA, score.2015))
alldat$direction = with(alldat, ifelse(score.2015 < score.2014, "dec", "inc"))
alldat$direction[is.na(alldat$score.2014)] = "absent"
ggplot(alldat, aes(x = area, y = score.2015, fill = group)) +
geom_bar(stat = "identity", position = position_dodge(width = 0.9)) +
# set the data min and max as the same to have a single 'cap' with no line
geom_errorbar(aes(ymin = score.2014, ymax = score.2014, color = direction),
position = position_dodge(width = .9), lwd = 1.5, show.legend = FALSE) +
#then add the line
geom_linerange(aes(ymin = score.2015, ymax = score.2014, color = direction),
position = position_dodge(width = .9), lwd = 1.5, show.legend = FALSE) +
coord_flip() +
scale_y_continuous(limit = limits, oob = squish, minor_breaks = breaks.minor, breaks = breaks.major) +
scale_color_manual(values = c(NA, "red", "green"))