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

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

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来添加文本:

par(mfrow = c(1,2)) # 并排绘图
# 左侧图
barplot(df$Grade_Semester1, names.arg = df$Person, horiz = TRUE, col = "grey", xlim = c(0, 10))

# 右侧图
barplot(ifelse(df$Difference < 0, df$Grade_Semester1, 0), 
        names.arg = df$Person, horiz = TRUE, col = "red", xlim = c(0, 10))
par(new = TRUE)
barplot(ifelse(df$Difference > 0, df$Grade_Semester2, 0), 
        names.arg = df$Person, horiz = TRUE, col = "green", xlim = c(0, 10))
par(new = TRUE)
xx <- barplot(pmin(df$Grade_Semester1, df$Grade_Semester2), 
        names.arg = df$Person, horiz = TRUE, col = "grey", xlim = c(0, 10))

text(pmin(df$Grade_Semester1, df$Grade_Semester2) + abs(pmin(df$Difference)/2),
     xx, ifelse(df$Difference == 0, "", ifelse(df$Difference > 0, paste0("+", df$Difference), df$Difference)))

数据:

df <- data.frame(Person = c("A", "B", "C"),
                 Grade_Semester1 = c(9, 10, 8),
                 Grade_Semester2 = c(7, 10, 10),
                 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:

par(mfrow = c(1,2)) #side by side plot
# left plot
barplot(df$Grade_Semester1, names.arg = df$Person, horiz = TRUE, col = &quot;grey&quot;, xlim = c(0, 10))

# right plot
barplot(ifelse(df$Difference &lt; 0, df$Grade_Semester1, 0), 
        names.arg = df$Person, horiz = TRUE, col = &quot;red&quot;, xlim = c(0, 10))
par(new = TRUE)
barplot(ifelse(df$Difference &gt; 0, df$Grade_Semester2, 0), 
        names.arg = df$Person, horiz = TRUE, col = &quot;green&quot;, xlim = c(0, 10))
par(new = TRUE)
xx &lt;- barplot(pmin(df$Grade_Semester1, df$Grade_Semester2), 
        names.arg = df$Person, horiz = TRUE, col = &quot;grey&quot;, xlim = c(0, 10))

text(pmin(df$Grade_Semester1, df$Grade_Semester2) + abs(pmin(df$Difference)/2),
     xx, ifelse(df$Difference == 0, &quot;&quot;, ifelse(df$Difference &gt; 0, paste0(&quot;+&quot;, df$Difference), df$Difference)))

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

Data

df &lt;- data.frame(Person = c(&quot;A&quot;, &quot;B&quot;, &quot;C&quot;),
                 Grade_Semester1 = c(9, 10, 8),
                 Grade_Semester2 = c(7, 10, 10),
                 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:

确定