为什么 ggplot2 中的 ‘dodge’ 命令对我无效?

huangapple go评论60阅读模式
英文:

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

这不是 ggplot2position_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 &lt;- data.frame(
  Question = paste0(&quot;Q&quot;, 1:4),
  Both = sample(1:10, 4),
  Control = sample(1:10, 4),
  Treatment = sample(1:10, 4)
)

correct.data3 |&gt;
  tidyr::pivot_longer(-Question, names_to = &quot;cat&quot;, values_to = &quot;n&quot;) |&gt;
  ggplot(aes(x = Question)) +
  geom_col(aes(y = n, fill = cat), position = &quot;dodge&quot;) +
  labs(
    title = &quot;Correct Responses&quot;,
    y = &quot;Number of Correct Responses&quot;,
    fill = &quot;Treatment Group&quot;
  ) +
  theme_bw()

为什么 ggplot2 中的 ‘dodge’ 命令对我无效?<!-- -->

huangapple
  • 本文由 发表于 2023年4月4日 07:06:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75924336.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定