如何使 1 成为 ggplot 中条形图的最高值

发布于 2025-01-19 12:52:32 字数 696 浏览 0 评论 0原文

我正在尝试绘制从PCR得出的CT值。高CT值表明病毒水平较低,CT低表明病毒水平很高。我希望在我的数据中反映出这一点,即从40开始开始的比例,然后达到0,以便0是x轴上最高值。我知道之前已经问过一个类似的问题,但是我尝试了答案中提供的代码,发现它与我的数据集不起作用。这是我用来在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))

我在Rstudio工作。 谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苍景流年 2025-01-26 12:52:32

ggplot2允许您逆转量表,只需在图中添加scale__y_reverse()

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()

或者,您可以以相反的顺序设置y轴的限制:

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)

请注意,请注意,这将扩展您的条形向下。

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.

吃不饱 2025-01-26 12:52:32

也许您可以用 ct 值减去 40,以获得一种“反转”的 ct 值。然后反转 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)))

rev_y_ct_val

用于演示的虚拟数据

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

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)))

rev_y_ct_val

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文