I am trying to graph ct values derived from a PCR. A high ct value indicates low levels of virus and a low ct indicates high levels of virus. I would like this reflected in my data, i.e. the scale to start at 40, then go up to 0, so that 0 would be the highest value on the x axis. I know a similar question has been asked before, however I tried the code provided in the answer and found it didn't work with my data set. Here is the code I am using to make my graph in ggplot:
ggplot(data=data, aes(x = Virus.hpi, y=ct, fill=Virus.hpi))
scale_fill_manual(values = c("BTV-1 0hpsi" = "blue",
"BTV-1 72hpsi" = "blue",
"BTV-8 0hpsi" = "purple",
"BTV-8 72hpsi" = "purple"))
stat_summary(fun = mean, geom = "bar")
stat_summary(fun.data = mean_cl_normal, geom = "errorbar", width = 0.3)
geom_point(data=data1, aes(x = Virus.hpi, y=ct, fill=Virus.hpi))
I am working in Rstudio. Thanks.
CodePudding user response:
Maybe you can subtract 40 with your ct
value, to get a kind of "reversed" ct
values. Then reverse the labelling in scale_y_continuous()
.
library(ggplot2)
library(dplyr)
data %>% mutate(ct = 40 - ct) %>%
ggplot(aes(x = Virus.hpi, y=ct, fill=Virus.hpi))
scale_fill_manual(values = c("BTV-1 0hpsi" = "blue",
"BTV-1 72hpsi" = "blue",
"BTV-8 0hpsi" = "purple",
"BTV-8 72hpsi" = "purple"))
stat_summary(fun = mean, geom = "bar")
stat_summary(fun.data = mean_cl_normal, geom = "errorbar", width = 0.3)
geom_point()
scale_y_continuous(breaks = seq(0, 40, 10), labels = rev(seq(0, 40, 10)))
dummy data for demonstration
structure(list(ct = c(35L, 30L, 22L, 38L), Virus.hpi = c("BTV-1 0hpsi",
"BTV-1 72hpsi", "BTV-8 0hpsi", "BTV-8 72hpsi")), class = "data.frame", row.names = c(NA,
-4L))
data
ct Virus.hpi
1 35 BTV-1 0hpsi
2 30 BTV-1 72hpsi
3 22 BTV-8 0hpsi
4 38 BTV-8 72hpsi
CodePudding user response:
ggplot2 allows you to reverse the scales, simply add scale_y_reverse()
to your plot:
ggplot(data=data, aes(x = Virus.hpi, y=ct, fill=Virus.hpi))
scale_fill_manual(values = c("BTV-1 0hpsi" = "blue",
"BTV-1 72hpsi" = "blue",
"BTV-8 0hpsi" = "purple",
"BTV-8 72hpsi" = "purple"))
stat_summary(fun = mean, geom = "bar")
stat_summary(fun.data = mean_cl_normal, geom = "errorbar", width = 0.3)
geom_point(data=data1, aes(x = Virus.hpi, y=ct, fill=Virus.hpi))
scale_y_reverse()
alternatively, you can set the limits of your y-axis in reverse order:
ggplot(data=data, aes(x = Virus.hpi, y=ct, fill=Virus.hpi))
scale_fill_manual(values = c("BTV-1 0hpsi" = "blue",
"BTV-1 72hpsi" = "blue",
"BTV-8 0hpsi" = "purple",
"BTV-8 72hpsi" = "purple"))
stat_summary(fun = mean, geom = "bar")
stat_summary(fun.data = mean_cl_normal, geom = "errorbar", width = 0.3)
geom_point(data=data1, aes(x = Virus.hpi, y=ct, fill=Virus.hpi))
ylim(40, 0)
Note that this will, however, extend your bars downwards.