-1

I'm trying to plot the histogram of Bloodpressure vs the Outcome by using ggplot but the graph given in R is not follow the data provided in the data file. This is the link for the data file: https://www.kaggle.com/kandij/diabetes-dataset

dataset=read.csv(file.choose(),header=T)
attach(dataset)
View(dataset)
str(dataset)
summary(dataset)
Bloodpressure_Freq <- frequency(dataset$BloodPressure)
Positive_or_Negative <- as.factor(dataset$Outcome)

ggplot(data = dataset, 
aes(x = BloodPressure, Bloodpressure_Freq, fill =Positive_or_Negative)) + 
geom_col()+
labs(title = "Histogram for Age", x = "Age", y = "Count") +
theme(plot.title = element_text(hjust = 0.5))

enter image description here

StupidWolf
  • 45,075
  • 17
  • 40
  • 72
Kok Lin
  • 39
  • 6
  • 1
    Please produce a reproducible answer (https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – Vivek Katial Nov 15 '19 at 01:41
  • what is your intended plot? – StupidWolf Nov 15 '19 at 02:00
  • 1
    What's the question exactly? It's unclear what you mean by "the graph given in R is not follow the data provided in the data file." `ggplot` expects you to be pointing to columns in a data frame, but you're giving other vectors in your `aes`. – camille Nov 15 '19 at 02:48
  • In my dataset I don't not have anydata of Bloodpressure = 0 but the graph is showing me data which bloodpressure = 0 – Kok Lin Nov 15 '19 at 03:21
  • `0` and `1` are for `Outcome` not `bloodpressure`, and if you do `nrow(dataset[dataset$BloodPressure==0,])` you'll see that there are `35` cases with BP `0`. – deepseefan Nov 15 '19 at 03:24
  • And the link you've just shared confirm that there are `35` observations with `BloodPressure = 0`. See the histogram at the top of each column to verify that. – deepseefan Nov 15 '19 at 03:41

1 Answers1

2

Is this what you're looking for?

library(tidyverse)

dataset %>% 
  ggplot(aes(x=BloodPressure, fill = as.factor(Outcome))) + 
  geom_histogram() + 
  facet_wrap(~Outcome)

enter image description here

You can change the legend title using theme().

Vivek Katial
  • 543
  • 4
  • 17