2015-05-29 106 views
2

我正在尝试在条形图的每个条上绘制分段,并使用ggplot2。我知道如何用连续的X轴来做到这一点,但不知道这个离散的轴。 我所做的是一种通过组成文本行的“黑客”。在ggplot2中向条形图(离散x轴)添加分段

图片看起来很不错,但我没有得到“限制”金属浓度的图例,并且最重要的是,每次放大或缩小时段的长度都会发生变化。

有谁知道哪个几何可以实现这个更好? enter image description here

df = data.frame('metal'=c("Cu", "Fr", "Zn"), 'observed'=c(550, 60, 100), 'limit'=c(200, 150, 120)) 
ggplot(data=df) + aes(x=metal) + 
    geom_bar(aes(y=observed), stat="identity", fill="grey") + 
    geom_text(aes(y=limit, label="_____________"), size=rel(6), color="red") 

编辑:

的问题是接近this一个

+0

使用'geom_errorbar(AES(Y =极限,YMIN =极限,YMAX =极限))'HTTP改编:// stackoverflow.com/questions/30158225/how-to-add-a-horizo​​ntal-line-above-a-bar-chart-using-ggplot – scoa

+0

@scoa感谢它完美的作品。你有没有关于如何获得“极限”传说的线索? – agenis

回答

1

this answer

ggplot(data=df) + aes(x=metal) + 
    geom_bar(aes(y=observed), stat="identity", fill="grey") + 
    geom_errorbar(aes(y=limit,ymin=limit,ymax=limit,colour="limit")) 

enter image description here

1

这是一种方法。

ggplot(data=df) + aes(x=metal) + 
    geom_bar(aes(y=observed), stat="identity", fill="grey", color = "grey60") + 
    geom_bar(data = df, aes(y=limit), stat="identity", fill="transparent", color = "grey30") + 
    geom_text(aes(y = limit +5), label = df$limit) 

enter image description here

+0

(+1)thanks @lawyeR这是一个非常明亮的建议,显示限制矩形。我想接受这两个答案,但是scoa的答案与最初的请求最接近并显示我的传奇。 – agenis