英文:
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 <- 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)
The plot has 30 bins but has 3 labels on x - axis:
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_bar和scale_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)
  )

英文:
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 <- 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)
  )
<!-- -->
答案2
得分: 0
你可以使用 scale_x_continuous() 与 breaks 来调整 x 轴的刻度。
英文:
One way could be using scale_x_continuous() with the breaks
library(dplyr)
library(ggplot2)
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 = -0.5
  ) +
  scale_x_continuous(breaks = seq(min(data$var1), max(data$var1), by = 1))
答案3
得分: 0
你可以使用 scale_x_continuous 来修改 x 轴刻度的外观。你可以通过 n.breaks 来改变刻度的数量,或者使用 breaks 或 labels 进行调整。文档提供了详细的信息,而这本书是了解刻度的很好资源(以及整个 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 %>%
  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))
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 %>%
  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))
答案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]))
英文:
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]))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。




评论