[英]ggplot: position=“dodge” question
我在下面有一個相當簡單的data.frame dput(x):
x <- structure(list(variable = c("a", "b", "c", "d", "e", "f", "g",
"h", "i", "j", "k", "l", "m", "n", "o", "p"), top2 = c(0.51,
0.24, 0.55, 0.36, 0.56, 0.67, 0.36, 0.22, 0.48, 0.6, 0.59, 0.06,
0.04, 0.2, 0.26, 0.25), bottom = c(0.03, 0.05, 0.03, 0.01, 0.02,
0.03, 0.01, 0.05, 0.03, 0, 0.03, 0.2, 0.11, 0.06, 0.16, 0.07)), .Names = c("variable",
"top2", "bottom"), row.names = c(NA, -16L), class = "data.frame")
我想做的是創建一個圖表,為每個“變量”顯示top2和bottom彼此相鄰。 我正在嘗試實現position =“ dodge”,但到目前為止,ggplot2似乎很大程度上忽略了它
ggplot() + geom_bar(aes(variable, top2), data=x, position="dodge") +
geom_bar(aes(x$variable, x$bottom), data=x, position="dodge", fill="pink") +
coord_flip()
粉紅色不停留:P
這是一個快速答案
df = melt(x, id = 'variable');
names(df) = c('variable', 'topbot', 'value');
pl1 = ggplot(df, aes(x = variable, y = value)) +
geom_bar(aes(fill = topbot, position = 'dodge')) +
coord_flip() +
scale_fill_manual(values = c('red', 'blue'));
print(pl1)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.