I want to plot a histogram, but was wondering if it is possible to change the default y-axis frequency, to something else? If so, what is the correct syntax? Thanks in advance :)
Asked
Active
Viewed 466 times
1 Answers
0
In ggplot2, the default y aesthetic for histograms is count
, which is a computed variable documented in ?geom_histogram
. You can change what you put on the y-axis by changing the aesthetic. If you want access to any of the computed variables, you can assign after_stat(the_computed_variable)
to y
. Example below:
library(ggplot2)
ggplot(diamonds, aes(carat)) +
geom_histogram(aes(y = after_stat(density)))
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

teunbrand
- 33,645
- 4
- 37
- 63
-
Thanks a lot. If it's okay I have another question. I want to create a subset from data that I have in "character" format. I want to include everything except ID's that consist of two or less digits within the " ". I tried to convert my data into numeric and used the logical operators, but it didn't work as some of the ID's have letters in them too, so aren't really numerical. It worked when I used the functions below, but was wondering if there's a streamlined way to do it. mother_ids = mother_ids[mother_ids!="0" & mother_ids!="1" & mother_ids!="2" & mother_ids!="3" & mother_ids!="11") – Alia K. Saeed Feb 21 '21 at 13:38
-
This is hard to grasp without some example data and example code. Could you supplement your question with data and code that illustrate the problem? – teunbrand Feb 21 '21 at 13:40
-
> cleaned_data$mid > mother_ids = cleaned_data$mid > mother_ids = mother_ids[mother_ids!="0" & mother_ids!="1" & mother_ids!="2" & mother_ids!="3" & mother_ids!="4" & mother_ids!="5" & mother_ids!="6" & mother_ids!="7" & mother_ids!="8" & mother_ids!="9" & mother_ids!="10" & mother_ids!="11"]. # So my question is about the last function, if there is another way to do it... – Alia K. Saeed Feb 21 '21 at 13:43
-
Well, I still don't understand. Have a look [here](https://stackoverflow.com/a/5963610/11374827) on how to pose good questions and please edit your question or submit a new one if your current question is unrelated to the y axes of histograms. – teunbrand Feb 21 '21 at 15:56