GGPLOT Barchart help

I'm trying to make a Barchart with GGPLOT the code I am using is as follows:

ggplot(data = Clean.Blood.Preasure, mapping = aes(Blood.Pressure, Mean.BP)) +
  geom_bar(stat = "summary" ) +
  geom_errorbar(aes(ymin = Mean.BP-se.BP, ymax = Mean.BP + se.BP), width = .2)

My data is as follows, I didn't know how else I could attach it other than just copying it from the excel spreadsheet to here:

|Blood.Pressure|Mean|SD|SEM|Count|P Value|
|Resting.Systolic|116.5746|13.39587|1.157227|134|2.20E-16|
|Resting.Diastolic|70.29104|10.6233|0.9177134|134|0.06099|
|Exercise.Systolic|134.8806|15.72128|1.358112|134|2.20E-16|
|Exercise.Diastolic|72.47761|14.21358|1.227866|134|0.06099|
|Recovery.Systolic|115.4104|13.52926|1.16875|134||
|Recovery.Diastolic|71.33582|12.36472|0.9177134|134||

I'm trying to show the mean of each category on the y axis so assuming stat = summary achieves this?

If anyone could please help with how I could achieve the following I would be extremely grateful.

  1. The tick marks on the y axis firstly don't go all the way to the top of the axis and secondly are too sparse I would like them to be at intervals of 20.
  2. I would like the bars to be ordered in the following order- resting systolic, resting diastolic, exercise systolic, exercise diastolic, recovery systolic, recovery diastolic
  3. I would like to colour systolic bars differently to diastolic bars to show contrast
  4. Also is it possible to include asterixes to show the statistical significance between resting systolic/exercise systolic, and resting diastolic and exercise diastolic.

Thank you in advance with any help you can give.

Here are two ways to get items 1-3. Number 4 can be done but I don't recall which package facilitates that.

library(ggplot2)
Clean.Blood.Preasure <- read.csv("~/R//Play/Dummy.csv",sep="|")
Clean.Blood.Preasure$Blood.Pressure <- factor(Clean.Blood.Preasure$Blood.Pressure,
                                              levels = c("Resting.Systolic", "Resting.Diastolic", "Exercise.Systolic", 
                                                         "Exercise.Diastolic", "Recovery.Systolic", "Recovery.Diastolic"))
ggplot(data = Clean.Blood.Preasure, mapping = aes(Blood.Pressure, Mean)) +
  geom_col(fill=rep(c("red","blue"),3)) +
  geom_errorbar(aes(ymin = Mean-SD, ymax = Mean + SD), width = .2) +
  scale_y_continuous(breaks = seq(0,160,20))

library(tidyr)  
Clean.Blood.Preasure2 <- separate(data = Clean.Blood.Preasure,col = Blood.Pressure,
                                  into = c("State","Measure"))
Clean.Blood.Preasure2
#>      State   Measure      Mean       SD       SEM Count   P.Value
#> 1  Resting  Systolic 116.57460 13.39587 1.1572270   134 2.200e-16
#> 2  Resting Diastolic  70.29104 10.62330 0.9177134   134 6.099e-02
#> 3 Exercise  Systolic 134.88060 15.72128 1.3581120   134 2.200e-16
#> 4 Exercise Diastolic  72.47761 14.21358 1.2278660   134 6.099e-02
#> 5 Recovery  Systolic 115.41040 13.52926 1.1687500   134        NA
#> 6 Recovery Diastolic  71.33582 12.36472 0.9177134   134        NA
ggplot(data = Clean.Blood.Preasure2, mapping = aes(State, Mean,fill=Measure)) +
  geom_col(position = "dodge") +
  geom_errorbar(aes(ymin = Mean-SD, ymax = Mean + SD), position=position_dodge(0.9), width = .2) +
  scale_y_continuous(breaks = seq(0,160,20))

Created on 2021-11-07 by the reprex package (v2.0.1)

1 Like

This topic was automatically closed 7 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.