在R中使用ggplot绘制多个变量在y轴上的图表。

huangapple go评论111阅读模式
英文:

Plot multiple variable on y axis in R using ggplot

问题

我是新手使用R,但有一些Matlab的经验。我正在尝试在一个图表上制作三种不同区域测量值的比较图,按动物模型的基因型进行分面。我想要按基因型(Control和Cre-)来绘制Glom.Area、Glom.Tuft和Mesangial.Area。

我的代码如下:

  1. # 设置环境
  2. library(tidyverse)
  3. library(ggbeeswarm)
  4. library(readxl)
  5. # 工作目录
  6. setwd("C:/Users/k_nic/OneDrive/Desktop")
  7. # 读取CSV文件
  8. t1 <- read.csv("sample.csv", header = TRUE)
  9. # 绘制一个变量的图表
  10. ggplot(t1, aes(x = Genotype, y = Glom.Area))

数据集:

  1. Genotype Sex Glom.Area Glom.Tuft Mesangial.Area
  2. 1 Control M 6617.669 3804.877 1917.310
  3. 2 Control M 7986.696 5377.977 2761.949
  4. 3 Control M 8617.410 4443.345 2335.244
  5. 4 Control M 7785.355 4307.021 2381.512
  6. 5 Control M 9159.720 5568.449 3163.232
  7. 6 Control M 8484.327 5314.041 3238.861
  8. 7 Control M 10315.521 6105.294 3429.906
  9. 8 Control M 5517.161 3090.970 1878.288
  10. 9 Cre- M 6075.166 3438.421 2147.758
  11. 10 Cre- M 8947.698 5809.953 3744.117
  12. 11 Cre- M 6677.471 4315.344 2113.502
  13. 12 Cre- M 7616.932 4293.291 2763.791
  14. 13 Cre- M 8191.464 4778.529 2159.007
  15. 14 Cre- M 8549.339 6043.961 2922.295
  16. 15 Cre- M 11057.196 6546.740 3771.891
  17. 16 Cre- M 5911.768 3382.556 1754.674

我希望它看起来类似于以下图表:

在R中使用ggplot绘制多个变量在y轴上的图表。

非常感谢任何帮助!

英文:

I am new to R but have some experience with matlab. I am trying to make comparison plots between three different area measurements on one graph faceted by genotype of animal model. I would like to plot Glom.Area, Glom.Tuft, and Mesangial.Area faceted by genotype (control v Cre -).

My code so far:

  1. # Setup
  2. library(tidyverse)
  3. library(ggbeeswarm)
  4. library(readxl)
  5. # work space
  6. setwd(&quot;C:/Users/k_nic/OneDrive/Desktop&quot;)
  7. # Read in csv file
  8. t1 &lt;- read.csv(&quot;sample.csv&quot;, header = TRUE)
  9. # plot for one varible
  10. ggplot(t1, aes(x = Genotype, y= Glom.Area))

Dataset:

  1. Genotype Sex Glom.Area Glom.Tuft Mesangial.Area
  2. 1 Control M 6617.669 3804.877 1917.310
  3. 2 Control M 7986.696 5377.977 2761.949
  4. 3 Control M 8617.410 4443.345 2335.244
  5. 4 Control M 7785.355 4307.021 2381.512
  6. 5 Control M 9159.720 5568.449 3163.232
  7. 6 Control M 8484.327 5314.041 3238.861
  8. 7 Control M 10315.521 6105.294 3429.906
  9. 8 Control M 5517.161 3090.970 1878.288
  10. 9 Cre- M 6075.166 3438.421 2147.758
  11. 10 Cre- M 8947.698 5809.953 3744.117
  12. 11 Cre- M 6677.471 4315.344 2113.502
  13. 12 Cre- M 7616.932 4293.291 2763.791
  14. 13 Cre- M 8191.464 4778.529 2159.007
  15. 14 Cre- M 8549.339 6043.961 2922.295
  16. 15 Cre- M 11057.196 6546.740 3771.891
  17. 16 Cre- M 5911.768 3382.556 1754.674

This is what I would like it to look something like:

在R中使用ggplot绘制多个变量在y轴上的图表。

Any help is greatly appreciated!

答案1

得分: 0

  1. 在这种方法中,我们将三个测量转化为长格式的数据。我们将测量名称映射到 x 轴,相应的数值映射到 y 轴。将基因型映射到填充美观度会产生一对列。
  2. ``` r
  3. library(tidyverse)
  4. df %>%
  5. pivot_longer(c(Glom.Area, Glom.Tuft, Mesangial.Area)) %>%
  6. ggplot(aes(name, value, fill = Genotype)) +
  7. geom_col(position = position_dodge())

在R中使用ggplot绘制多个变量在y轴上的图表。

创建于2023年06月15日,使用 reprex v2.0.2

  1. <details>
  2. <summary>英文:</summary>
  3. In this approach, we pivot the three measurements into long-form data. We map the measurement names to the x-axis and their respective values along the y. Mapping the Genotypes to the fill aesthetic yields the pairs of columns.
  4. ``` r
  5. library(tidyverse)
  6. df %&gt;%
  7. pivot_longer(c(Glom.Area, Glom.Tuft, Mesangial.Area)) %&gt;%
  8. ggplot(aes(name, value, fill = Genotype)) +
  9. geom_col(position = position_dodge())

在R中使用ggplot绘制多个变量在y轴上的图表。<!-- -->

<sup>Created on 2023-06-15 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年6月16日 09:35:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76486464.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定