ggplot2:geom_bar 组,position_dodge 和填充
ggplot2: geom_bar with group, position_dodge and fill
我正在尝试生成一个条形图,使 x 轴按患者划分,每个患者都有多个样本。因此,例如(使用 mtcars 数据作为数据外观的模板):
library("ggplot2")
ggplot(mtcars, aes(x = factor(cyl), group = factor(gear))) +
geom_bar(position = position_dodge(width = 0.8), binwidth = 25) +
xlab("Patient") +
ylab("Number of Mutations per Patient Sample")
这会产生这样的结果:
每个条形图代表每个患者的一个样本。
我想通过使用颜色填充条形图(例如,每个患者样本中不同类型的突变)来添加有关每个患者样本的附加信息。我在想我可以像这样指定填充参数:
ggplot(mtcars, aes(x = factor(cyl), group = factor(gear), fill = factor(vs))) +
geom_bar(position = position_dodge(width = 0.8), binwidth = 25) +
xlab("Patient") +
ylab("Number of Mutations per Patient Sample")
但这不会为每个患者样本条形图生成 "stacked barplots"。我假设这是因为 position_dodge() 已设置。无论如何要解决这个问题?基本上,我想要的是:
ggplot(mtcars, aes(x = factor(cyl), fill = factor(vs))) +
geom_bar() +
xlab("Patient") +
ylab("Number of Mutations per Patient Sample")
但是在我列出的第一个图中可以使用这些颜色。这对 ggplot2 可行吗?
如果我正确理解你的问题,你想将 aes() 传递到你的 geom_bar 层。这将允许您通过填充美学。然后,您可以将条形放置为 "dodge" 或 "fill",具体取决于您希望如何显示数据。
这里列出了一个简短的例子:
ggplot(mtcars, aes(x = factor(cyl), fill = factor(vs))) +
geom_bar(aes(fill = factor(vs)), position = "dodge", binwidth = 25) +
xlab("Patient") +
ylab("Number of Mutations per Patient Sample")
结果图:http://imgur.com/ApUJ4p2(抱歉 S/O 还不允许我 post 图片)
希望对您有所帮助!
我认为构面最接近您要寻找的内容:
ggplot(mtcars, aes(x = factor(gear), fill = factor(vs))) +
geom_bar(position = position_dodge(width = 0.8), binwidth = 25) +
xlab("Patient") +
ylab("Number of Mutations per Patient Sample") +
facet_wrap(~cyl)
我在 ggplot2 的问题跟踪器中没有找到任何相关内容。
我想,我在这个 post 中的回答将帮助您为每个患者构建包含多个堆叠垂直条的图表 ...
Layered axes in ggplot?
我已经通过按我喜欢的顺序将多个 geom_cols 层叠在一起来绕过这个问题几次。例如,代码
ggplot(data, aes(x=cert, y=pct, fill=Party, group=Treatment, shape=Treatment)) +
geom_col(aes(x=cert, y=1), position=position_dodge(width=.9), fill="gray90") +
geom_col(position=position_dodge(width=.9)) +
scale_fill_manual(values=c("gray90", "gray60"))
允许我在没有刻面的情况下生成您正在寻找的功能。请注意我是如何将背景图层的 y 值设置为 1 的。要添加更多图层,您只需对变量求和即可。
情节图片:
我正在尝试生成一个条形图,使 x 轴按患者划分,每个患者都有多个样本。因此,例如(使用 mtcars 数据作为数据外观的模板):
library("ggplot2")
ggplot(mtcars, aes(x = factor(cyl), group = factor(gear))) +
geom_bar(position = position_dodge(width = 0.8), binwidth = 25) +
xlab("Patient") +
ylab("Number of Mutations per Patient Sample")
这会产生这样的结果:
每个条形图代表每个患者的一个样本。
我想通过使用颜色填充条形图(例如,每个患者样本中不同类型的突变)来添加有关每个患者样本的附加信息。我在想我可以像这样指定填充参数:
ggplot(mtcars, aes(x = factor(cyl), group = factor(gear), fill = factor(vs))) +
geom_bar(position = position_dodge(width = 0.8), binwidth = 25) +
xlab("Patient") +
ylab("Number of Mutations per Patient Sample")
但这不会为每个患者样本条形图生成 "stacked barplots"。我假设这是因为 position_dodge() 已设置。无论如何要解决这个问题?基本上,我想要的是:
ggplot(mtcars, aes(x = factor(cyl), fill = factor(vs))) +
geom_bar() +
xlab("Patient") +
ylab("Number of Mutations per Patient Sample")
但是在我列出的第一个图中可以使用这些颜色。这对 ggplot2 可行吗?
如果我正确理解你的问题,你想将 aes() 传递到你的 geom_bar 层。这将允许您通过填充美学。然后,您可以将条形放置为 "dodge" 或 "fill",具体取决于您希望如何显示数据。
这里列出了一个简短的例子:
ggplot(mtcars, aes(x = factor(cyl), fill = factor(vs))) +
geom_bar(aes(fill = factor(vs)), position = "dodge", binwidth = 25) +
xlab("Patient") +
ylab("Number of Mutations per Patient Sample")
结果图:http://imgur.com/ApUJ4p2(抱歉 S/O 还不允许我 post 图片)
希望对您有所帮助!
我认为构面最接近您要寻找的内容:
ggplot(mtcars, aes(x = factor(gear), fill = factor(vs))) +
geom_bar(position = position_dodge(width = 0.8), binwidth = 25) +
xlab("Patient") +
ylab("Number of Mutations per Patient Sample") +
facet_wrap(~cyl)
我在 ggplot2 的问题跟踪器中没有找到任何相关内容。
我想,我在这个 post 中的回答将帮助您为每个患者构建包含多个堆叠垂直条的图表 ...
Layered axes in ggplot?
我已经通过按我喜欢的顺序将多个 geom_cols 层叠在一起来绕过这个问题几次。例如,代码
ggplot(data, aes(x=cert, y=pct, fill=Party, group=Treatment, shape=Treatment)) +
geom_col(aes(x=cert, y=1), position=position_dodge(width=.9), fill="gray90") +
geom_col(position=position_dodge(width=.9)) +
scale_fill_manual(values=c("gray90", "gray60"))
允许我在没有刻面的情况下生成您正在寻找的功能。请注意我是如何将背景图层的 y 值设置为 1 的。要添加更多图层,您只需对变量求和即可。
情节图片: