 # distribution plot

i am new in R and I am trying to visualize mean diastolic blood pressure by location and gender. (y = mean DBP , x= residence , gender)
i know I can do this by ggplot, bar, and hist function but I can't understand some function.

ggplot(df, aes(x = Residence, y = mean , fill= Gender))
geom_histogram(bins = 6, position = "dodge")

for example, what are bins?? or position?
could you please recommend me resources for learning this in detail?

As shown in the examples below, `bin` controls how finely subdivided the data are plotted. Position controls side-by-side vs. stacked. See `help(geom_historgram)'. A great guide to using ggplot is The R Graphics Cookbook by Winston Chang. It's well worth buying a physical copy.

``````suppressPackageStartupMessages({
library(dplyr)
library(ggplot2)
library(qrmix) # for blood pressure data
})

blood.pressure %>%
ggplot(., aes(x = systolic , fill= gender)) +
geom_histogram(bins = 15, position = "dodge") +
theme_minimal()
`````` ``````blood.pressure %>%
ggplot(., aes(x = systolic , fill= gender)) +
geom_histogram(bins = 15, position = "stack") +
theme_minimal()
`````` ``````blood.pressure %>%
ggplot(., aes(x = systolic , fill= gender)) +
geom_histogram(bins = 15, position = "dodge") +
theme_minimal()
`````` ``````blood.pressure %>%
ggplot(., aes(x = systolic , fill= gender)) +
geom_histogram(bins = 7, position = "dodge") +
theme_minimal()
`````` ``````blood.pressure %>%
ggplot(., aes(x = systolic , fill= gender)) +
geom_histogram(bins = 30, position = "dodge") +
theme_minimal()
`````` ``````blood.pressure %>%
ggplot(., aes(x = systolic , fill= gender, color = race)) +
geom_histogram(bins = 15, position = "dodge") +
theme_minimal()
`````` Created on 2020-12-31 by the reprex package (v0.3.0.9001)

1 Like

thank you.
I got it
one more question...
if I want to provide y by age what should I do?

``````suppressPackageStartupMessages({
library(dplyr)
library(ggplot2)
library(qrmix) # for blood pressure data
})
bindh <- function(x,y) trunc(x/y)*y

blood.pressure %>%
ggplot(., aes(x = systolic , fill = as.factor(bindh(age,5)))) +
geom_histogram(bins = 5, position = "dodge") +
theme_minimal()
`````` Created on 2021-01-01 by the reprex package (v0.3.0.9001)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.