这里有点像R新手。我试图创建一个条形图,其中有三个单独的条来表示对照组,治疗组和两者组合的正确答案之和,但dodge参数似乎不起作用。我将在这里附上我的R代码。
ggplot(correct.data3, aes(x = Question)) +
geom_bar(aes(y = Both, fill = "Both"), position = 'dodge',
stat = "identity") +
geom_bar(aes(y = Control, fill = "Control"), position = 'dodge',
stat = "identity") +
geom_bar(aes(y = Treatment, fill = "Treatment"), position = 'dodge',
stat = "identity") +
labs(title = "Correct Responses",
y = "Number of Correct Responses",
fill = "Treatment Group") +
theme_bw()
我想使用ggplot2
创建一个条形图,x阅读Q1,Q2,Q3,Q4,每个问题都有三个条形代表对照组和治疗组的正确答案。我尝试使用dodge参数,但条形似乎重叠。我如何解决这个问题?
1条答案
按热度按时间vddsk6oq1#
这不是
ggplot2
和position_dodge
的工作方式。对于position_dodge
,您需要一个包含类别的列,这些类别应该显示为“隐藏”条,而不是三个单独的列。要实现这一点,请使用例如tidyr::pivot_longer
将数据整形为长或整洁的格式。使用一些假随机示例数据: