附加的右轴用于分组。

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

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:

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.

答案1

得分: 1

Here is the translated code portion:

基本上,你走在了正确的道路上。你可以通过使用`scale_y_continuous(sec.axis = dup_axis(name="组的性别"))`来添加你的标题。除此之外,我们还需要一些额外的`theme`调整来去掉轴文本和刻度线,后者还需要将刻度长度设置为0:

```R
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标题") +
  ylab("Y标题") +
  scale_fill_manual(
    name = "状态", labels = c("A", "B"),
    values = c("#0d4e04", "#a36424")
  ) +
  scale_y_continuous(sec.axis = dup_axis(name="组的性别")) +
  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]][1]

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

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")
)


[![enter image description here][1]][1]


  [1]: https://i.stack.imgur.com/JHvcZ.png

</details>



# 答案2
**得分**: 0

你的 sec_axis 函数没有正确指定:
* 函数应为 sec_axis(),而不是 sec.axis()
* 它需要一个转换系数(你可以为你的示例任意选择,我选择了1)
* 你可以指定任何你喜欢的名称
* guide = "none" 可以省略右侧的任何刻度值/刻度线,如果你不想显示的话。

以下内容对我有效:

```R
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))

p<-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 = 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:

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

p&lt;-ggplot(temp.data, aes(x = Species, y = Proportion, fill = Status)) +
  geom_bar(stat=&#39;identity&#39;, position = position_dodge(width = 0.73), width=.67) +
  facet_grid(Sex ~ .) + xlab(&quot;X title&quot;) + 
  ylab(&quot;Y title&quot;) + 
  scale_fill_manual(name = &quot;Status&quot;, labels = c(&quot;A&quot;,&quot;B&quot;),
                    values = c(&quot;#0d4e04&quot;,&quot;#a36424&quot;)) +
  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:

确定