英文:
Plot multiple variable on y axis in R using ggplot
问题
我是新手使用R,但有一些Matlab的经验。我正在尝试在一个图表上制作三种不同区域测量值的比较图,按动物模型的基因型进行分面。我想要按基因型(Control和Cre-)来绘制Glom.Area、Glom.Tuft和Mesangial.Area。
我的代码如下:
# 设置环境
library(tidyverse)
library(ggbeeswarm)
library(readxl)
# 工作目录
setwd("C:/Users/k_nic/OneDrive/Desktop")
# 读取CSV文件
t1 <- read.csv("sample.csv", header = TRUE)
# 绘制一个变量的图表
ggplot(t1, aes(x = Genotype, y = Glom.Area))
数据集:
Genotype Sex Glom.Area Glom.Tuft Mesangial.Area
1 Control M 6617.669 3804.877 1917.310
2 Control M 7986.696 5377.977 2761.949
3 Control M 8617.410 4443.345 2335.244
4 Control M 7785.355 4307.021 2381.512
5 Control M 9159.720 5568.449 3163.232
6 Control M 8484.327 5314.041 3238.861
7 Control M 10315.521 6105.294 3429.906
8 Control M 5517.161 3090.970 1878.288
9 Cre- M 6075.166 3438.421 2147.758
10 Cre- M 8947.698 5809.953 3744.117
11 Cre- M 6677.471 4315.344 2113.502
12 Cre- M 7616.932 4293.291 2763.791
13 Cre- M 8191.464 4778.529 2159.007
14 Cre- M 8549.339 6043.961 2922.295
15 Cre- M 11057.196 6546.740 3771.891
16 Cre- M 5911.768 3382.556 1754.674
我希望它看起来类似于以下图表:
非常感谢任何帮助!
英文:
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:
# Setup
library(tidyverse)
library(ggbeeswarm)
library(readxl)
# work space
setwd("C:/Users/k_nic/OneDrive/Desktop")
# Read in csv file
t1 <- read.csv("sample.csv", header = TRUE)
# plot for one varible
ggplot(t1, aes(x = Genotype, y= Glom.Area))
Dataset:
Genotype Sex Glom.Area Glom.Tuft Mesangial.Area
1 Control M 6617.669 3804.877 1917.310
2 Control M 7986.696 5377.977 2761.949
3 Control M 8617.410 4443.345 2335.244
4 Control M 7785.355 4307.021 2381.512
5 Control M 9159.720 5568.449 3163.232
6 Control M 8484.327 5314.041 3238.861
7 Control M 10315.521 6105.294 3429.906
8 Control M 5517.161 3090.970 1878.288
9 Cre- M 6075.166 3438.421 2147.758
10 Cre- M 8947.698 5809.953 3744.117
11 Cre- M 6677.471 4315.344 2113.502
12 Cre- M 7616.932 4293.291 2763.791
13 Cre- M 8191.464 4778.529 2159.007
14 Cre- M 8549.339 6043.961 2922.295
15 Cre- M 11057.196 6546.740 3771.891
16 Cre- M 5911.768 3382.556 1754.674
This is what I would like it to look something like:
Any help is greatly appreciated!
答案1
得分: 0
在这种方法中,我们将三个测量转化为长格式的数据。我们将测量名称映射到 x 轴,相应的数值映射到 y 轴。将基因型映射到填充美观度会产生一对列。
``` r
library(tidyverse)
df %>%
pivot_longer(c(Glom.Area, Glom.Tuft, Mesangial.Area)) %>%
ggplot(aes(name, value, fill = Genotype)) +
geom_col(position = position_dodge())
创建于2023年06月15日,使用 reprex v2.0.2
<details>
<summary>英文:</summary>
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.
``` r
library(tidyverse)
df %>%
pivot_longer(c(Glom.Area, Glom.Tuft, Mesangial.Area)) %>%
ggplot(aes(name, value, fill = Genotype)) +
geom_col(position = position_dodge())
<!-- -->
<sup>Created on 2023-06-15 with reprex v2.0.2</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论