附加的右轴用于分组。

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

Additional right axis for groups

问题

I'm trying to make a right-side axis title (but only a title) for grouped (facet_grid()) variables.

我试图为分组(facet_grid())变量创建一个右侧轴标题(但仅为标题)。

I have tried the scale_y_continuous(sec.axis = (name= "title") command, it didn't work without a variable which I don't have. I have also tried axis.title.y.right = element_text("Sex of groups") element in the theme function. It didn't work either.

我尝试使用 scale_y_continuous(sec.axis = (name= "title") 命令,但没有可用的变量,因此它无法正常工作。我还尝试在主题函数中使用 axis.title.y.right = element_text("Sex of groups") 元素,但也无效。

What I have as code is this:

我所拥有的代码如下:

temp.data = data.frame (Species = rep(c("A","B"),each=2, times=2),
Status = rep(c("An","Bac"), times=4),
Sex = rep(c("Male","Female"), each=4, times=1),
Proportion = c(6.86, 7.65, 30.13, 35.71, 7.13, 10.33, 29.24, 31.09))

ggplot(temp.data, aes(x = Species, y = Proportion, fill = Status)) +
geom_bar(stat='identity', position = position_dodge(width = 0.73), width=.67) +
facet_grid(Sex ~ .) + xlab("X title") +
ylab("Y title") +
scale_fill_manual(name = "Status", labels = c("A","B"),
values = c("#0d4e04","#a36424")) +

scale_y_continuous(sec.axis = (name= "Sex of groups")) +

theme_light() + theme(legend.title= element_text(size = 8),
legend.title.align=0.5,
legend.position = c(),
legend.background=element_blank(),
legend.text=element_text(size=7),
legend.key.size = unit(0.6, 'cm'),
axis.title.x = element_text(size = 9),
axis.title.y = element_text(size = 9),
axis.title.y.right = element_text("Sex of groups"),
strip.background = element_rect(
color="lightgrey", fill="white", size=0.5, linetype="solid"),
strip.text.y = element_text(color = "darkgrey", face = "bold"))

This is the graph I have produced:

这是我生成的图表:

附加的右轴用于分组。

So basically I'm looking for a way to put a title on the right Y axis, just near the facetted groups. It'd be great to know, if you know a solution for it.

所以基本上我正在寻找一种在右侧Y轴上放置标题的方法,就在分组的旁边。如果您知道解决方法,那将非常好。

英文:

I'm trying to make a right-side axis title (but only a title) for grouped (facet_grid()) variables.

I have tried the scale_y_continuous(sec.axis = (name= "title") command, it didn't work without a variable which I don't have. I have also tried axis.title.y.right = element_text("Sex of groups") element in the theme function. It didn't work either.

What I have as code is this:

  1. temp.data = data.frame (Species = rep(c("A","B"),each=2, times=2),
  2. Status = rep(c("An","Bac"), times=4),
  3. Sex = rep(c("Male","Female"), each=4, times=1),
  4. Proportion = c(6.86, 7.65, 30.13, 35.71, 7.13, 10.33, 29.24, 31.09))
  5. ggplot(temp.data, aes(x = Species, y = Proportion, fill = Status)) +
  6. geom_bar(stat='identity', position = position_dodge(width = 0.73), width=.67) +
  7. facet_grid(Sex ~ .) + xlab("X title") +
  8. ylab("Y title") +
  9. scale_fill_manual(name = "Status", labels = c("A","B"),
  10. values = c("#0d4e04","#a36424")) +
  11. # scale_y_continuous(sec.axis = (name= "Sex of groups")) +
  12. theme_light() + theme(legend.title= element_text(size = 8),
  13. legend.title.align=0.5,
  14. legend.position = c(),
  15. legend.background=element_blank(),
  16. legend.text=element_text(size=7),
  17. legend.key.size = unit(0.6, 'cm'),
  18. axis.title.x = element_text(size = 9),
  19. axis.title.y = element_text(size = 9),
  20. axis.title.y.right = element_text("Sex of groups"),
  21. strip.background = element_rect(
  22. color="lightgrey", fill="white", size=0.5, linetype="solid"),
  23. strip.text.y = element_text(color = "darkgrey", face = "bold"))

This is the graph I have produced:

附加的右轴用于分组。

So basically I'm looking for a way to put a title on the right Y axis, just near the facetted groups. It'd be great to know, if you know a solution for it.

答案1

得分: 1

Here is the translated code portion:

  1. 基本上,你走在了正确的道路上。你可以通过使用`scale_y_continuous(sec.axis = dup_axis(name="组的性别"))`来添加你的标题。除此之外,我们还需要一些额外的`theme`调整来去掉轴文本和刻度线,后者还需要将刻度长度设置为0
  2. ```R
  3. library(ggplot2)
  4. ggplot(temp.data, aes(x = Species, y = Proportion, fill = Status)) +
  5. geom_bar(stat = "identity", position = position_dodge(width = 0.73), width = .67) +
  6. facet_grid(Sex ~ .) +
  7. xlab("X标题") +
  8. ylab("Y标题") +
  9. scale_fill_manual(
  10. name = "状态", labels = c("A", "B"),
  11. values = c("#0d4e04", "#a36424")
  12. ) +
  13. scale_y_continuous(sec.axis = dup_axis(name="组的性别")) +
  14. theme_light() +
  15. theme(
  16. legend.title = element_text(size = 8),
  17. legend.title.align = 0.5,
  18. legend.position = c(),
  19. legend.background = element_blank(),
  20. legend.text = element_text(size = 7),
  21. legend.key.size = unit(0.6, "cm"),
  22. axis.title.x = element_text(size = 9),
  23. axis.title.y = element_text(size = 9),
  24. axis.title.y.right = element_text(margin = margin(l = 5.5)),
  25. axis.text.y.right = element_blank(),
  26. axis.ticks.y.right = element_blank(),
  27. axis.ticks.length.y.right = unit(0, "pt"),
  28. strip.background = element_rect(
  29. color = "lightgrey", fill = "white", size = 0.5, linetype = "solid"
  30. ),
  31. strip.text.y = element_text(color = "darkgrey", face = "bold")
  32. )

附加的右轴用于分组。

  1. [![这里输入图片描述][1]][1]
  1. <details>
  2. <summary>英文:</summary>
  3. Basically you were on the right track. You could add your title via a duplicated y scale using `scale_y_continuous(sec.axis = dup_axis(name= &quot;Sex of groups&quot;))`. Besides that we need some additional `theme` adjustments to get rid of the axis text and ticks where the latter also requires to set the tick length to 0:

library(ggplot2)

ggplot(temp.data, aes(x = Species, y = Proportion, fill = Status)) +
geom_bar(stat = "identity", position = position_dodge(width = 0.73), width = .67) +
facet_grid(Sex ~ .) +
xlab("X title") +
ylab("Y title") +
scale_fill_manual(
name = "Status", labels = c("A", "B"),
values = c("#0d4e04", "#a36424")
) +
scale_y_continuous(sec.axis = dup_axis(name= "Sex of groups")) +
theme_light() +
theme(
legend.title = element_text(size = 8),
legend.title.align = 0.5,
legend.position = c(),
legend.background = element_blank(),
legend.text = element_text(size = 7),
legend.key.size = unit(0.6, "cm"),
axis.title.x = element_text(size = 9),
axis.title.y = element_text(size = 9),
axis.title.y.right = element_text(margin = margin(l = 5.5)),
axis.text.y.right = element_blank(),
axis.ticks.y.right = element_blank(),
axis.ticks.length.y.right = unit(0, "pt"),
strip.background = element_rect(
color = "lightgrey", fill = "white", size = 0.5, linetype = "solid"
),
strip.text.y = element_text(color = "darkgrey", face = "bold")
)

  1. [![enter image description here][1]][1]
  2. [1]: https://i.stack.imgur.com/JHvcZ.png
  3. </details>
  4. # 答案2
  5. **得分**: 0
  6. 你的 sec_axis 函数没有正确指定:
  7. * 函数应为 sec_axis(),而不是 sec.axis()
  8. * 它需要一个转换系数(你可以为你的示例任意选择,我选择了1
  9. * 你可以指定任何你喜欢的名称
  10. * guide = "none" 可以省略右侧的任何刻度值/刻度线,如果你不想显示的话。
  11. 以下内容对我有效:
  12. ```R
  13. temp.data = data.frame (Species = rep(c("A","B"),each=2, times=2),
  14. Status = rep(c("An","Bac"), times=4),
  15. Sex = rep(c("Male","Female"), each=4, times=1),
  16. Proportion = c(6.86, 7.65, 30.13, 35.71, 7.13, 10.33, 29.24, 31.09))
  17. p<-ggplot(temp.data, aes(x = Species, y = Proportion, fill = Status)) +
  18. geom_bar(stat='identity', position = position_dodge(width = 0.73), width=.67) +
  19. facet_grid(Sex ~ .) + xlab("X title") +
  20. ylab("Y title") +
  21. scale_fill_manual(name = "Status", labels = c("A","B"),
  22. values = c("#0d4e04","#a36424")) +
  23. scale_y_continuous(sec.axis = sec_axis( trans=~.*1, name="Sex", guide = "none"))
英文:

Your sec_axis function is not specified correctly:

  • the function is sec_axis(), not sec.axis()
  • it needs a transformation coefficient (you can choose it arbitrarily for your example, I chose 1)
  • you can specify any name you like
  • guide = "none" omits any scale-values / ticks on the right side that you do not want to display.

The following works for me:

  1. temp.data = data.frame (Species = rep(c(&quot;A&quot;,&quot;B&quot;),each=2, times=2),
  2. Status = rep(c(&quot;An&quot;,&quot;Bac&quot;), times=4),
  3. Sex = rep(c(&quot;Male&quot;,&quot;Female&quot;), each=4, times=1),
  4. Proportion = c(6.86, 7.65, 30.13, 35.71, 7.13, 10.33, 29.24, 31.09))
  5. p&lt;-ggplot(temp.data, aes(x = Species, y = Proportion, fill = Status)) +
  6. geom_bar(stat=&#39;identity&#39;, position = position_dodge(width = 0.73), width=.67) +
  7. facet_grid(Sex ~ .) + xlab(&quot;X title&quot;) +
  8. ylab(&quot;Y title&quot;) +
  9. scale_fill_manual(name = &quot;Status&quot;, labels = c(&quot;A&quot;,&quot;B&quot;),
  10. values = c(&quot;#0d4e04&quot;,&quot;#a36424&quot;)) +
  11. scale_y_continuous(sec.axis = sec_axis( trans=~.*1, name=&quot;Sex&quot;, guide = &quot;none&quot;))

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

发表评论

匿名网友

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

确定