显示 ggplot 直方图上的所有 x 轴标签。

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

Display all x-axis labels on ggplot histogram

问题

我只会翻译你提供的代码部分,不会回答关于翻译的问题。以下是代码的翻译:

data <- data.frame(var1=floor(6*rnorm(65000) + 1))
data %>%
  ggplot(aes(x=var1)) +
    geom_histogram(fill="lightblue", color="black", alpha=0.9, bins = 30) +
    theme(plot.title = element_text(size=15),
          axis.text.x = element_text(angle = 90, vjust = 0.5)) +
    stat_bin(geom="text", 
           colour="black",
           bins = 30,
           aes(label=..count..), 
           angle = 45, 
           vjust=-.5)

希望这有所帮助。

英文:

I have a simple frequency histogram for a data with 65k rows and here is the code I have with an example data:

data &lt;- data.frame(var1=floor(6*rnorm(65000)  + 1))
data %&gt;%
  ggplot(aes(x=var1)) +
    geom_histogram(fill=&quot;lightblue&quot;, color=&quot;black&quot;, alpha=0.9, bins = 30) +
    theme(plot.title = element_text(size=15),
          axis.text.x = element_text(angle = 90, vjust = 0.5)) +
    stat_bin(geom=&quot;text&quot;, 
           colour=&quot;black&quot;,
           bins = 30,
           aes(label=..count..), 
           angle = 45, 
           vjust=-.5)

The plot has 30 bins but has 3 labels on x - axis:

显示 ggplot 直方图上的所有 x 轴标签。

I just want it to have a label for each bin.

I tried theme() and scale_x_cont() but they did not work and messed the whole plot. I don't want to change anything i just want to add labels on the x. Thank you!

答案1

得分: 1

使用geom_barscale_x_binned的不同方法来绘制直方图,并同时允许为每个区间添加坐标轴标签的方式如下:

library(ggplot2)

set.seed(123)

data <- data.frame(var1 = floor(6 * rnorm(200) + 1))

ggplot(data, aes(x = var1)) +
  geom_bar(fill = "lightblue", color = "black", alpha = 0.9) +
  stat_count(
    geom = "text",
    colour = "black",
    aes(label = after_stat(count)),
    angle = 45,
    vjust = 0,
    hjust = 0
  ) +
  scale_x_binned(n.breaks = 30, show.limits = TRUE) +
  theme(
    plot.title = element_text(size = 15),
    axis.text.x = element_text(angle = 90, vjust = .5)
  )

显示 ggplot 直方图上的所有 x 轴标签。

英文:

A different approach to draw your histogram and at the same time allowing to add axis labels for each bin would be to use geom_bar with scale_x_binned:

library(ggplot2)

set.seed(123)

data &lt;- data.frame(var1 = floor(6 * rnorm(200) + 1))

ggplot(data, aes(x = var1)) +
  geom_bar(fill = &quot;lightblue&quot;, color = &quot;black&quot;, alpha = 0.9) +
  stat_count(
    geom = &quot;text&quot;,
    colour = &quot;black&quot;,
    aes(label = after_stat(count)),
    angle = 45,
    vjust = 0,
    hjust = 0
  ) +
  scale_x_binned(n.breaks = 30, show.limits = TRUE) +
  theme(
    plot.title = element_text(size = 15),
    axis.text.x = element_text(angle = 90, vjust = .5)
  )

显示 ggplot 直方图上的所有 x 轴标签。<!-- -->

答案2

得分: 0

你可以使用 scale_x_continuous()breaks 来调整 x 轴的刻度。

英文:

One way could be using scale_x_continuous() with the breaks

library(dplyr)
library(ggplot2)

data &lt;- data.frame(var1 = floor(6 * rnorm(200) + 1))

data %&gt;%
  ggplot(aes(x = var1)) +
  geom_histogram(fill = &quot;lightblue&quot;, color = &quot;black&quot;, alpha = 0.9, bins = 30) +
  theme(plot.title = element_text(size = 15),
        axis.text.x = element_text(angle = 90, vjust = 0.5)) +
  stat_bin(
    geom = &quot;text&quot;,
    colour = &quot;black&quot;,
    bins = 30,
    aes(label = ..count..),
    angle = 45,
    vjust = -0.5
  ) +
  scale_x_continuous(breaks = seq(min(data$var1), max(data$var1), by = 1))

显示 ggplot 直方图上的所有 x 轴标签。

答案3

得分: 0

你可以使用 scale_x_continuous 来修改 x 轴刻度的外观。你可以通过 n.breaks 来改变刻度的数量,或者使用 breakslabels 进行调整。文档提供了详细的信息,而这本是了解刻度的很好资源(以及整个 ggplot2)。

对于你的代码,答案如下:

