英文:
Adding dots and a line with second axis to a barplot
问题
以下是代码的部分翻译:
我正在查看12个子区域的滞留数据。我有一个条形图,显示每个子区域的滞留总数,我想在图中添加点和一条线,使用每个75公里长子区域的滞留率(kmrate)。
> dfo_sba2_noNA
Subarea Quantity kmrate
<fct> <int> <dbl>
1 1 96 1.28
2 2 431 5.75
3 3 118 1.57
4 4 469 6.25
5 5 1 0.0133
6 6 20 0.267
7 7 313 4.17
8 8 252 3.36
9 9 14 0.187
10 10 118 1.57
11 11 7 0.0933
12 12 21 0.28
以下是代码的部分翻译:
使用以下代码,我得到以下图表,但点和线不正确。
[![在此输入图像描述][1]][1]
```R
p_sba2 <- ggplot(dfo_sba2_noNA, aes(x=Subarea, y=Quantity)) +
geom_bar(stat = "identity", position="dodge") +
labs(x = "Subarea", y = "Number of observations") +
geom_point(aes(kmrate, color = "red", group = Subarea)) +
geom_line(aes(x=as.numeric(Subarea), y=kmrate),stat="identity",color="red")+
scale_y_continuous(breaks = seq(0, 1600, 200),sec.axis=sec_axis(~./75),
name="Stranding rate (per km)")+
theme(
panel.grid.major.y = element_line(colour = "white"),
legend.background = element_rect(),
legend.text = element_text(size=10),
legend.title = element_text(size=10),
panel.border = element_rect(colour="black", fill="NA"),
panel.grid.minor = element_blank(),
panel.background = element_rect(fill = "white"),
axis.title = element_text(size=10),
axis.text = element_text(size=10)
)
p_sba2
如何添加一个更好刻度的第二个轴?如何在条形图上方添加点和线?我希望我的图表看起来像这样,每个条形的滞留率:
我知道我的问题与该平台上的其他问题类似,但似乎没有一个解决方案适用于我。
<details>
<summary>英文:</summary>
I am looking at stranding data in 12 subareas. I have a barplot with the total number of strandings per subarea and I would like to add dots and a line to the plot unisg the stranding rate per kilometre (kmrate) in each 75 km long subarea.
> dfo_sba2_noNA
Subarea Quantity kmrate
<fct> <int> <dbl>
1 1 96 1.28
2 2 431 5.75
3 3 118 1.57
4 4 469 6.25
5 5 1 0.0133
6 6 20 0.267
7 7 313 4.17
8 8 252 3.36
9 9 14 0.187
10 10 118 1.57
11 11 7 0.0933
12 12 21 0.28
With the following code I get the following graph, but the dots and line are not right.
[![enter image description here][1]][1]
p_sba2 <- ggplot(dfo_sba2_noNA, aes(x=Subarea, y=Quantity)) +
geom_bar(stat = "identity", position="dodge") +
labs(x = "Subarea", y = "Number of observations") +
geom_point(aes(kmrate, color = "red", group = Subarea)) +
geom_line(aes(x=as.numeric(Subarea), y=kmrate),stat="identity",color="red")+
scale_y_continuous(breaks = seq(0, 1600, 200),sec.axis=sec_axis(~./75),
name="Stranding rate (per km)")+
theme(
panel.grid.major.y = element_line(colour = "white"),
legend.background = element_rect(),
legend.text = element_text(size=10),
legend.title = element_text(size=10),
panel.border = element_rect(colour="black", fill="NA"),
panel.grid.minor = element_blank(),
panel.background = element_rect(fill = "white"),
axis.title = element_text(size=10),
axis.text = element_text(size=10)
)
p_sba2
How can I add a second axis with a better scale? And how do I add the dots and lines on top of the barplot?
I'd like my plot to look something like this, with a stranding rater per bar:
[![enter image description here][2]][2]
I know my question is similar to others on this platform, but none of the solutions seem to work for me.
[1]: https://i.stack.imgur.com/jpmxV.png
[2]: https://i.stack.imgur.com/TKv5n.png
</details>
# 答案1
**得分**: 0
在处理`ggplot2`中的次要轴时,经常出现的情况是您忽略了对数据的缩放。在下面的我的代码中,我使用`scales::rescale`来首先缩放您的`kmrate`变量,并在`sec_axis`中应用反向“转换”。
```R
library(ggplot2)
library(scales)
to <- c(0, max(dfo_sba2_noNA$Quantity) / 2) # 缩放到最大列高度的50%
from <- c(0, max(dfo_sba2_noNA$kmrate))
dfo_sba2_noNA$kmrate <- scales::rescale(dfo_sba2_noNA$kmrate, from = from, to = to)
ggplot(dfo_sba2_noNA, aes(x = Subarea, y = Quantity)) +
geom_col() +
geom_point(aes(y = kmrate), color = "red") +
geom_line(aes(x = Subarea, y = kmrate), color = "red") +
scale_y_continuous(
breaks = seq(0, 1600, 200),
sec.axis = sec_axis(~ scales::rescale(.x, to = from, from = to),
name = "每公里搁浅率"
)
) +
theme(
panel.grid.major.y = element_line(colour = "white"),
legend.background = element_rect(),
legend.text = element_text(size = 10),
legend.title = element_text(size = 10),
panel.border = element_rect(colour = "black", fill = "NA"),
panel.grid.minor = element_blank(),
panel.background = element_rect(fill = "white"),
axis.title = element_text(size = 10),
axis.text = element_text(size = 10)
) +
labs(x = "子区域", y = "观测次数")
数据
dfo_sba2_noNA <- structure(list(Subarea = 1:12, Quantity = c(
96L, 431L, 118L,
469L, 1L, 20L, 313L, 252L, 14L, 118L, 7L, 21L
), kmrate = c(
1.28,
5.75, 1.57, 6.25, 0.0133, 0.267, 4.17, 3.36, 0.187, 1.57, 0.0933,
0.28
)), class = "data.frame", row.names = c(
"1", "2", "3", "4",
"5", "6", "7", "8", "9", "10", "11", "12"
))
英文:
As is quite often the case when dealing with secondary axes in ggplot2
you missed to scale your data. In my code below I use scales::rescale
to first scale your kmrate
variable and applying the inverse "transformation" inside sec_axis
.
library(ggplot2)
library(scales)
to <- c(0, max(dfo_sba2_noNA$Quantity) / 2) # Scale to 50% of max column height
from <- c(0, max(dfo_sba2_noNA$kmrate))
dfo_sba2_noNA$kmrate <- scales::rescale(dfo_sba2_noNA$kmrate, from = from, to = to)
ggplot(dfo_sba2_noNA, aes(x = Subarea, y = Quantity)) +
geom_col() +
geom_point(aes(y = kmrate), color = "red") +
geom_line(aes(x = Subarea, y = kmrate), color = "red") +
scale_y_continuous(
breaks = seq(0, 1600, 200),
sec.axis = sec_axis(~ scales::rescale(.x, to = from, from = to),
name = "Stranding rate (per km)"
)
) +
theme(
panel.grid.major.y = element_line(colour = "white"),
legend.background = element_rect(),
legend.text = element_text(size = 10),
legend.title = element_text(size = 10),
panel.border = element_rect(colour = "black", fill = "NA"),
panel.grid.minor = element_blank(),
panel.background = element_rect(fill = "white"),
axis.title = element_text(size = 10),
axis.text = element_text(size = 10)
) +
labs(x = "Subarea", y = "Number of observations")
DATA
dfo_sba2_noNA <- structure(list(Subarea = 1:12, Quantity = c(
96L, 431L, 118L,
469L, 1L, 20L, 313L, 252L, 14L, 118L, 7L, 21L
), kmrate = c(
1.28,
5.75, 1.57, 6.25, 0.0133, 0.267, 4.17, 3.36, 0.187, 1.57, 0.0933,
0.28
)), class = "data.frame", row.names = c(
"1", "2", "3", "4",
"5", "6", "7", "8", "9", "10", "11", "12"
))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论