从首选数字开始的Y轴

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

starting Y axis from preferred number

问题

我有一个类似这样的数据框:

DATA1 <- data.frame(
    Percent.houses = c(44, 44.5, 44.7, 45.8, 45.9, 45, 45.4, 45.6, 45.3),
    BUILDING.TYPE = c("wood bungalow", "brick bungalow", "glass bungalow", "wood flat", "brick flat", "glass flat", "wood house", "brick house", "glass house"),
    STRUCTURAL.MAKEUP = c("wood", "brick", "glass", "wood", "brick", "glass", "wood", "brick", "glass"))

我想要我的y轴从43开始,到53结束(每隔1刻度)。

我已经阅读了以前的问题和解决方案,并尝试了以下内容:

a <- ggbarplot(
    DATA1, 
    x = "BUILDING.TYPE" , 
    y = "Percent.houses", 
    fill = "STRUCTURAL.MAKEUP", 
    color = "black", 
    width = 0.5, 
    sort.by.groups = TRUE, 
    x.text.angle = 45, 
    label = TRUE, 
    label.pos = "in"
) + scale_y_continuous(limits = c(43, 53), breaks = seq(43, 53, by = 1))

a + scale_fill_manual(
    "", 
    breaks = c("wood", "brick", "glass"), 
    values = c("#009E73", "#D55E00", "#CC79A7"), 
    labels = c("wood", "brick", "glass")
) + theme(text = element_text(size = 6))

然而,这似乎不会按要求更改y轴。任何帮助将不胜感激。抱歉,我还是很新手。

英文:

I have a data frame like this

DATA1 &lt;- data.frame(
    Percent.houses = c(44, 44.5, 44.7, 45.8, 45.9, 45, 45.4, 45.6, 45.3),
    BUILDING.TYPE = c(&quot;wood bungalow&quot;, &quot;brick bungalow&quot;, &quot;glass bungalow&quot;, &quot;wood flat&quot;, &quot;brick flat&quot;, &quot;glass flat&quot;, &quot;wood house&quot;, &quot;brick house&quot;, &quot;glass house&quot;),
    STRUCTURAL.MAKEUP = c(&quot;wood&quot;, &quot;brick&quot;, &quot;glass&quot;, &quot;wood&quot;, &quot;brick&quot;, &quot;glass&quot;, &quot;wood&quot;, &quot;brick&quot;, &quot;glass&quot;))

I want my y axis to start from 43 and end at 53 (with ticks every 1)

I have read through past questions and solutions, and tried this