data %>%
  ggplot(aes(x=var1)) +
    geom_histogram(fill="lightblue", color="black", alpha=0.9, bins = 30) +
    theme(plot.title = element_text(size=15),
          axis.text.x = element_text(angle = 90, vjust = 0.5)) +
    stat_bin(geom="text", 
           colour="black",
           bins = 30,
           aes(label=..count..), 
           angle = 45, 
           vjust=-.5) +
  scale_x_continuous(breaks = seq(-17, 17, 1))

另外,我要补充一点,..count.. 已经在 ggplot2 3.4.0 中被弃用。这里有关于弃用和新方法的更多信息。为了避免警告,可以尝试以下代码:

data %>%
  ggplot(aes(x=var1)) +
    geom_histogram(fill="lightblue", color="black", alpha=0.9, bins = 30) +
    theme(plot.title = element_text(size=15),
          axis.text.x = element_text(angle = 90, vjust = 0.5)) +
    stat_bin(geom="text", 
           colour="black",
           bins = 30,
           aes(label = after_stat(count)), 
           angle = 45, 
           vjust=-.5) +
  scale_x_continuous(breaks = seq(-17, 17, 1))
英文:

You can use scale_x_continuous to modify how the x axis scale looks. You can change the number of breaks using n.breaks or adjust using breaks or labels. The documentation gives great detail, and this book is an awesome resource for understanding scales a bit better (and ggplot2 as a whole).

For your code, the answer looks like:

data %&gt;%
  ggplot(aes(x=var1)) +
    geom_histogram(fill=&quot;lightblue&quot;, color=&quot;black&quot;, alpha=0.9, bins = 30) +
    theme(plot.title = element_text(size=15),
          axis.text.x = element_text(angle = 90, vjust = 0.5)) +
    stat_bin(geom=&quot;text&quot;, 
           colour=&quot;black&quot;,
           bins = 30,
           aes(label=..count..), 
           angle = 45, 
           vjust=-.5) +
  scale_x_continuous(breaks = seq(-17, 17, 1))

I would also add that ..count.. has been deprecated by ggplot2 3.4.0. Here is a little more info about the deprecation and the new approach. To avoid getting a warning, try:

data %&gt;%
  ggplot(aes(x=var1)) +
    geom_histogram(fill=&quot;lightblue&quot;, color=&quot;black&quot;, alpha=0.9, bins = 30) +
    theme(plot.title = element_text(size=15),
          axis.text.x = element_text(angle = 90, vjust = 0.5)) +
    stat_bin(geom=&quot;text&quot;, 
           colour=&quot;black&quot;,
           bins = 30,
           aes(label = after_stat(count)), 
           angle = 45, 
           vjust=-.5) +
  scale_x_continuous(breaks = seq(-17, 17, 1))

答案4

得分: 0

From ?scale_x_continuous, the breaks argument can be one of

> NULL for no breaks
>
> waiver() for the default breaks computed by the transformation object
>
> A numeric vector of positions
>
> A function that takes the limits as input and returns breaks as output (e.g., a function returned by scales::extended_breaks()). Also accepts rlang lambda function notation.

Using the last option, we can make a simple function giving every integer in the axis:

data <- data.frame(var1=floor(6*rnorm(200) + 1))
data %>%
  ggplot(aes(x=var1)) +
    geom_histogram(fill="lightblue", color="black", alpha=0.9, bins = 30) +
    theme(plot.title = element_text(size=15),
          axis.text.x = element_text(angle = 90, vjust = 0.5)) +
    stat_bin(geom="text", 
           colour="black",
           bins = 30,
           aes(label=..count..), 
           angle = 45, 
           vjust=-.5) +
  scale_x_continuous(breaks = function(range) floor(range[1]):ceiling(range[2]))

显示 ggplot 直方图上的所有 x 轴标签。

英文:

From ?scale_x_continuous, the breaks argument can be one of

> NULL for no breaks
>
> waiver() for the default breaks computed by the transformation object
>
> A numeric vector of positions
>
> A function that takes the limits as input and returns breaks as output (e.g., a function returned by scales::extended_breaks()). Also accepts rlang lambda function notation.

Using the last option, we can make a simple function giving every integer in the axis:

data &lt;- data.frame(var1=floor(6*rnorm(200)  + 1))
data %&gt;%
  ggplot(aes(x=var1)) +
    geom_histogram(fill=&quot;lightblue&quot;, color=&quot;black&quot;, alpha=0.9, bins = 30) +
    theme(plot.title = element_text(size=15),
          axis.text.x = element_text(angle = 90, vjust = 0.5)) +
    stat_bin(geom=&quot;text&quot;, 
           colour=&quot;black&quot;,
           bins = 30,
           aes(label=..count..), 
           angle = 45, 
           vjust=-.5) +
  scale_x_continuous(breaks = function(range) floor(range[1]):ceiling(range[2]))

显示 ggplot 直方图上的所有 x 轴标签。

huangapple
  • 本文由 发表于 2023年5月18日 03:01:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76275412.html
匿名

发表评论

匿名网友

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

确定