在R中制作并排的条形图。

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

Side by side barplots in r

问题

我有一个看起来像这样的数据集
在R中制作并排的条形图。

我想创建并列的柱状图,以显示两个等级之间的差异,我构想的是这样的
在R中制作并排的条形图。

任何帮助都将不胜感激。

英文:

I have a data set that looks like this
在R中制作并排的条形图。

I want to create side by side barplots that will show the differences between the two grades, I envisioned something like this

在R中制作并排的条形图。

Any help would be highly appreciated.

答案1

得分: 1

在基本的R中,您可以使用barplot通过一系列的par(new = TRUE)ifelse逻辑来迭代地叠加值。您可以使用类似的逻辑使用text来添加文本:

  1. par(mfrow = c(1,2)) # 并排绘图
  2. # 左侧图
  3. barplot(df$Grade_Semester1, names.arg = df$Person, horiz = TRUE, col = "grey", xlim = c(0, 10))
  4. # 右侧图
  5. barplot(ifelse(df$Difference < 0, df$Grade_Semester1, 0),
  6. names.arg = df$Person, horiz = TRUE, col = "red", xlim = c(0, 10))
  7. par(new = TRUE)
  8. barplot(ifelse(df$Difference > 0, df$Grade_Semester2, 0),
  9. names.arg = df$Person, horiz = TRUE, col = "green", xlim = c(0, 10))
  10. par(new = TRUE)
  11. xx <- barplot(pmin(df$Grade_Semester1, df$Grade_Semester2),
  12. names.arg = df$Person, horiz = TRUE, col = "grey", xlim = c(0, 10))
  13. text(pmin(df$Grade_Semester1, df$Grade_Semester2) + abs(pmin(df$Difference)/2),
  14. xx, ifelse(df$Difference == 0, "", ifelse(df$Difference > 0, paste0("+", df$Difference), df$Difference)))

数据:

  1. df <- data.frame(Person = c("A", "B", "C"),
  2. Grade_Semester1 = c(9, 10, 8),
  3. Grade_Semester2 = c(7, 10, 10),
  4. Difference = c(-2, 0, 2))

在R中制作并排的条形图。

英文:

In base R, you can use barplot to iteratively lay over the values through a series of par(new = TRUE) and ifelse logic. You can add the text with similar logic using text:

  1. par(mfrow = c(1,2)) #side by side plot
  2. # left plot
  3. barplot(df$Grade_Semester1, names.arg = df$Person, horiz = TRUE, col = &quot;grey&quot;, xlim = c(0, 10))
  4. # right plot
  5. barplot(ifelse(df$Difference &lt; 0, df$Grade_Semester1, 0),
  6. names.arg = df$Person, horiz = TRUE, col = &quot;red&quot;, xlim = c(0, 10))
  7. par(new = TRUE)
  8. barplot(ifelse(df$Difference &gt; 0, df$Grade_Semester2, 0),
  9. names.arg = df$Person, horiz = TRUE, col = &quot;green&quot;, xlim = c(0, 10))
  10. par(new = TRUE)
  11. xx &lt;- barplot(pmin(df$Grade_Semester1, df$Grade_Semester2),
  12. names.arg = df$Person, horiz = TRUE, col = &quot;grey&quot;, xlim = c(0, 10))
  13. text(pmin(df$Grade_Semester1, df$Grade_Semester2) + abs(pmin(df$Difference)/2),
  14. xx, ifelse(df$Difference == 0, &quot;&quot;, ifelse(df$Difference &gt; 0, paste0(&quot;+&quot;, df$Difference), df$Difference)))

在R中制作并排的条形图。

Data

  1. df &lt;- data.frame(Person = c(&quot;A&quot;, &quot;B&quot;, &quot;C&quot;),
  2. Grade_Semester1 = c(9, 10, 8),
  3. Grade_Semester2 = c(7, 10, 10),
  4. Difference = c(-2, 0, 2))

huangapple
  • 本文由 发表于 2023年7月28日 06:08:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76783700.html
匿名

发表评论

匿名网友

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

确定