【问题标题】:Scatter plot R for multiple values多个值的散点图 R
【发布时间】:2020-01-08 00:38:36
【问题描述】:

我无法为我的数据绘制散点图。我有 1 个自变量“应变”,我有 3 个解释值。查看结构数据框

'data.frame':   30 obs. of  4 variables:
 $ Strain       : Factor w/ 30 levels "1","10","11",..: 1 12 14 15 25 27 28 29 30 2 ...
 $ second_hour  : Factor w/ 30 levels "10356.3888888889",..: 15 16 8 14 7 6 11 10 13 12 ...
 $ second_hour_n: Factor w/ 30 levels "10149.4751953184",..: 5 4 15 6 18 19 13 14 9 12 ...
 $ Beula        : num  21674 21308 19905 20817 20017 ...

> head(hour_2)
  Strain      second_hour    second_hour_n    Beula
1      1 19354.4444444444 12103.3628274451 21673.72
2      2 20021.2222222222 11577.7991047524 21307.61
3      3 16105.9444444444 14425.8808435683 19905.39
4      4 18993.3888888889 12149.3204615723 20816.78
5      5 15541.3888888889 15370.8433645383 20016.94
6      6 14767.1666666667 16288.3635541566 19000.44

我想在散点图中绘制每个带颜色编码的菌株的每个解释值。

在我目前的尝试中,我首先使用以下代码融化数据框:

> hour_2_melted <- melt(hour_2, id.vars = "Strain")
Warning message:
attributes are not identical across measure variables; they will be dropped

然后我绘制

ggplot(hour_2_melted, aes(Strain, value)) + geom_point() 

但是 Y 轴不能更改,因为它是连续的,我不希望每个值都显示在 y 轴上。 x轴的顺序也很奇怪。最后,如何对 3 个不同的解释值进行颜色编码?

感谢任何帮助。

【问题讨论】:

  • 您能否提供一个可重现的数据集示例?见:stackoverflow.com/questions/5963269/…
  • 意外的绘图是因为您已将三列数据作为“因子”导入,而您几乎肯定希望它们为数字。在导入时更改上游,或使用hour_2$Strain = as.numeric(as.character(hour_2$Strain)) 等转换为数字。

标签: r ggplot2


【解决方案1】:

您可以使用tidyr 包和函数pivot_longerggplot2 重塑您的数据:

library(tidyr)
library(dplyr)
df %>% pivot_longer(., - Strain, names_to = "Variable", values_to = "Value")

# A tibble: 18 x 3
   Strain Variable       Value
    <int> <chr>          <dbl>
 1      1 second_hour   19354.
 2      1 second_hour_n 12103.
 3      1 Beula         21674.
 4      2 second_hour   20021.
 5      2 second_hour_n 11578.
 6      2 Beula         21308.
 7      3 second_hour   16106.
 8      3 second_hour_n 14426.
 9      3 Beula         19905.
10      4 second_hour   18993.
11      4 second_hour_n 12149.
12      4 Beula         20817.
13      5 second_hour   15541.
14      5 second_hour_n 15371.
15      5 Beula         20017.
16      6 second_hour   14767.
17      6 second_hour_n 16288.
18      6 Beula         19000.

然后对于绘图,您可以将其作为一系列管道传递

library(tidyr)
library(dplyr)
library(ggplot2)
df %>% pivot_longer(., - Strain, names_to = "Variable", values_to = "Value") %>%
  ggplot(aes(x = Strain, y = Value, color = Variable))+
  geom_point()

关于 x 轴顺序的问题,使用我的答案代码和我提供的可重现示例(见下文),我无法重现您的问题(即使我在重塑之前将 Strain 转换为因子级别数据框):

library(tidyr)
library(dplyr)
library(ggplot2)
df$Strain <- as.factor(df$Strain)
df %>% pivot_longer(., - Strain, names_to = "Variable", values_to = "Value") %>%
  ggplot(aes(x = Strain, y = Value, color = Variable))+
  geom_point()

但是,根据您的数据框,我建议您通过以下方式更改数值中的因子水平:

hour_2$Strain <- as.numeric(as.vector(hour_2$Strain))
hour_2$second_hour <- as.numeric(as.vector(hour_2$second_hour))
hour_2$second_hour_n <- as.numeric(as.vector(hour_2$second_hour_n))

它回答了你的问题吗?

数据

structure(list(Strain = 1:6, second_hour = c(19354.4444444444, 
20021.2222222222, 16105.9444444444, 18993.3888888889, 15541.3888888889, 
14767.1666666667), second_hour_n = c(12103.3628274451, 11577.7991047524, 
14425.8808435683, 12149.3204615723, 15370.8433645383, 16288.3635541566
), Beula = c(21673.72, 21307.61, 19905.39, 20816.78, 20016.94, 
19000.44)), class = "data.frame", row.names = c(NA, -6L))

数据 2

structure(list(Strain = c(1L, 2L, 21L, 44L, 5L, 6L), second_hour = c(19354.4444444444, 
20021.2222222222, 16105.9444444444, 18993.3888888889, 15541.3888888889, 
14767.1666666667), second_hour_n = c(12103.3628274451, 11577.7991047524, 
14425.8808435683, 12149.3204615723, 15370.8433645383, 16288.3635541566
), Beula = c(21673.72, 21307.61, 19905.39, 20816.78, 20016.94, 
19000.44)), class = "data.frame", row.names = c(NA, -6L))

【讨论】:

  • 哇哦,你的回答帮了大忙。感谢您向我介绍此枢轴更长功能!
  • 乐于助人 ;)
猜你喜欢
  • 2014-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-08
  • 1970-01-01
  • 2016-04-07
  • 1970-01-01
  • 2017-04-09
相关资源
最近更新 更多