gpt4 book ai didi

r - R中带有分组列的ggplot2条形图,如何取消堆叠?

转载 作者:行者123 更新时间:2023-12-01 10:32:54 25 4
gpt4 key购买 nike

我正在尝试将数据(见下文)绘制为分组条形图。我已按照此处的其他建议进行更改 position = ""geom_bar ,但我似乎无法让它发挥作用。我得到的只是一个堆积的条形图。这是我最近的尝试:

ggplot(data = IFNg, aes(x = IFNg$Location, y = IFNg$`Fold Change`)) + 
geom_bar(position = 'identity', stat = "identity", fill = IFNg$Day) +
ylab("Fold Change (log_2)") + geom_errorbar(limits, width=0.15)

我还尝试了以下方法,这也会导致堆积图,但这个图的颜色不同:
ggplot(data = IFNg, aes(x = IFNg$Location, y = IFNg$`Fold Change`, 
fill= IFNg$Day)) + geom_bar(position = 'identity', stat = "identity") +
ylab("Fold Change (log_2)") + geom_errorbar(limits, width=0.15)

enter image description here

这是我的其余代码与数据:
dat <- structure(list(Day = c(1L, 3L, 7L, 21L, 1L, 3L, 7L, 21L, 1L, 
3L, 7L, 21L), Order = c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L,
3L, 3L), Target = c("IFN-g", "IFN-g", "IFN-g", "IFN-g", "IFN-g",
"IFN-g", "IFN-g", "IFN-g", "IFN-g", "IFN-g", "IFN-g", "IFN-g"),
Location = c("Duodenum", "Duodenum", "Duodenum", "Duodenum",
"Duodenum (Lymph)", "Duodenum (Lymph)", "Duodenum (Lymph)",
"Duodenum (Lymph)", "Proximal Jejunum", "Proximal Jejunum",
"Proximal Jejunum", "Proximal Jejunum"), `Fold Change` = c(-1.750896098,
-2.194886907, -0.838822724, 0.680612172, -0.375563984, 0.650154987,
0.844897327, -0.228441603, -1.298230671, -1.173634963, -1.877736135,
0.787322978), StDev = c(0.027477611, 0.029974897, 0.19025044, 1.235986647,
0.055798435, 0.086102327, 0.115422155, 0.34470734, 0.020947691, 0.165294027,
0.040111751, 0.035010207)), .Names = c("Day", "Order", "Target", "Location",
"Fold Change", "StDev"), class = c("tbl_df", "data.frame"), row.names = c(NA,-12L))

IFNg <- dat[dat$Target == 'IFN-g', ]

limits <- aes(ymax = IFNg$`Fold Change` + IFNg$StDev,
ymin = IFNg$`Fold Change` - IFNg$StDev)

最佳答案

您的大部分问题都来自您输入了不应该输入的内容,例如 data$column而不是 column .另外,您的 Day是数字,但具有由 Day 定义的不同并排条,它需要是离散的(一个因素)。

我还要指出的是?geom_bar帮助页面并不长,有这一段:

By default, multiple x's occurring in the same place will be stacked atop one another by position_stack. If you want them to be dodged side-to-side, see ?position_dodge. Finally, position_fill shows relative proportions at each x by stacking the bars and then stretching or squashing to the same height.



通常, position = 'dodge'会工作得很好,但它会感到困惑,并认为在这种情况下您的误差条宽度是要躲避的宽度,因此我们将使用完整的 position = position_dodge() :
# No data$ inside aes()!!!
limits <- aes(ymax = `Fold Change` + StDev,
ymin = `Fold Change` - StDev)

ggplot(data = IFNg,
# group lets `ggplot` know we want different errorbars/bars for each day
aes(x = Location, y = `Fold Change`, group = factor(Day))
) +
geom_bar(
stat = "identity",
aes(fill = factor(Day)),
position = position_dodge(width = 0.9)
) +
ylab("Fold Change (log_2)") +
geom_errorbar(
limits,
width = 0.15,
position = position_dodge(width = 0.9)
)

enter image description here

关于r - R中带有分组列的ggplot2条形图,如何取消堆叠?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40475856/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com