在R中,在水平条形图中在条旁边添加轴标签。

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

Adding axis labels next to the bars within a horizontal barplot in R

问题

使用下面的代码。首先,我尝试让y轴上的每个替代方案水平显示。但是,输出被截断了(见图片),似乎y轴旁边的空间只能容纳那么多空间,我尝试保存它或放大它,但仍然不起作用。所以我想我可以将标签放在绘图中的柱子旁边。ChatGPT建议使用text函数(见下面的代码),它有点起作用,但文本与柱子不对齐,我正在努力改变它。

rel_freq <- function(x) {
  (x/sum(x))*100
}

alternatives <- c("District heating"= 13,"Heat pump"= 10,"Solar thermal collector"= 6, "Pellet stove"= 6,
                  "Wood stove"= 4, "Geothermal" = 3, "Thermal energy storage" = 2, "Sustainable gas boiler" = 2,
                  "Biogas boiler" = 1, "Electric boiler" = 1, "Wood fireplace" = 1)

barplot(rel_freq(alternatives), xlab="Relative frequency (%)",
        col = c("coral2"), border=NA, horiz = TRUE, las = 1, beside = TRUE, xlim = c(0,29))
text(x = rel_freq(alternatives), y = 1:length(alternatives), 
     labels = names(alternatives), pos = 4, cex = 0.7)

box(col="black")


<details>
<summary>英文:</summary>

With the code below. First I tried to have every single alternative displayed by the y axis horizontally. However, the output is chopped (see picture), it seems like the space next to the y axis can only contain so much space, I tried saving it or zooming it but it still didn&#39;t work. So I thought I can put the labels next to the bar in the plot. ChatGPT suggested the text function (see code below), it kind of worked but the text does not align with the bars and I am struggling to change that.




rel_freq <- function(x) {
(x/sum(x))*100
}

alternatives <- c("District heating"= 13,"Heat pump"= 10,"Solar thermal collector"= 6, "Pellet stove"= 6,
"Wood stove"= 4, "Geothermal" = 3, "Thermal energy storage" = 2, "Sustainable gas boiler" = 2,
"Biogas boiler" = 1, "Electric boiler" = 1, "Wood fireplace" = 1)

barplot(rel_freq(alternatives), xlab="Relative frequency (%)",
col = c("coral2"), border=NA, horiz = TRUE, las = 1, beside = TRUE, xlim = c(0,29))
text(x = rel_freq(alternatives), y = 1:length(alternatives),
labels = names(alternatives), pos = 4, cex = 0.7)

box(col="black")


</details>


# 答案1
**得分**: 1

你需要增加页面左侧的边距。要做到这一点,使用`par`函数设置图形参数`mar`:

```r
par(mar = c(5, 12, 2, 2))

barplot(rel_freq(alternatives), xlab="Relative frequency (%)",
        col = c("coral2"), border=NA, horiz = TRUE, las = 1, beside = TRUE, 
        xlim = c(0,29))

或者,你可以使用ggplot,它会自动适应并为你指定边距:

library(ggplot2)

ggplot(data.frame(name = names(alternatives), values = rel_freq(alternatives)),
       aes(x = values, y = factor(name, name))) +
  geom_col(fill = 'coral2', width = 0.7) +
  labs(y = NULL, x = 'Relative frequency (%)') +
  scale_x_continuous(expand = c(0, 0)) +
  theme_minimal(base_size = 16) +
  theme(panel.grid = element_blank(),
        axis.line.x = element_line(),
        axis.ticks.x = element_line())
英文:

You need to increase the margin on the left of the page. To do this set the graphic parameter mar using par:

par(mar = c(5, 12, 2, 2))

barplot(rel_freq(alternatives), xlab=&quot;Relative frequency (%)&quot;,
        col = c(&quot;coral2&quot;), border=NA, horiz = TRUE, las = 1, beside = TRUE, 
        xlim = c(0,29))

在R中,在水平条形图中在条旁边添加轴标签。

Or, you could use ggplot, which will fit everything in for you without you having to specify a margin:

library(ggplot2)

ggplot(data.frame(name = names(alternatives), values = rel_freq(alternatives)),
       aes(x = values, y = factor(name, name))) +
  geom_col(fill = &#39;coral2&#39;, width = 0.7) +
  labs(y = NULL, x = &#39;Relative frequency (%)&#39;) +
  scale_x_continuous(expand = c(0, 0)) +
  theme_minimal(base_size = 16) +
  theme(panel.grid = element_blank(),
        axis.line.x = element_line(),
        axis.ticks.x = element_line())

在R中,在水平条形图中在条旁边添加轴标签。

答案2

得分: 1

以下解决方案包含两种不同的方法。首先,左边距使用 par(mar, .....) 扩展。

为了将标签直接放在条形旁边,我们需要兼容的 y 坐标,因为 barplot 使用每个条形的比例不同的刻度。幸运的是,这些值作为一个向量返回,我们可以将其赋给一个变量,例如 bp,然后在 text 函数中使用。

rel_freq <- function(x) {
  (x/sum(x))*100
}

par(mar=c(4,10,1,1)+.1)

alternatives <- c("District heating" = 13,"Heat pump" = 10,
  "Solar thermal collector" = 6, "Pellet stove" = 6,
  "Wood stove" = 4, "Geothermal" = 3, "Thermal energy storage" = 2,
  "Sustainable gas boiler" = 2, "Biogas boiler" = 1,
  "Electric boiler" = 1, "Wood fireplace" = 1)

bp <- barplot(rel_freq(alternatives), xlab = "Relative frequency (%)",
        col = c("coral2"), border=NA, horiz = TRUE, las = 1,
        beside = TRUE, xlim = c(0, 35))

text(x = rel_freq(alternatives), y = bp, 
     labels = names(alternatives), pos = 4, cex = 0.7)

box(col = "black")

在R中,在水平条形图中在条旁边添加轴标签。

创建于 2023-03-09,使用 reprex v2.0.2

英文:

The following solution contains two different approaches. First, the left margin ins extended with par(mar, .....).

To place labels directly besides of the bars, we need compatible y-coordinates, as barplot uses a scale different from 1 per bar. Fortunately, these values are returned as a vector, that we can assign to a variable, e.g. bp and use in text.

rel_freq &lt;- function(x) {
  (x/sum(x))*100
}

par(mar=c(4,10,1,1)+.1)

alternatives &lt;- c(&quot;District heating&quot; = 13,&quot;Heat pump&quot; = 10,
  &quot;Solar thermal collector&quot; = 6, &quot;Pellet stove&quot; = 6,
  &quot;Wood stove&quot; = 4, &quot;Geothermal&quot; = 3, &quot;Thermal energy storage&quot; = 2,
  &quot;Sustainable gas boiler&quot; = 2, &quot;Biogas boiler&quot; = 1,
  &quot;Electric boiler&quot; = 1, &quot;Wood fireplace&quot; = 1)

bp &lt;- barplot(rel_freq(alternatives), xlab = &quot;Relative frequency (%)&quot;,
        col = c(&quot;coral2&quot;), border=NA, horiz = TRUE, las = 1,
        beside = TRUE, xlim = c(0, 35))

text(x = rel_freq(alternatives), y = bp, 
     labels = names(alternatives), pos = 4, cex = 0.7)

box(col = &quot;black&quot;)

在R中,在水平条形图中在条旁边添加轴标签。

<sup>Created on 2023-03-09 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年3月9日 21:42:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75685413.html
匿名

发表评论

匿名网友

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

确定