我正在绘制ggplot中某些类别的值和相应的误差范围,然后添加特定样本的值 . 这是一个例子:
# Example data
dataM <- data.frame(
category = c('A', 'B', 'C', 'D'),
value = c(0.075, 0.090, 0.146, 0.070),
error = c(0.008, 0.013, 0.018, 0.019) )
dataP <- data.frame(
category = c('A', 'B', 'C', 'D'),
value = c(0.079, 0.097, 0.110, 0.080) )
# Basic plot
library(ggplot2)
ggplot(dataM, aes(category, value)) +
geom_bar(stat = 'identity', width = .66 ) +
geom_errorbar(aes(ymin = value-error, ymax = value+error), width = .5) +
geom_point(data = dataP, aes(y = value), shape = 21, size = 4, color = 'darkblue', fill = 'lightblue') +
labs(x= NULL, y = NULL)
我需要添加一个图例来说明每个图层/几何图形 . 我可以添加颜色,填充和形状的比例,并使用:
ggplot(dataM, aes(category, value)) +
geom_bar(aes(fill = 'value: '), stat = 'identity', width = .66 ) +
geom_errorbar(aes(ymin = value-error, ymax = value+error, color = 'error: '), width = .5) +
geom_point(data = dataP, aes(y = value, shape = 'pct value: '), size = 4, color = 'darkblue', fill = 'lightblue') +
labs(x= NULL, y = NULL) +
scale_fill_manual(NULL, values = '#595959') +
scale_color_manual(NULL, values = 'black') +
scale_shape_manual(NULL, values = 21) +
theme(legend.position = 'bottom') +
guides(fill = guide_legend(order = 1, label.position = 'left'), color = guide_legend(order = 2, label.position = 'left'), shape = guide_legend(order = 3, label.position = 'left'))
问题在于,图例中的黑线与图形中的误差线相对应并不是很明显 . 理想情况下,图例看起来像这样:
所以问题是:可以添加引用层的图例指南而不是ggplot中的美学元素吗?图表和数据的类型是无关紧要的,我只是做了一个具有不同几何形状的简单示例 . 关键是要做一个更好的传奇 .
1 回答
I agree with @Tjebo on the use of bar charts. Just because it's (regrettably often) done, doesn't mean it's a good idea;-)
我推荐一个带有抖动点的小提琴情节,以显示分布和实际点以及任何其他功能,特别是对于非专业 Spectator .
以下是基于一些示例数据的示例