a &lt;- ggbarplot(
    DATA1, 
    x = &quot;BUILDING.TYPE&quot; , 
    y = &quot;Percent.houses&quot;, 
    fill = &quot;STRUCTURAL.MAKEUP&quot;, 
    color = &quot;black&quot;, 
    width = 0.5, 
    sort.by.groups = TRUE, 
    x.text.angle = 45, label = TRUE, label.pos = &quot;in&quot;, lab.vjust = -0.2 + scale_y_continuous(limits = c(43, 53), breaks = seq(43, 53, by = 1))
) +
    scale_fill_manual(
        &quot;&quot;, 
        breaks = c(&quot;wood&quot;, &quot;brick&quot;, &quot;glass&quot;), 
        values = c(&quot;#009E73&quot;, &quot;#D55E00&quot;, &quot;#CC79A7&quot;), 
        labels = c(&quot;wood&quot;, &quot;brick&quot;, &quot;glass&quot;)
    )
a + font(&quot;x.text&quot;, size = 6)

However, this does not seem to change the y axis as required. Any help will be appreciated. Apologies, I am still very new to R.

答案1

得分: 2

如@zephryl在上面提到的,您需要将scale_y_continuous的调用移出ggbarplot函数调用之外。但您还需要告诉ggplot不要省略在您指定的范围之外的值。由于条形图从零开始,将范围限制在不包括零的范围内将导致这些值被排除在外。您可以通过指定不同的“超出范围”的值函数来覆盖此行为,具体为scales::squish。就像这样:

a <- ggbarplot(
  DATA1, 
  x = "BUILDING.TYPE" , 
  y = "Percent.houses", 
  fill = "STRUCTURAL.MAKEUP", 
  color = "black", 
  width = 0.5, 
  sort.by.groups = TRUE, 
  x.text.angle = 45, 
  label = TRUE, 
  label.pos = "in", 
  lab.vjust = -0.2 
) +
scale_y_continuous(oob = scales::squish, 
                   limits = c(43, 53), 
                   breaks = seq(43, 53, by = 1)) +
scale_fill_manual(
  "", 
  breaks = c("wood", "brick", "glass"), 
  values = c("#009E73", "#D55E00", "#CC79A7"), 
  labels = c("wood", "brick", "glass")
)
a + font("x.text", size = 6)

请注意,我已经将代码中的HTML转义字符(如&lt;&quot;)还原为正常字符。

英文:

As @zephryl mentioned above you need to move the scale_y_continuous call outside the ggbarplot function call. But you also need to tell ggplot not to omit values outside the range you specify. Since the bars start at zero, limiting the range to something that does not include zero will cause those values to be excluded. You can over-ride this behavior by specifying a different function of "out of bounds" values, specifically scales::squish. Like this:

a &lt;- ggbarplot(
  DATA1, 
  x = &quot;BUILDING.TYPE&quot; , 
  y = &quot;Percent.houses&quot;, 
  fill = &quot;STRUCTURAL.MAKEUP&quot;, 
  color = &quot;black&quot;, 
  width = 0.5, 
  sort.by.groups = TRUE, 
  x.text.angle = 45, 
  label = TRUE, 
  label.pos = &quot;in&quot;, 
  lab.vjust = -0.2 
) +
scale_y_continuous(oob = scales::squish, 
                   limits = c(43, 53), 
                   breaks = seq(43, 53, by = 1)) +
scale_fill_manual(
  &quot;&quot;, 
  breaks = c(&quot;wood&quot;, &quot;brick&quot;, &quot;glass&quot;), 
  values = c(&quot;#009E73&quot;, &quot;#D55E00&quot;, &quot;#CC79A7&quot;), 
  labels = c(&quot;wood&quot;, &quot;brick&quot;, &quot;glass&quot;)
)
a + font(&quot;x.text&quot;, size = 6)

答案2

得分: 0

@erikor 是对的,仅仅在 scale_y_continuous() 内设置限制会导致所有的值都被排除。一种解决方法是使用他们演示的 oob 参数。另一种方法是在 coord_cartesian() 中指定 ylim;与 scale_y_continuous() 不同,这会在不移除数据的情况下“缩放”到指定的限制。

library(ggpubr)

a <- ggbarplot(
    DATA1, 
    x = "BUILDING.TYPE" , 
    y = "Percent.houses", 
    fill = "STRUCTURAL.MAKEUP", 
    color = "black", 
    width = 0.5, 
    sort.by.groups = TRUE, 
    x.text.angle = 45, label = TRUE, label.pos = "in", lab.vjust = -0.2
) +
    scale_fill_manual(
        "", 
        breaks = c("wood", "brick", "glass"), 
        values = c("#009E73", "#D55E00", "#CC79A7"), 
        labels = c("wood", "brick", "glass")
    ) + 
    scale_y_continuous(breaks = 43:53) +
    coord_cartesian(ylim = c(43, 53))

a + font("x.text", size = 6)

从首选数字开始的Y轴


[1]: https://i.stack.imgur.com/f0Z2y.jpg

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

@erikor is right that simply setting limits within `scale_y_continuous()` will cause all your values to be excluded. One solution is to use the `oob` argument as they demonstrated. Another is to specify `ylim` in `coord_cartesian()`; unlike `scale_y_continuous()`, this has the effect of &quot;zooming in&quot; to the specified limits without removing data. 
``` r
library(ggpubr)

a &lt;- ggbarplot(
    DATA1, 
    x = &quot;BUILDING.TYPE&quot; , 
    y = &quot;Percent.houses&quot;, 
    fill = &quot;STRUCTURAL.MAKEUP&quot;, 
    color = &quot;black&quot;, 
    width = 0.5, 
    sort.by.groups = TRUE, 
    x.text.angle = 45, label = TRUE, label.pos = &quot;in&quot;, lab.vjust = -0.2
) +
    scale_fill_manual(
        &quot;&quot;, 
        breaks = c(&quot;wood&quot;, &quot;brick&quot;, &quot;glass&quot;), 
        values = c(&quot;#009E73&quot;, &quot;#D55E00&quot;, &quot;#CC79A7&quot;), 
        labels = c(&quot;wood&quot;, &quot;brick&quot;, &quot;glass&quot;)
    ) + 
    scale_y_continuous(breaks = 43:53) +
    coord_cartesian(ylim = c(43, 53))

a + font(&quot;x.text&quot;, size = 6)

从首选数字开始的Y轴

huangapple
  • 本文由 发表于 2023年1月9日 10:32:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75052705.html
匿名

发表评论

匿名网友

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

确定