英文:
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'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="Relative frequency (%)",
col = c("coral2"), border=NA, horiz = TRUE, las = 1, beside = TRUE,
xlim = c(0,29))
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 = '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())
答案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")
创建于 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 <- 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")
<sup>Created on 2023-03-09 with reprex v2.0.2</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论