简体   繁体   English

R Ggplot2:将直方图 y 轴更改为小数位

[英]R Ggplot2: changing histogram y axis to decimal places

I am trying to change the y axis (Proportion) of the right histogram into decimal places like the left histogram using ggplot.我正在尝试使用 ggplot 将右直方图的 y 轴(比例)更改为小数位,如左直方图。 I tried using geom_histogram(aes(y=..ncount..) but the bars end up squashed. Any other suggestions?我尝试使用 geom_histogram(aes(y=..ncount..) 但条形图最终被压扁了。还有其他建议吗?

Help desperately needed!急需帮助!

Thanksss谢谢

代码

直方图

I will give you an example using the mtcars dataset.我会给你一个使用mtcars数据集的例子。 To change the y-axis decimals to what you want, you should use the scales package which has the function number_format .要将 y 轴小数更改为您想要的,您应该使用具有 function number_formatscales package。 If you run the following command number_format(accuracy = 0.1) in the labels of scale_y_continuous you will get the right decimals.如果您在scale_y_continuous的标签中运行以下命令number_format(accuracy = 0.1)您将获得正确的小数。 You can use the following code as an example:您可以使用以下代码作为示例:

library(tidyverse)
library(scales)
mtcars %>%
  ggplot(aes(x = mpg)) +
  geom_histogram(aes(y=..count../sum(..count..))) +
  ylab("Proportion") +
  scale_y_continuous(
    labels = scales::number_format(accuracy = 0.1))

Output: Output:

在此处输入图像描述

As you can see, the decimals for the y-axis are in the right format.如您所见,y 轴的小数位格式正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM