英文:
Why is the 'dodge' command in ggplot2 not working for me?
问题
I can help you with the translation. Here's the translated content you requested:
"我是一个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 = "正确答案",
y = "正确答案数量",
fill = "治疗组") +
theme_bw()
我想使用ggplot2
创建一个条形图,X轴读取Q1、Q2、Q3、Q4,每个问题有三个条形,分别代表两者、对照组和治疗组的正确答案。我尝试使用dodge
参数,但似乎条形图重叠在一起。我该如何解决这个问题?"
英文:
Sort of a R rookie here. I'm trying to create a bar chart with three separate bars to represent the sum of correct answers for the control, treatment and both combined but the dodge argument doesn't seem to be working. I'll attach my R code here.
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()
I wanted to use ggplot2
to create a bar chart with the x-axis reading Q1, Q2, Q3, Q4 and for each question to have three bars representing the correct responses for both, control and treatment groups. I tried using the dodge argument but the bars seemingly overlap. How can I solve this?
答案1
得分: 2
这不是 ggplot2
和 position_dodge
的工作方式。对于 position_dodge
,您需要一个包含应该显示为“dodged”条形的类别的列,而不是三个单独的列。 为了实现这一点,请使用例如 tidyr::pivot_longer
将数据重塑为长格式或整洁格式。
使用一些虚构的随机示例数据:
library(ggplot2)
set.seed(123)
correct.data3 <- data.frame(
Question = paste0("Q", 1:4),
Both = sample(1:10, 4),
Control = sample(1:10, 4),
Treatment = sample(1:10, 4)
)
correct.data3 |>
tidyr::pivot_longer(-Question, names_to = "cat", values_to = "n") |>
ggplot(aes(x = Question)) +
geom_col(aes(y = n, fill = cat), position = "dodge") +
labs(
title = "Correct Responses",
y = "Number of Correct Responses",
fill = "Treatment Group"
) +
theme_bw()
英文:
That's not how ggplot2
and position_dodge
works. For position_dodge
you need one column containing the categories which should be displayed as "dodged" bars not three separate columns. To achieve that reshape your data to long or tidy format using e.g. tidyr::pivot_longer
.
Using some fake random example data:
library(ggplot2)
set.seed(123)
correct.data3 <- data.frame(
Question = paste0("Q", 1:4),
Both = sample(1:10, 4),
Control = sample(1:10, 4),
Treatment = sample(1:10, 4)
)
correct.data3 |>
tidyr::pivot_longer(-Question, names_to = "cat", values_to = "n") |>
ggplot(aes(x = Question)) +
geom_col(aes(y = n, fill = cat), position = "dodge") +
labs(
title = "Correct Responses",
y = "Number of Correct Responses",
fill = "Treatment Group"
) +
theme_bw()
<!-- -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论