I want to visualize the reference ranges of several liver enzymes (for example GOT and GPT) that I calculated with two programs "kosmic" and "RLE" using ggplot2.
I do not understand why the bars always start at 0, even if the lower range is for example 16.02.
How do I need to change my code so the minimum and maximum values of the bars look like that:
[16.02,45.46] [9.16,60.52] [16.10,68.90] and [9.30,64.40].
Thank you in advance!
#install.packages("ggplot2") library(ggplot2) program <- c(rep("kosmic",4),rep("RLE",4)) value <- c(16.02,45.46,9.16,60.52,16.1,48.9,9.3,64.4) parameter <- c(rep("GOT",2),rep("GPT",2),rep("GOT",2),rep("GPT",2)) table1 <- data.frame(program,value,parameter) p <- ggplot(table1, aes(parameter,value, fill = program))+ geom_bar(position="dodge", stat="identity") print(p)
I am looking for something like this:
Are you looking for something like this?
library(dplyr)
table1 %>%
group_by(parameter, program) %>%
summarize(min = min(value),
median = median(value),
max = max(value), .groups = "drop") %>%
ggplot(aes(interaction(parameter,program), fill = program))+
geom_tile(aes(y = median, height = max-min), width = 0.6)
Edit: Okay this is hacky, but:
table1 %>%
# example of reordering the parameters
mutate(parameter = fct_relevel(parameter, "GPT", after = 0)) %>%
# forcats offers a variety of fct_*** functions to change factors
# (factors are a data type that can separately store labels and ordering)
group_by(parameter, program) %>%
summarize(min = min(value),
median = median(value),
mean = mean(value),
max = max(value), .groups = "drop") %>%
ggplot(aes(parameter, mean, color = program))+
geom_errorbar(aes(ymin = min, ymax = max),
position = position_dodge(width = 0.3), size = 10,
width = 0) +
# control the legend so the key squares aren't gigantic to match the error bar widths
guides(colour = guide_legend(override.aes = list(size=8))) +
# example of assigning different colors.
# a variety of scale_color_* functions are available
scale_color_manual(values = c("kosmic" = "#cc5588", "RLE" = "#779988"))
A downside of this is that the width/spacing of the bars will vary depending on your graphic output aspect ratio, so to use it might take some fiddling to get as you want.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.