堆叠的barplot [英] Stacked barplot
问题描述
x < - 结构(列表) (变量= c(a,b,c),f = c(0.98,0.66,0.34),m = c(0.75760989010989,0.24890977443609,0.175125)),.Names = c(variable f,m),row.names = c(NA,3L),class =data.frame)
> x
变量fm
1 a 0.98 0.7576099
2 b 0.66 0.2489098
3 c 0.34 0.1751250
以及示例运行以下代码时:
ggplot(x,aes(variable ,f,label = variable))+
geom_bar()+ geom_bar(aes(variable,m),fill =purple)
下面的堆积条形图显示了......
轮到我了!以下是数据的数据框架:
>数据
asset.std asset.dstd符号
IEF 0.00470368279997122 0.00477691934631662 IEF
SPY 0.0119358320227236 0.0130162006829043 SPY
GSG 0.0137798134700255 0.0147096635302501 GSG
VNQ 0.016058588692544 0.0169327904112519 VNQ
TLT 0.0108803682930942 0.0109165197621356 TLT
SHY 0.000635574928974698 0.000676146828833939 SHY
我运行以下代码
ggplot(data,aes(symbols,asset.std,label = symbols))+
geom_bar()+ geom_bar(aes(symbols ,asset.dstd),fill =blue)
是吧?
上午我在ggplot2代码中缺少某些东西?任何事情都可以帮助thxs
查看您的对象 data
:
DF [1] - DF [2]
#asset.std
#IEF -7.323655e-05
#SPY -1.080369e-03
#GSG -9.298501e-04
#VNQ -8.742017e-04
#TLT -3.615147e-05
#SHY -4.057190e-05
在所有情况下, asset.std
小于 asset.dstd
;因此,如果您首先绘制 asset.std
,那么当您绘制第二列时,您完全可以掩盖第一个图!
要复制您提供的示例,请首先绘制 asset.dstd
:
ggplot(DF,aes(symbols,asset.dstd,label = symbols))+
geom_bar(fill =red)+
geom_bar(aes符号,asset.std),fill =blue,position =stack)
然而,这是不是堆叠条形图,因为这个术语是常用的。
I took the following sample code from another similar question and tried to replicate it.
x <- structure(list(variable = c("a", "b", "c"), f = c(0.98, 0.66, 0.34), m = c(0.75760989010989, 0.24890977443609, 0.175125)), .Names = c("variable","f", "m"), row.names = c(NA, 3L), class = "data.frame")
> x
variable f m
1 a 0.98 0.7576099
2 b 0.66 0.2489098
3 c 0.34 0.1751250
and when the example ran the following code:
ggplot(x, aes(variable, f,label=variable)) +
geom_bar() + geom_bar(aes(variable, m), fill="purple")
the following stacked bar char shows...
My turn! The following is a data frame of data
> data
asset.std asset.dstd symbols
IEF 0.00470368279997122 0.00477691934631662 IEF
SPY 0.0119358320227236 0.0130162006829043 SPY
GSG 0.0137798134700255 0.0147096635302501 GSG
VNQ 0.016058588692544 0.0169327904112519 VNQ
TLT 0.0108803682930942 0.0109165197621356 TLT
SHY 0.000635574928974698 0.000676146828833939 SHY
and i run the following code
ggplot(data, aes(symbols, asset.std, label=symbols))+
geom_bar() + geom_bar(aes(symbols, asset.dstd),fill="blue")
and i get this instead....huh?
Am i missing something in my ggplot2 code? Anything would help thxs
Looking at your object data
:
DF[1] - DF[2]
# asset.std
# IEF -7.323655e-05
# SPY -1.080369e-03
# GSG -9.298501e-04
# VNQ -8.742017e-04
# TLT -3.615147e-05
# SHY -4.057190e-05
In all cases, asset.std
is less than asset.dstd
; thus, if you plotted asset.std
first, when you plot the second column on top of that, you would just entirely cover up the first plot!
To replicate the example you've provided, plot asset.dstd
first:
ggplot(DF, aes(symbols, asset.dstd, label=symbols)) +
geom_bar(fill="red") +
geom_bar(aes(symbols, asset.std), fill="blue", position="stack")
Note, however, that this is not a stacked bar chart in the sense that the term is commonly used.
这篇关于堆叠的barplot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!