I am constructing bar plots for 2 different parameters for 22 patients.
The database consists of 3 columns (for patient name, parameter I and parameter C) and thousands of rows (each of it corresponds to the value of one of those parameters (I or C) per minute, that corresponds to one of the 22 patients).
Therefore, every row represents 1 minute of monitoring in each patient.
This is the link to my example dataset for 4 patients with 674 rows of data: https://docs.google.com/spreadsheets/d/1IP8yUh4zx6JfEr1DU4N0UWeMAbViVBk73ZolCNh1-Ks/edit?usp=sharing
The issue is that I need the x axis to represent my patients and the y axis to represent the monitoring duration. Since every number represents consecutive minutes, I have expected that the y-axis on the both charts will be of the same length with the same scale. Instead of this I received the upper y value of 2000 for I parameter and 20 000 for C parameter.
This is my example code:
Maps <- read_excel("C:/Users/Lenovo/Desktop/Temp ICM/Maps for stack.xlsx")
View(Maps)
colnames(Maps) <- c("name", "A", "I", "T", "C")
#colors for I parameter
Icol <- c("green", "greenyellow", "#fed976","#feb24c",
"#fd8d3c", "#fc4e2a", "#e31a1c", "#bd0026",
"#800026", "black")
#barplot
i + geom_bar(stat = "identity")+
scale_fill_gradientn(colours = ICPcol,
limits = c(0,100),
breaks = c(20, 40, 60, 80, 100),
guide = guide_colourbar(nbin=100,
draw.ulim = FALSE,
draw.llim = FALSE))+
guides(fill = guide_colourbar(barwidth = 1, barheight = 10))+
coord_flip()+
labs(x='patient', y='Monitoring duration', fill = "ICP mmHg")+
theme(plot.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank(),
axis.line = element_line(colour = "black"),
axis.text.y = element_blank())
##Map if transform y axis in time scale with scale_y_time
i + geom_bar(stat = "identity")+
scale_fill_gradientn(colours = ICPcol,
limits = c(0,100),
breaks = c(20, 40, 60, 80, 100),
guide = guide_colourbar(nbin=100,
draw.ulim = FALSE,
draw.llim = FALSE))+
guides(fill = guide_colourbar(barwidth = 1, barheight = 10))+
coord_flip()+
scale_y_time()+
labs(x='patient', y='Monitoring duration', fill = "ICP mmHg")+
theme(plot.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank(),
axis.line = element_line(colour = "black"),
axis.text.y = element_blank())
###code for C parameter
c<- ggplot(Maps, aes(name, C, fill=C))
Ccol <- c("black", "#bd0026","#e31a1c","#fd8d3c", "#dadaeb",
"#bcbddc", "#9e9ac8", "#807dba", "#6a51a3",
"#54278f", "#3f007d")
c + geom_bar(stat = "identity")+
scale_fill_gradientn(colours = CPPcol,
limits = c(0,160),
breaks = c(60, 120),
guide = guide_colourbar(nbin=100,
draw.ulim = FALSE,
draw.llim = FALSE))+
guides(fill = guide_colourbar(barwidth = 1, barheight = 10))+
coord_flip()+
scale_y_time()+
labs(x='patient', y='Monitoring duration', fill = "CPP mmHg")+
theme(plot.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank(),
axis.line = element_line(colour = "black"),
axis.text.y = element_blank())
And this is how my graphs look like with scale_y_time() and without it
https://imgur.com/eEIrF3k
So my questions are:
How to fix the y scale, so 2 of my graphs will be comparable.
How to transform it into the time scale (I’ve tried to do it that with
scale_y_time()), which gave me the wrong number, since 674 minutes is approximately 11 hours.
Sample data
dput(Maps[1:20, ]) structure(list(Name = c("2y", "2y", "2y", "2y", "2y", "2y", "2y", "2y", "2y", "2y", "2y", "2y", "2y", "2y", "2y", "2y", "2y", "2y", "2y", "2y"), I = c(19.97, 22.01, 22.96, 14.01, 14.18, 14.2, 14.38, 14.26, 14.84, 14.75, 22.61, 23.13, 23.57, 23.27, 23.41, 21.69, 22.18, 22.62, 20.67, 17.94), C = c(63.89, 61.28, 60.04, 78.46, 78.44, 77.59, 77.44, 76.75, 77.33, 77.17, NA, NA, NA, NA, NA, NA, NA, 66.79, 73.06, 77.75)), row.names = c(NA, -20L), class = c("tbl_df", "tbl", "data.frame"))